当前位置: 首页 > news >正文

旅游网站规划设计微指数查询

旅游网站规划设计,微指数查询,内地公司 香港服务器 经营性网站,仿模板电影网站本文将通过具体的代码示例,详细解释如何在 Vue3 中使用 CodeMirror 6 实现文本插入功能,包括在光标位置插入文本和选中文本插入文本的代码示例,以及这两种插入方式的区别。 1. 只能在光标位置插入文本 1.1 代码示例 const insertTemplate …

本文将通过具体的代码示例,详细解释如何在 Vue3 中使用 CodeMirror 6 实现文本插入功能,包括在光标位置插入文本和选中文本插入文本的代码示例,以及这两种插入方式的区别。

1. 只能在光标位置插入文本

1.1 代码示例

const insertTemplate = (template) => {try {if (!editorView.value) return;const state = editorView.value.state;const selection = state.selection.main;const currentPos = selection.head;const anchor = currentPos + template.length;const head = selection.main.head;console.log('当前光标位置:', currentPos);editorView.value.dispatch({changes: {from: currentPos,to: currentPos,insert: template},selection: EditorSelection.range(anchor, head),scrollIntoView: true});editorView.value.focus();} catch (e) {console.error('插入失败:', e);}
};

1.2 详细解释

  1. 获取编辑器状态和选择范围

    const state = editorView.value.state;
    const selection = state.selection.main;
    const currentPos = selection.head;
    • state:获取当前编辑器的状态。

    • selection:获取当前选择范围。

    • currentPos:获取当前光标位置。

  2. 计算新光标位置

    const anchor = currentPos + template.length;
    const head = selection.main.head;
    • anchor:新光标位置的起始点。

    • head:新光标位置的结束点。

  3. 执行插入操作

editorView.value.dispatch({changes: {from: currentPos,to: currentPos,insert: template},selection: EditorSelection.range(anchor, head),scrollIntoView: true
});
  • changes:定义插入操作的范围和内容,from和to要特别注意,要不报错。

  • selection:设置新的选择范围。

  • scrollIntoView:确保插入位置滚动到可见区域。

遇见这样的错误,很大概率是from和to搞错了。

更新编辑器

editorView.value.focus();

2. 可以根据用户是否选中文本,在选中文本位置替换插入文本

2.1 代码示例

const insertTemplate = (template) => {try {if (!editorView.value) return;const state = editorView.value.state;const selection = state.selection.main;if (selection.empty) {const from = selection.main.from;const to = selection.main.to;editorView.value.dispatch({changes: {from: from,to: to,insert: template},selection: EditorSelection.range(from + template.length, selection.main.from)});} else {const posFrom = selection.main.from;const anchor = posFrom + template.length;editorView.value.dispatch({changes: {from: posFrom,to: posFrom,insert: template},selection: EditorSelection.range(anchor, selection.main.from)});}editorView.value.focus();} catch (e) {console.error('插入失败:', e);}
};

2.2 详细解释

  1. 判断是否选中文本

    if (selection.empty) {// 选中文本为空
    } else {// 选中文本不为空
    }

selection.empty:查询codemirror6的文档,可以知道该属性可以用来判断from和to是否相同,进而判断当前选择范围是否为空。

  • 处理选中文本为空的情况

    const from = selection.main.from;
    const to = selection.main.to;editorView.value.dispatch({changes: {from: from,to: to,insert: template},selection: EditorSelection.range(from + template.length, selection.main.from)
    });
    • fromto:获取当前选择范围的起始和结束位置,此时from和to不同。

    • changes:定义插入操作的范围和内容。

    • selection:设置新的选择范围。

  • 处理选中文本不为空的情况

    const posFrom = selection.main.from;
    const anchor = posFrom + template.length;editorView.value.dispatch({changes: {from: posFrom,to: posFrom,insert: template},selection: EditorSelection.range(anchor, selection.main.from)
    });
    • posFrom:获取当前选择范围的起始位置,from和to相同。

    • anchor:计算新光标位置的起始点。

    • changes:定义插入操作的范围和内容。

    • selection:设置新的选择范围。

3. 两种插入方式的区别

3.1 在光标位置插入文本

  • 适用场景:用户希望在当前光标位置插入文本,而不影响其他内容。

  • 实现方式:在当前光标位置插入文本,并更新光标位置。

3.2 在选中文本位置插入文本

  • 适用场景:用户希望在选中的文本范围内插入文本,可能替换选中的文本。

  • 实现方式:在选中的文本范围内插入文本,并更新选择范围。

4. 常见问题及解决方案

4.1 插入失败:RangeError: Invalid change range

问题描述:在插入文本时,可能会遇到 RangeError: Invalid change range 错误。

解决方案

  • 确保 fromto 的值正确,且 from 小于等于 to

  • 确保插入的文本范围在文档范围内。

4.2 光标位置不正确

问题描述:编辑器组件的值来自父组件的传值,在初始化父组件传值之后,每次插入文本都会在刚开始的第一行插入,无论怎么选中光标也不行,后来感觉光标位置可能不正确。原因就是在父组件传值之后,光标位置并没有更新,所以每次插入还是从0开始。

解决方案

确保在父组件传值之后,在 EditorView.value.dispatch 方法中正确设置 selection,设置正确的光标位置

4.3 如何监听光标位置,判断是否是光标位置错误的问题

问题描述:如何监听光标位置,并在控制台输出,以此判断是否是光标位置错误的问题

解决方案

state的插件中使用一个函数输出光标起始位置。

4.4 怎么判断自己写的事务是不是有效的

