CodeMirror - addWidget是什么以及如何使用它?

Geo*_*uer 7 widget codemirror

我想对CodeMirror进行一些扩展.addWidget方法似乎是一个很有前途的起点.文件说明

addWidget(pos,node,scrollIntoView)将节点(应该是绝对定位的DOM节点)放入编辑器中,位于给定{line,ch}位置的正下方.当scrollIntoView为true时,编辑器将确保整个节点可见(如果可能).要再次删除窗口小部件,只需使用DOM方法(将其移动到其他位置,或在其父窗口上调用removeChild).

我真的不明白这意味着什么,或者我会用它做什么.我无法在CodeMirror代码库中找到它的用法,也无法在谷歌的任何其他地方找到它的用法.

alj*_*n82 5

您需要传递一个 html 节点和一个位置以及一个布尔值

// create a node
var htmlNode =document.createElement("h1");
var text =  document.createTextNode("Text or whatever");
htmlNode.appendChild(text)

// call this after you initialized the editor.
// the position must be like this {ch: YourCharecterNumber, line: YourLineNumber}
editor.addWidget({ch:30 , line: 1},htmlNode, true)
Run Code Online (Sandbox Code Playgroud)