news 2026/5/13 16:37:31

操作历史 - Cordova 与 OpenHarmony 混合开发实战

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
操作历史 - Cordova 与 OpenHarmony 混合开发实战

欢迎大家加入开源鸿蒙跨平台开发者社区,一起共建开源鸿蒙跨平台生态。

📌 模块概述

操作历史模块是MovieTracker应用中用于记录用户操作的功能。系统会记录用户的所有操作,如添加影片、编辑影片、删除影片等。用户可以查看操作历史,了解自己的操作记录,同时可以撤销或重做某些操作。

该模块的主要功能包括:记录操作、查看历史、撤销操作、重做操作、清空历史等。通过Cordova框架与OpenHarmony原生能力的结合,实现了完整的操作历史管理。

操作历史需要处理大量的操作记录,同时需要支持撤销和重做功能。

🔗 完整流程

第一步:操作记录

系统在用户执行操作时自动记录操作信息,包括操作类型、操作对象、操作时间等。记录过程需要异步进行,避免影响用户体验。

操作记录需要存储在数据库中,支持后续的查询和分析。

第二步:历史查看

用户可以查看所有的操作历史,按时间倒序排列。历史列表需要显示操作的类型、对象、时间等信息。

用户可以搜索和筛选历史记录,快速找到特定的操作。

第三步:撤销与重做

用户可以撤销最近的操作,系统会恢复到操作前的状态。同时支持重做操作,用户可以重新执行撤销的操作。

撤销和重做需要维护一个操作栈,记录所有的操作和状态变化。

🔧 Web代码实现

操作历史HTML结构

<divid="history-page"class="page"><divclass="page-header"><h2>操作历史</h2><divclass="history-actions"><buttonclass="btn btn-secondary"onclick="undoLastOperation()"id="undo-btn">↶ 撤销</button><buttonclass="btn btn-secondary"onclick="redoLastOperation()"id="redo-btn">↷ 重做</button><buttonclass="btn btn-danger"onclick="clearHistory()">🗑️ 清空历史</button></div></div><divclass="history-list"id="history-list"></div></div>

操作历史实现