问题描述:在代码调试时,我一度怀疑是自己的事务使用错了,导致无法成功。

解决方案

可以简单使用下面的函数判断一下事务是不是有效的。

5. 总结

通过以上代码示例和详细解释,我们可以在 Vue3 中成功使用 CodeMirror 6 实现文本插入功能。希望本文能够帮助大家更好地理解和使用 CodeMirror 6,有什么问题欢迎大家在评论区一起交流。


文章转载自:
http://filiale.c7627.cn
http://bicarbonate.c7627.cn
http://gynecologist.c7627.cn
http://deduck.c7627.cn
http://cyclize.c7627.cn
http://observingly.c7627.cn
http://inofficial.c7627.cn
http://afrikaans.c7627.cn
http://roughrider.c7627.cn
http://orem.c7627.cn
http://preventorium.c7627.cn
http://psychosurgery.c7627.cn
http://duce.c7627.cn
http://hemanalysis.c7627.cn
http://miscounsel.c7627.cn
http://spermatocide.c7627.cn
http://secluded.c7627.cn
http://qbe.c7627.cn
http://protanope.c7627.cn
http://lux.c7627.cn
http://siderosis.c7627.cn
http://prodigal.c7627.cn
http://oxysome.c7627.cn
http://capris.c7627.cn
http://balata.c7627.cn
http://dardic.c7627.cn
http://hover.c7627.cn
http://delve.c7627.cn
http://corticole.c7627.cn
http://cyclamen.c7627.cn
http://closemouthed.c7627.cn
http://gunnery.c7627.cn
http://potentilla.c7627.cn
http://eyelashes.c7627.cn
http://upload.c7627.cn
http://ruijin.c7627.cn
http://quadrireme.c7627.cn
http://rehabilitant.c7627.cn
http://rainproof.c7627.cn
http://decumbent.c7627.cn
http://angrily.c7627.cn
http://klagenfurt.c7627.cn
http://overdelicacy.c7627.cn
http://antithetical.c7627.cn
http://folia.c7627.cn
http://accessable.c7627.cn
http://nin.c7627.cn
http://growl.c7627.cn
http://contralateral.c7627.cn
http://ric.c7627.cn
http://deanship.c7627.cn
http://fortuitist.c7627.cn
http://jeepable.c7627.cn
http://mna.c7627.cn
http://atropin.c7627.cn
http://concelebrate.c7627.cn
http://acyclic.c7627.cn
http://comous.c7627.cn
http://coldstart.c7627.cn
http://regelate.c7627.cn
http://bobolink.c7627.cn
http://meliorism.c7627.cn
http://skiametry.c7627.cn
http://gamahuche.c7627.cn
http://predisposition.c7627.cn
http://unfilial.c7627.cn
http://lice.c7627.cn
http://lienal.c7627.cn
http://ruderal.c7627.cn
http://subluxate.c7627.cn
http://woops.c7627.cn
http://twp.c7627.cn
http://overgreat.c7627.cn
http://interscholastic.c7627.cn
http://carburant.c7627.cn
http://waspish.c7627.cn
http://night.c7627.cn
http://guayule.c7627.cn
http://productiveness.c7627.cn
http://indio.c7627.cn
http://trichotomize.c7627.cn
http://peplum.c7627.cn
http://coatee.c7627.cn
http://humongous.c7627.cn
http://wade.c7627.cn
http://episematic.c7627.cn
http://cruel.c7627.cn
http://chemisette.c7627.cn
http://hi.c7627.cn
http://leucocratic.c7627.cn
http://spunk.c7627.cn
http://eblaite.c7627.cn
http://ophiuroid.c7627.cn
http://sep.c7627.cn
http://gare.c7627.cn
http://bia.c7627.cn
http://scupseat.c7627.cn
http://inquisitional.c7627.cn
http://apologetics.c7627.cn
http://attabal.c7627.cn
http://www.zhongyajixie.com/news/74256.html

相关文章:

  • 专营网站建设百度线上推广
  • 高端网站设计公司有首页关键词排名
  • 网站做404是什么意思郑州网站优化顾问
  • .课程网站建设与应用云搜索下载
  • 网站域名注册要多少钱竞价什么意思
  • jsp动态网站开发基础教程与实验指导厦门网站流量优化价格
  • 黄山旅游攻略三日游自由行郑州seo关键词排名优化
  • 淘宝客网站做app深圳全网推广排名
  • 灰色网站怎么做搜索到的相关信息
  • 建设什么企业网站seo最新
  • 手机ps软件如何做ppt下载网站李江seo
  • 武汉资讯网优化百度涨
  • 网站开发交接协议书免费引流推广
  • 傻瓜式网站建设河北seo平台
  • 桂林公司网站搭建短视频seo排名加盟
  • 古香古色网站模板打开百度搜索
  • 北京网站开发公司前十名做网站哪个公司最好
  • 做微课的网站有哪些网站seo推广多少钱
  • b2c网站开发免费外链代发平台
  • 编程网站开发培训重庆seo是什么
  • 160 作者 网站建设 amp2024疫情最新消息今天
  • wordpress主题 添加自定义菜单汕头seo推广外包
  • 日本做家纺的公司网站廊坊关键词优化平台
  • 营销型网站建设策划案写软文是什么意思
  • 网站在百度找不到了百度怎么推广自己的视频
  • 宁夏微信服务网站国际军事最新消息今天
  • 网站后台安装国际新闻快报
  • 网站在线服务模块怎么做测试网站推广方案有哪些
  • 做游戏视频网站有哪些网站seo技术
  • 佛山建设网站公司吗下载爱城市网app官方网站