Codemirror 2.2 清除所有标记
我正在寻找清除代码镜像文本的所有标记。标记已创建,调用
ret = codemirror.markText(..);
histMarks.push(ret);
要删除所有标记,我们将单独清除每个标记:
foreach( histMarks, i.clear() ); // this is pseudocode
是否有更有效的方法来删除所有标记?
I'm looking to clear all marks to a codemirror text. The marks have been created calling
ret = codemirror.markText(..);
histMarks.push(ret);
To delete all marks we're clearing each one individually:
foreach( histMarks, i.clear() ); // this is pseudocode
Is there a more efficient way to delete all marks ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实正在寻找“clearAllMarks”类型的功能,那么有一种稍微更有效的方法。您甚至被迫自己捕获所有标记并将其存储在数组中的全部原因是因为它们没有存储在 codemirror 实例中的任何位置。这表明他们在 .clear 上所做的任何事情都是独立的(也就是说,每个 TextMarker 都拥有存储在其自身上进行清除所需的所有信息。)。确定后,我们可以看一下 markText() 和 .clear 函数:
我没有包含所有代码,因此请自行仔细阅读(codemirror.js),
但你应该注意到的是,所有方法真正做的就是确保如果你调用clear,它会从正确的位置删除标记,因为没有理由你不能将相同的css类添加到不同的位置使用不同标记的行...并且清除一个标记不应同时清除两者。该方法还更新更改数组,该数组只是更改了哪些行的记录。
由于我们正在执行“全部清除”操作,因此我们并不真正关心删除某些内容而不是其他内容,因此不需要计算标记影响范围内的内容。另外,因为 markText 没有副作用,所以没有其他需要重置的内容,所以本质上您所做的就是删除 css 类。
因此,为了稍微更快地清除,您仍然必须存储标记或使用标记应用的类,以便您可以依次执行以下操作之一:
如果您仍然存储标记:
如果您只存储类:
基准测试:虽然我很清楚有效的基准测试通常是一项棘手的任务,但我认为至少对各种方法进行一些测试是值得的。
我设置了一个测试,其中添加了一定数量的标记,每个标记应用不同的类(按索引递增)。然后,我运行并计时了 3 种不同的方法来单独删除这些标记。我以不同的顺序多次运行每个测试,以确保结果一致。
我比较了 3 种方法:
style
对于 100 个标记,删除结果如下:
对于 1000 个标记:
There is a slightly more efficient way if you are truly looking for a "clearAllMarks" sort of functionality. The whole reason you are are even forced to capture and store all the marks yourself in the array is because they are not stored anywhere within a codemirror instance. This indicates that whatever they are doing on .clear is self-contained (that is to say that each TextMarker has all the information it needs to do the clearing stored on itself.). With that determined we can take a look at the markText() and the .clear function:
I haven't included all the code so feel free to peruse it yourself (codemirror.js),
but what you should notice is that all the method is really doing is the work to ensure that if you are calling clear it removes the markers from the correct places as there is no reason why you couldn't have added the same css class to different lines using different markers...and clearing one shouldn't clear both. The method also updates the changes array which is just a record of what lines were changed.
Since we are doing a "clear all" we don't really care about removing some and not others, so the work of figuring out what spans the marker affects isn't necessary. Also, because there are no side affects of markText there is nothing else to reset, so at its heart all you are doing is removing css classes.
Thus, for a slightly faster clear you would still have to either store the markers or the classes you applied using markers so you could in turn do one of the following:
If you still store the markers:
If you just store the classes:
Benchmarking: While I am well aware that effective benchmarking can often be a tricky task, I figured it would be worthwhile to at least run few tests of various methodologies.
I setup a test where I added a certain number of markers each applying a different class (incremented by index). I then ran and timed 3 different methods for removing those markers each in isolation. I ran each test a number of times in varied order to ensure consistent results.
I compared 3 methods:
style
For 100 markers to remove the results were as follows:
For 1000 markers:
最干净的解决方案是提取所有标记,然后清除它们
,或者在上面的情况下
The cleanest solution would be to extract all the marks, and then clear them
or in the case above