/
comment-ot.html
192 lines (191 loc) · 7.01 KB
/
comment-ot.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>comment-ot</title>
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet" />
<style>
body,
html {
background-color: #eee;
font-size: 14px;
}
.editor-container {
margin: 0 auto;
width: 700px;
background-color: #fff;
}
.editor-container > .ql-toolbar {
border: unset;
border-bottom: 1px solid #eee;
}
#editor {
background-color: #fff;
border: unset;
}
.apply-comment {
width: auto !important;
}
</style>
</head>
<body>
<div id="root">
<div class="editor-container">
<div id="toolbar" style="display: none">
<select class="ql-header"></select>
<button class="ql-bold"></button>
<button class="ql-italic"></button>
<button class="ql-underline"></button>
<button class="apply-comment">评论</button>
</div>
<div id="editor"></div>
</div>
</div>
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
<script>
const dom = document.getElementById("editor");
const toolbar = document.getElementById("toolbar");
const editor = new Quill(dom, {
modules: {
toolbar: toolbar,
},
placeholder: "Enter Text...",
theme: "snow",
});
toolbar.style.display = "block";
window.editor = editor;
editor.setContents([
{ insert: "为了演示实际效果,此处的评论是使用虚拟图层的方式实现的。" },
{ insert: "\n" },
]);
</script>
<script>
const commentRangeDOM = document.createElement("div");
commentRangeDOM.className = "ql-range ignore-dom layer-comment";
editor.container.appendChild(commentRangeDOM);
const buildLayerDOM = ranges => {
commentRangeDOM.innerText = "";
const color = "rgb(255, 125, 0, 0.3)";
ranges.forEach(range => {
const startRect = editor.getBounds(range.index, 0);
const endRect = editor.getBounds(range.index + range.length, 0);
const block = document.createElement("div");
block.style.position = "absolute";
block.style.width = "100%";
block.style.height = "0";
block.style.top = startRect.top + "px";
block.style.pointerEvents = "none";
const head = document.createElement("div");
const body = document.createElement("div");
const tail = document.createElement("div");
if (startRect.top === endRect.top) {
head.style.marginLeft = startRect.left + "px";
head.style.height = startRect.height + "px";
head.style.width = endRect.right - startRect.left + "px";
head.style.backgroundColor = color;
} else if (endRect.top - startRect.bottom < startRect.height) {
head.style.marginLeft = startRect.left + "px";
head.style.height = startRect.height + "px";
head.style.width = startRect.width - startRect.left + "px";
head.style.backgroundColor = color;
body.style.height = endRect.top - startRect.bottom + "px";
tail.style.width = endRect.right + "px";
tail.style.height = endRect.height + "px";
tail.style.backgroundColor = color;
} else {
head.style.marginLeft = startRect.left + "px";
head.style.height = startRect.height + "px";
head.style.width = startRect.width - startRect.left + "px";
head.style.backgroundColor = color;
body.style.width = "100%";
body.style.height = endRect.top - startRect.bottom + "px";
body.style.backgroundColor = color;
tail.style.marginLeft = 0;
tail.style.height = endRect.height + "px";
tail.style.width = endRect.right + "px";
tail.style.backgroundColor = color;
}
block.appendChild(head);
block.appendChild(body);
block.appendChild(tail);
commentRangeDOM.appendChild(block);
});
};
editor.renderLayer = (origin, ignoreLineMarker = true) => {
/** @type {{start: number, length: number}} */
const lines = [];
let currentIndex = 0;
editor.scroll.lines().forEach(line => {
const length = line.length();
lines.push({ start: currentIndex, length });
currentIndex = currentIndex + length;
});
const ranges = [];
for (const item of origin) {
const { index, length } = item;
let traceLength = length;
for (const line of lines) {
if (length === 1 && index + length === line.start + line.length) {
if (ignoreLineMarker) break;
const payload = { index: line.start, length: line.length - 1 };
!ignoreLineMarker && payload.length > 0 && ranges.push(payload);
break;
}
if (index < line.start + line.length && line.start <= index + traceLength) {
const nextIndex = Math.max(line.start, index);
const nextLength = Math.min(
traceLength,
line.length - 1,
line.start + line.length - nextIndex
);
traceLength = traceLength - nextLength;
const payload = { index: nextIndex, length: nextLength };
if (nextIndex + nextLength === line.start + line.length) {
payload.length--;
}
payload.length > 0 && ranges.push(payload);
} else if (line.start > index + length || traceLength <= 0) {
break;
}
}
}
buildLayerDOM(ranges);
return ranges;
};
</script>
<script>
const COMMENT_LIST = [
{ index: 12, length: 2 },
{ index: 17, length: 4 },
];
editor.renderLayer(COMMENT_LIST);
const applyComment = document.querySelector(".apply-comment");
applyComment.onmousedown = e => {
e.stopPropagation();
e.preventDefault();
};
applyComment.onclick = e => {
const selection = editor.getSelection();
if (selection) {
const sel = { ...selection };
console.log("添加评论:", sel);
COMMENT_LIST.push(sel);
editor.renderLayer(COMMENT_LIST);
editor.setSelection(sel.index + sel.length);
}
};
const Delta = Quill.import("delta");
editor.on("text-change", delta => {
for(const item of COMMENT_LIST){
const { index, length } = item;
const start = delta.transformPosition(index);
const end = delta.transformPosition(index + length, true);
item.index = start;
item.length = end - start;
}
editor.renderLayer(COMMENT_LIST);
});
</script>
</body>
</html>