letoperationHistory=[];lethistoryIndex=-1;asyncfunctionrecordOperation(type,object,details){constoperation={type:type,object:object,details:details,timestamp:Date.now(),id:Date.now()};// 移除重做栈中的操作operationHistory=operationHistory.slice(0,historyIndex+1);operationHistory.push(operation);historyIndex++;// 保存到数据库awaitdb.addHistory(operation);updateHistoryButtons();}asyncfunctionundoLastOperation(){if(historyIndex<0)return;constoperation=operationHistory[historyIndex];historyIndex--;// 执行撤销操作awaitperformUndo(operation);updateHistoryButtons();loadHistoryList();}asyncfunctionredoLastOperation(){if(historyIndex>=operationHistory.length-1)return;historyIndex++;constoperation=operationHistory[historyIndex];// 执行重做操作awaitperformRedo(operation);updateHistoryButtons();loadHistoryList();}asyncfunctionperformUndo(operation){// 根据操作类型执行撤销switch(operation.type){case'add':awaitdb.deleteMovie(operation.object);break;case'delete':awaitdb.addMovie(operation.details);break;case'update':awaitdb.updateMovie(operation.object,operation.details.oldData);break;}}asyncfunctionperformRedo(operation){// 根据操作类型执行重做switch(operation.type){case'add':awaitdb.addMovie(operation.details);break;case'delete':awaitdb.deleteMovie(operation.object);break;case'update':awaitdb.updateMovie(operation.object,operation.details.newData);break;}}functionupdateHistoryButtons(){document.getElementById('undo-btn').disabled=historyIndex<0;document.getElementById('redo-btn').disabled=historyIndex>=operationHistory.length-1;}asyncfunctionloadHistoryList(){try{consthistory=awaitdb.getHistory();renderHistoryList(history);}catch(error){console.error('加载历史失败:',error);}}functionrenderHistoryList(history){constcontainer=document.getElementById('history-list');container.innerHTML='';if(history.length===0){container.innerHTML='<p class="empty-message">暂无操作历史</p>';return;}history.reverse().forEach(op=>{constitem=document.createElement('div');item.className='history-item';constdate=newDate(op.timestamp).toLocaleString('zh-CN');consttypeText={'add':'添加','delete':'删除','update':'修改'}[op.type]||op.type;item.innerHTML=`<span class="operation-type">${typeText}</span> <span class="operation-object">${op.object}</span> <span class="operation-time">${date}</span>`;container.appendChild(item);});}asyncfunctionclearHistory(){if(confirm('确定要清空操作历史吗?')){try{awaitdb.clearHistory();operationHistory=[];historyIndex=-1;loadHistoryList();updateHistoryButtons();showSuccess('历史已清空');}catch(error){console.error('清空历史失败:',error);showError('清空历史失败');}}}

🔌 OpenHarmony原生代码

操作历史插件

// HistoryPlugin.etsimport{webview}from'@kit.ArkWeb';import{common}from'@kit.AbilityKit';exportclassHistoryPlugin{privatecontext:common.UIAbilityContext;constructor(context:common.UIAbilityContext){this.context=context;}publicregisterHistory(controller:webview.WebviewController):void{controller.registerJavaScriptProxy({object:newHistoryBridge(),name:'historyNative',methodList:['recordOperation','getOperationStats']});}}exportclassHistoryBridge{publicrecordOperation(operationJson:string):string{try{constoperation=JSON.parse(operationJson);returnJSON.stringify({success:true,operationId:operation.id,timestamp:operation.timestamp});}catch(error){returnJSON.stringify({success:false,error:error.message});}}publicgetOperationStats(historyJson:string):string{try{consthistory=JSON.parse(historyJson);conststats={totalCount:history.length,addCount:history.filter((h:any)=>h.type==='add').length,deleteCount:history.filter((h:any)=>h.type==='delete').length,updateCount:history.filter((h:any)=>h.type==='update').length};returnJSON.stringify(stats);}catch(error){returnJSON.stringify({error:error.message});}}}

📝 总结

操作历史模块展示了Cordova与OpenHarmony混合开发中的操作记录和撤销重做功能。通过Web层提供历史查看界面,同时利用OpenHarmony原生能力进行操作统计。

在实现这个模块时,需要注意操作记录的完整性、撤销重做的准确性、以及性能的优化。通过合理的架构设计,可以构建出高效、易用的操作历史功能。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/11 20:04:03

时间轴模块 - Cordova与OpenHarmony混合开发实战

欢迎大家加入开源鸿蒙跨平台开发者社区&#xff0c;一起共建开源鸿蒙跨平台生态。 &#x1f4cc; 概述 时间轴模块用于以时间顺序展示所有日记。这个模块提供了按时间排序的日记视图&#xff0c;用户可以直观地看到日记的时间分布。通过Cordova框架&#xff0c;我们能够在Web层…

作者头像 李华
网站建设 2026/5/12 15:23:12

淘宝购物助手:3大技巧助你提升购物效率,轻松选购心仪好物!

还在为淘宝热门商品总是"库存不足"而烦恼吗&#xff1f;每次看到喜欢的商品很快售罄&#xff0c;那种失落感是不是让你有些无奈&#xff1f;别担心&#xff0c;今天我要分享的淘宝购物助手就是你的好帮手&#xff0c;它能帮你提升购物效率&#xff0c;让你更轻松地选…

作者头像 李华
网站建设 2026/4/28 20:29:05

LangFlow与Redis集成:实现高速数据缓存与共享

LangFlow与Redis集成&#xff1a;实现高速数据缓存与共享 在构建现代AI应用的实践中&#xff0c;一个日益突出的矛盾逐渐显现&#xff1a;开发者渴望快速验证复杂逻辑&#xff0c;而现实却是——每修改一次提示词就得重跑一遍LLM调用&#xff0c;每次测试都伴随着数秒甚至更长的…

作者头像 李华
网站建设 2026/5/10 22:00:17

LangFlow与Excel文件处理:读取、写入与数据分析

LangFlow与Excel文件处理&#xff1a;读取、写入与数据分析 在企业数据仍大量沉淀于Excel表格的今天&#xff0c;如何让大语言模型&#xff08;LLM&#xff09;真正“读懂”这些业务报表&#xff0c;并自动完成分析、摘要甚至决策建议&#xff1f;这不仅是技术挑战&#xff0c;…

作者头像 李华
网站建设 2026/5/13 6:10:19

LangFlow中的绩效评估助手:自动生成评语与建议

LangFlow中的绩效评估助手&#xff1a;自动生成评语与建议 在企业HR的实际工作中&#xff0c;每到季度或年度考核期&#xff0c;撰写员工绩效评语往往成了一项令人头疼的任务。几十甚至上百份评语要逐个打磨&#xff0c;既要体现差异化&#xff0c;又要保持语气一致、结构规范&…

作者头像 李华
网站建设 2026/5/8 5:31:03

LangFlow中的OCR节点:图像文字识别集成方案

LangFlow中的OCR节点&#xff1a;图像文字识别集成方案 在智能应用开发日益复杂的今天&#xff0c;如何快速将现实世界中的非结构化信息——比如一张合同截图、一份扫描版发票或教科书的一页照片——转化为可被大语言模型理解与处理的数据&#xff0c;已成为多模态AI系统构建的…

作者头像 李华