HTML 注释(选择、突出显示、删除格式)
我正在开发一个基于跨浏览器网络的注释工具集,它允许用户选择网页的任何部分
- HIGHLIGHT,如果您选择:
约翰是
大 转储
结果
约翰是
大
dump
- 删除格式:如果您选择:
约翰
来自
约翰是
大
dump
结果
;约翰 是
大
dump
- 撤消/重做:很高兴有功能
为了能够撤消和重做所执行的操作,
我有一个用于突出显示的部分解决方案
function highlight(colour) {
var range, sel;
if (document.selection && (!window.getSelection)) {
// IE case
range = document.selection.createRange();
range.execCommand("BackColor", false, colour);
} else if (window.getSelection) {
// Non-IE case
sel = window.getSelection();
if (sel.getRangeAt) {
range = sel.getRangeAt(0);
}
//without designmode=on, you can't highlight the selected html (chrome)
document.designMode = "on";
if (range) {
sel.removeAllRanges();
sel.addRange(range);
}
// HiliteColor avoids FF3.5 from painting the background of the whole block
if (!document.execCommand("HiliteColor", false, colour) ) {
document.execCommand("BackColor", false, colour);
}
document.designMode = "off";
}
}
由于我的要求与富文本编辑器有很多相似之处,所以我研究了 ckeditor 和(广泛)谷歌闭包编辑器的代码。我对它们都放弃了希望,因为它们只能在可编辑的 iframe 中工作。我的要求之一是不允许用户更改原始文本,而只能添加自己的注释(突出显示、内联注释等)。
我很乐意在这里提出您的所有意见,如果可能的话,请为我指明正确的方向。
——初尚
I am working on a cross browser web based annotation toolset, which allows the users to select any part of a web page
- HIGHLIGHT, if you select:
john is <li>big</li> <li>dump</li>
Result
<span style="background-color: rgb(106, 168, 79)">john is</span>
<li><span style="background-color: rgb(106, 168, 79)">big</span></li> <li><span style="background-color: rgb(106, 168, 79)">dump</span></li>
- REMOVE FORMAT: if you select:
john
from
<span style="background-color: rgb(106, 168, 79)">john is</span>
<li><span style="background-color: rgb(106, 168, 79)">big</span></li> <li><span style="background-color: rgb(106, 168, 79)">dump</span></li>
Result
<span style="background-color: rgb(106, 168, 79)"></span> john <span style="background-color: rgb(106, 168, 79)">is</span>
<li><span style="background-color: rgb(106, 168, 79)">big</span></li> <li><span style="background-color: rgb(106, 168, 79)">dump</span></li>
- UNDO/REDO: nice to have feature
To be able to undo and redo the actions performed
I have a partial solution for highlighting
function highlight(colour) {
var range, sel;
if (document.selection && (!window.getSelection)) {
// IE case
range = document.selection.createRange();
range.execCommand("BackColor", false, colour);
} else if (window.getSelection) {
// Non-IE case
sel = window.getSelection();
if (sel.getRangeAt) {
range = sel.getRangeAt(0);
}
//without designmode=on, you can't highlight the selected html (chrome)
document.designMode = "on";
if (range) {
sel.removeAllRanges();
sel.addRange(range);
}
// HiliteColor avoids FF3.5 from painting the background of the whole block
if (!document.execCommand("HiliteColor", false, colour) ) {
document.execCommand("BackColor", false, colour);
}
document.designMode = "off";
}
}
Since my requirements had much similarity with a richtext editor, I looked into the codes of ckeditor and (extensively) in google closures editor. I have given up hope in both of them because, they work in only an editable iframe. One of my requirement is that users are not allowed to change the original text but only to add there own annotations(highlight, inline notes,etc).
I would love to here all of your opinions and if possible to point me to the right direction.
--Choesang
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你仍然可以使用“富文本编辑器”方式(iframe),但然后尝试捕获“onkeypress”、“onkeydown”和其他交互事件来停止默认行为(编辑文档)。
I think you can still use "rich-text-editor" way (iframe), but then try to catch "onkeypress","onkeydown" and other interactive events to stop default behavior (editing the document).
这是一个很好的资源,可以帮助我完成类似的事情: http://www.quirksmode.org/dom /execCommand.html
从第一页链接作为示例:
http://www. quirksmode.org/dom/execCommand/
Here's a good resource that helped me with something similiar: http://www.quirksmode.org/dom/execCommand.html
linked from the first page as an example:
http://www.quirksmode.org/dom/execCommand/
您可以在论坛中找到解决方案: http://cksource.com /forums/viewtopic.php?f=11&t=15659
为了清楚起见,我插入下面的代码:
并在您的 javascript 中,按如下方式调用
上面基本上提供了一个区域(iframe),即可编辑,同时只读(不能从键盘输入)。它完全满足了我想要的所有功能。我不必关心如何实现:突出显示、删除格式、撤消和重做。一切都由 ckeditor 处理:)
You will find the solution in the forum: http://cksource.com/forums/viewtopic.php?f=11&t=15659
For the sake of clarity, i am inserting the code below:
and in your javascript, call as following
The above basically provides an area (iframe) that is editable and it the same time read-only (you can not input from keyboard). It completely fulfills all my desired functionalities. I do not have to care about how to implement: highlighting, remove format, undo and redo. Everything is taken cared of ckeditor :)