@cloudea/yang-editor 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/common.d.ts +15 -0
- package/dist/common.d.ts.map +1 -0
- package/dist/common.js +10 -0
- package/dist/common.js.map +1 -0
- package/dist/componets.d.ts +103 -0
- package/dist/componets.d.ts.map +1 -0
- package/dist/componets.js +684 -0
- package/dist/componets.js.map +1 -0
- package/dist/context.d.ts +3 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +9 -0
- package/dist/context.js.map +1 -0
- package/dist/event.d.ts +13 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +42 -0
- package/dist/event.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +38 -0
- package/dist/utils.js.map +1 -0
- package/package.json +38 -0
- package/src/common.ts +24 -0
- package/src/componets.ts +795 -0
- package/src/context.ts +4 -0
- package/src/event.ts +52 -0
- package/src/index.ts +85 -0
- package/src/style.css +247 -0
- package/src/utils.ts +38 -0
- package/tsconfig.json +47 -0
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ComponentFactory = exports.EditorParagraph = exports.EditorParagraphMenu = exports.EditorCollapse = exports.EditorToolbar = exports.EditorContent = exports.EditorBody = void 0;
|
|
4
|
+
class EditorBody {
|
|
5
|
+
element;
|
|
6
|
+
context;
|
|
7
|
+
content;
|
|
8
|
+
menuButton;
|
|
9
|
+
constructor(editor) {
|
|
10
|
+
this.element = document.createElement("div");
|
|
11
|
+
this.context = editor;
|
|
12
|
+
this.content = new EditorContent(this.context);
|
|
13
|
+
this.menuButton = new ContentBeforeMenu(this.context);
|
|
14
|
+
}
|
|
15
|
+
onMount() {
|
|
16
|
+
this.element.classList.add("yang-editor-body");
|
|
17
|
+
this.element.appendChild(this.content.onMount());
|
|
18
|
+
this.element.appendChild(this.menuButton.onMount());
|
|
19
|
+
this.element.style.height = this.context.options.height;
|
|
20
|
+
return this.element;
|
|
21
|
+
}
|
|
22
|
+
onMounted() {
|
|
23
|
+
throw new Error("Method not implemented.");
|
|
24
|
+
}
|
|
25
|
+
getElement() {
|
|
26
|
+
return this.element;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.EditorBody = EditorBody;
|
|
30
|
+
class EditorContent {
|
|
31
|
+
element;
|
|
32
|
+
context;
|
|
33
|
+
constructor(editor) {
|
|
34
|
+
this.element = document.createElement("div");
|
|
35
|
+
this.context = editor;
|
|
36
|
+
}
|
|
37
|
+
onMount() {
|
|
38
|
+
this.element.classList.add("yang-editor-content");
|
|
39
|
+
if (this.context.options.mode === "edit") {
|
|
40
|
+
this.element.contentEditable = "true";
|
|
41
|
+
}
|
|
42
|
+
const observer = new MutationObserver((mutations) => {
|
|
43
|
+
if (this.context.options.events.onContentChange) {
|
|
44
|
+
this.context.options.events.onContentChange(this.element.innerHTML);
|
|
45
|
+
}
|
|
46
|
+
let childNodes = this.element.childNodes;
|
|
47
|
+
// transform text nodes or non-paragraph elements to paragraphs
|
|
48
|
+
for (let node of childNodes) {
|
|
49
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
50
|
+
let p = document.createElement("p");
|
|
51
|
+
p.innerText = node.textContent || "";
|
|
52
|
+
node.replaceWith(p);
|
|
53
|
+
}
|
|
54
|
+
else if (node.nodeType === Node.ELEMENT_NODE) {
|
|
55
|
+
let nodeElem = node;
|
|
56
|
+
let tag = nodeElem.tagName.toLowerCase();
|
|
57
|
+
if (tag !== 'p' && tag !== 'div') {
|
|
58
|
+
let p = document.createElement("p");
|
|
59
|
+
p.innerHTML = nodeElem.outerHTML;
|
|
60
|
+
node.replaceWith(p);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// insert empty paragraph
|
|
65
|
+
let lastElement = this.element.children[this.element.children.length - 1];
|
|
66
|
+
if (lastElement === undefined || lastElement.childNodes.length > 0) {
|
|
67
|
+
this.insertDefaultParagraph();
|
|
68
|
+
}
|
|
69
|
+
// tag every childnodes
|
|
70
|
+
for (let node of this.element.children) {
|
|
71
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
72
|
+
let elem = node;
|
|
73
|
+
if (!elem.classList.contains(ComponentFactory.PARAGRAPH_STYLE)) {
|
|
74
|
+
elem.classList.add(ComponentFactory.PARAGRAPH_STYLE);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// add mousemove and mouseleave event to every child nodes
|
|
79
|
+
for (let node of this.element.children) {
|
|
80
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
81
|
+
if (this.context.options.mode === "edit") {
|
|
82
|
+
let elem = node;
|
|
83
|
+
elem.onmousemove = () => {
|
|
84
|
+
this.context.body.menuButton.show(elem);
|
|
85
|
+
};
|
|
86
|
+
elem.onmouseleave = () => {
|
|
87
|
+
this.context.body.menuButton.hide();
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// activate all components
|
|
93
|
+
for (let node of this.element.children) {
|
|
94
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
95
|
+
for (let className of node.classList) {
|
|
96
|
+
if (ComponentFactory.STYLES.indexOf(className) !== -1) {
|
|
97
|
+
this.context.componentFactory.createComponent(className, node).onMount();
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
// 开始观察
|
|
105
|
+
observer.observe(this.element, {
|
|
106
|
+
childList: true, // 观察直接子节点的变动
|
|
107
|
+
subtree: true, // 观察所有后代节点的变动
|
|
108
|
+
characterData: true, // 观察节点内容或文本的变动
|
|
109
|
+
attributes: false, // 观察属性的变动
|
|
110
|
+
// attributeFilter: ['style', 'class'] // 只观察特定属性
|
|
111
|
+
});
|
|
112
|
+
this.element.oncopy = (e) => {
|
|
113
|
+
// TODO: handle copy event
|
|
114
|
+
// e.preventDefault();
|
|
115
|
+
// let selection = window.getSelection();
|
|
116
|
+
// if (selection) {
|
|
117
|
+
// let range = selection.getRangeAt(0);
|
|
118
|
+
// let cloned = range.cloneContents();
|
|
119
|
+
// // console.log(cloned);
|
|
120
|
+
// // console.log(e.clipboardData)
|
|
121
|
+
// // console.log(e.clipboardData?.getData("text/html"));
|
|
122
|
+
// // e.clipboardData?.setData("text/html", cloned.innerHTML);
|
|
123
|
+
// }
|
|
124
|
+
};
|
|
125
|
+
this.element.onpaste = (e) => {
|
|
126
|
+
// TODO: handle paste event
|
|
127
|
+
//e.preventDefault();
|
|
128
|
+
// let clipboardData = e.clipboardData;
|
|
129
|
+
// if (clipboardData) {
|
|
130
|
+
// let html = clipboardData.getData("text/html");
|
|
131
|
+
// let text = clipboardData.getData("text/plain");
|
|
132
|
+
// if (html) {
|
|
133
|
+
// let div = document.createElement("div");
|
|
134
|
+
// div.innerHTML = html;
|
|
135
|
+
// this.element.appendChild(div);
|
|
136
|
+
// } else if (text) {
|
|
137
|
+
// this.element.appendChild(document.createTextNode(text));
|
|
138
|
+
// }
|
|
139
|
+
// }
|
|
140
|
+
};
|
|
141
|
+
this.insertDefaultParagraph();
|
|
142
|
+
return this.element;
|
|
143
|
+
}
|
|
144
|
+
insertDefaultParagraph() {
|
|
145
|
+
this.element.appendChild(document.createElement('p'));
|
|
146
|
+
}
|
|
147
|
+
insertCollapse() {
|
|
148
|
+
let paragraphElement = this.context.selectionUtils.getSelectedParagraph();
|
|
149
|
+
if (paragraphElement?.innerText.trim() === "") {
|
|
150
|
+
paragraphElement.replaceWith(new EditorCollapse(this.context).onMount());
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
for (let child of this.element.children) {
|
|
154
|
+
if (child === paragraphElement) {
|
|
155
|
+
child.after(new EditorCollapse(this.context).onMount());
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
onMounted() {
|
|
162
|
+
throw new Error("Method not implemented.");
|
|
163
|
+
}
|
|
164
|
+
getElement() {
|
|
165
|
+
return this.element;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
exports.EditorContent = EditorContent;
|
|
169
|
+
class ContentBeforeMenu {
|
|
170
|
+
element;
|
|
171
|
+
context;
|
|
172
|
+
sideMenu;
|
|
173
|
+
target;
|
|
174
|
+
constructor(editor) {
|
|
175
|
+
this.context = editor;
|
|
176
|
+
this.element = document.createElement("div");
|
|
177
|
+
this.sideMenu = new EditorParagraphMenu(this.context);
|
|
178
|
+
}
|
|
179
|
+
onMount() {
|
|
180
|
+
this.element.classList.add("yang-editor-content-before-menu");
|
|
181
|
+
let button = document.createElement("button");
|
|
182
|
+
button.classList.add("yang-editor-content-before-menu-button");
|
|
183
|
+
button.style.backgroundImage = `url(${this.context.options.images.menu})`;
|
|
184
|
+
this.element.onclick = () => this.sideMenu.show(this.target);
|
|
185
|
+
this.element.onmouseenter = () => this.show(this.target);
|
|
186
|
+
this.element.appendChild(button);
|
|
187
|
+
this.element.appendChild(this.sideMenu.onMount());
|
|
188
|
+
return this.element;
|
|
189
|
+
}
|
|
190
|
+
onMounted() {
|
|
191
|
+
throw new Error("Method not implemented.");
|
|
192
|
+
}
|
|
193
|
+
getElement() {
|
|
194
|
+
return this.element;
|
|
195
|
+
}
|
|
196
|
+
show(target) {
|
|
197
|
+
this.element.style.display = "flex";
|
|
198
|
+
if (target !== this.target) {
|
|
199
|
+
this.sideMenu.hide();
|
|
200
|
+
}
|
|
201
|
+
if (target) {
|
|
202
|
+
this.target = target;
|
|
203
|
+
this.element.style.left = `24px`;
|
|
204
|
+
this.element.style.top = target.offsetTop + `px`;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
hide() {
|
|
208
|
+
this.element.style.display = "none";
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
class ButtonAdd {
|
|
212
|
+
element;
|
|
213
|
+
context;
|
|
214
|
+
constructor(editor) {
|
|
215
|
+
this.context = editor;
|
|
216
|
+
this.element = document.createElement("button");
|
|
217
|
+
this.element.innerText = "";
|
|
218
|
+
this.element.style.width = "16px";
|
|
219
|
+
this.element.style.height = "16px";
|
|
220
|
+
this.element.style.height = "16px";
|
|
221
|
+
this.element.style.backgroundImage = `url(${this.context.options.images.add})`;
|
|
222
|
+
this.element.style.backgroundSize = "contain";
|
|
223
|
+
this.element.style.backgroundRepeat = "no-repeat";
|
|
224
|
+
this.element.style.cursor = "pointer";
|
|
225
|
+
this.element.title = "Add Collapsed Panel";
|
|
226
|
+
this.element.onclick = () => this.context.body.content.insertCollapse();
|
|
227
|
+
}
|
|
228
|
+
onMount() {
|
|
229
|
+
return this.element;
|
|
230
|
+
}
|
|
231
|
+
onMounted() {
|
|
232
|
+
throw new Error("Method not implemented.");
|
|
233
|
+
}
|
|
234
|
+
getElement() {
|
|
235
|
+
return this.element;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
class BackGroundColorStrip {
|
|
239
|
+
element;
|
|
240
|
+
context;
|
|
241
|
+
colors;
|
|
242
|
+
titles;
|
|
243
|
+
constructor(editor) {
|
|
244
|
+
this.context = editor;
|
|
245
|
+
this.element = document.createElement("div");
|
|
246
|
+
this.colors = ["rgba(228, 73, 91, 1)", "rgba(255, 140, 0, 1)", "rgba(255, 215, 0, 1)", "rgba(34, 139, 34, 1)", "rgba(30, 144, 255, 1)", "rgba(138, 43, 226, 1)", "rgba(255, 20, 147, 1)"];
|
|
247
|
+
this.titles = ["red", "orange", "yellow", "green", "blue", "purple", "pink"];
|
|
248
|
+
}
|
|
249
|
+
onMount() {
|
|
250
|
+
this.element.classList.add("yang-editor-color-strip");
|
|
251
|
+
this.element.style.display = "flex";
|
|
252
|
+
this.element.style.alignItems = "center";
|
|
253
|
+
for (let i = 0; i < this.colors.length; i++) {
|
|
254
|
+
let color = this.colors[i];
|
|
255
|
+
let title = this.titles[i];
|
|
256
|
+
let btn = document.createElement("button");
|
|
257
|
+
if (color != undefined && title != undefined) {
|
|
258
|
+
btn.style.backgroundColor = color;
|
|
259
|
+
btn.style.width = "16px";
|
|
260
|
+
btn.style.height = "16px";
|
|
261
|
+
btn.style.borderRadius = "2px";
|
|
262
|
+
btn.style.cursor = "pointer";
|
|
263
|
+
btn.title = title;
|
|
264
|
+
btn.onclick = () => {
|
|
265
|
+
document.execCommand('backColor', false, color);
|
|
266
|
+
this.context.selectionUtils.getSelectionRange()?.collapse();
|
|
267
|
+
// eqauls to:
|
|
268
|
+
// document.execCommand('styleWithCSS', false, true);
|
|
269
|
+
// document.execCommand('foreColor', false, this.value);
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
this.element.appendChild(btn);
|
|
273
|
+
}
|
|
274
|
+
return this.element;
|
|
275
|
+
}
|
|
276
|
+
onMounted() {
|
|
277
|
+
throw new Error("Method not implemented.");
|
|
278
|
+
}
|
|
279
|
+
getElement() {
|
|
280
|
+
return this.element;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
class ForeGroundColorStrip {
|
|
284
|
+
element;
|
|
285
|
+
context;
|
|
286
|
+
colors;
|
|
287
|
+
titles;
|
|
288
|
+
constructor(editor) {
|
|
289
|
+
this.context = editor;
|
|
290
|
+
this.element = document.createElement("div");
|
|
291
|
+
this.colors = ["rgba(228, 73, 91, 1)", "rgba(255, 140, 0, 1)", "rgba(255, 215, 0, 1)", "rgba(34, 139, 34, 1)", "rgba(30, 144, 255, 1)", "rgba(138, 43, 226, 1)", "rgba(255, 20, 147, 1)"];
|
|
292
|
+
this.titles = ["red", "orange", "yellow", "green", "blue", "purple", "pink"];
|
|
293
|
+
}
|
|
294
|
+
onMount() {
|
|
295
|
+
this.element.classList.add("yang-editor-fgcolor-strip");
|
|
296
|
+
this.element.style.display = "flex";
|
|
297
|
+
this.element.style.alignItems = "center";
|
|
298
|
+
for (let i = 0; i < this.colors.length; i++) {
|
|
299
|
+
let color = this.colors[i];
|
|
300
|
+
let title = this.titles[i];
|
|
301
|
+
let btn = document.createElement("button");
|
|
302
|
+
if (color != undefined && title != undefined) {
|
|
303
|
+
// btn.style.backgroundColor = "transparent";
|
|
304
|
+
// btn.style.backgroundImage = `url(${this.context.options.images.fgColor})`;
|
|
305
|
+
// btn.style.backgroundSize = "16px 16px";
|
|
306
|
+
// btn.style.backgroundRepeat = "no-repeat";
|
|
307
|
+
// btn.style.backgroundPosition = "center";
|
|
308
|
+
btn.style.mask = `url(${this.context.options.images.fgColor}) no-repeat center`;
|
|
309
|
+
btn.style.webkitMask = `url(${this.context.options.images.fgColor}) no-repeat center`;
|
|
310
|
+
btn.style.backgroundColor = color;
|
|
311
|
+
btn.style.width = "17px";
|
|
312
|
+
btn.style.height = "17px";
|
|
313
|
+
btn.style.borderRadius = "2px";
|
|
314
|
+
btn.style.cursor = "pointer";
|
|
315
|
+
btn.title = title;
|
|
316
|
+
btn.onclick = () => {
|
|
317
|
+
document.execCommand('foreColor', false, color);
|
|
318
|
+
this.context.selectionUtils.getSelectionRange()?.collapse();
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
this.element.appendChild(btn);
|
|
322
|
+
}
|
|
323
|
+
return this.element;
|
|
324
|
+
}
|
|
325
|
+
onMounted() {
|
|
326
|
+
throw new Error("Method not implemented.");
|
|
327
|
+
}
|
|
328
|
+
getElement() {
|
|
329
|
+
return this.element;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
class IconButton {
|
|
333
|
+
element;
|
|
334
|
+
context;
|
|
335
|
+
constructor(editor, icon, tooltip, onclick) {
|
|
336
|
+
this.context = editor;
|
|
337
|
+
this.element = document.createElement("button");
|
|
338
|
+
this.element.classList.add("yang-editor-icon-button");
|
|
339
|
+
this.element.style.backgroundImage = `url(${icon})`;
|
|
340
|
+
this.element.style.backgroundColor = "transparent";
|
|
341
|
+
this.element.style.width = "26px";
|
|
342
|
+
this.element.style.height = "26px";
|
|
343
|
+
this.element.style.borderRadius = "3px";
|
|
344
|
+
this.element.style.cursor = "pointer";
|
|
345
|
+
this.element.style.backgroundSize = "16px 16px";
|
|
346
|
+
this.element.style.backgroundRepeat = "no-repeat";
|
|
347
|
+
this.element.style.backgroundPosition = "center center";
|
|
348
|
+
this.element.title = tooltip;
|
|
349
|
+
this.element.onclick = () => {
|
|
350
|
+
if (onclick) {
|
|
351
|
+
onclick();
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
this.element.onmouseover = () => {
|
|
355
|
+
this.element.style.backgroundColor = "rgba(231, 233, 232, 1)";
|
|
356
|
+
};
|
|
357
|
+
this.element.onmouseleave = () => {
|
|
358
|
+
this.element.style.backgroundColor = "transparent";
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
onMount() {
|
|
362
|
+
return this.element;
|
|
363
|
+
}
|
|
364
|
+
onMounted() {
|
|
365
|
+
throw new Error("Method not implemented.");
|
|
366
|
+
}
|
|
367
|
+
getElement() {
|
|
368
|
+
return this.element;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
class EditorToolbar {
|
|
372
|
+
element;
|
|
373
|
+
context;
|
|
374
|
+
constructor(editor) {
|
|
375
|
+
this.context = editor;
|
|
376
|
+
this.element = document.createElement("div");
|
|
377
|
+
}
|
|
378
|
+
onMount() {
|
|
379
|
+
this.element.classList.add("yang-editor-toolbar");
|
|
380
|
+
this.element.appendChild(new ButtonAdd(this.context).onMount());
|
|
381
|
+
this.element.appendChild(new BackGroundColorStrip(this.context).onMount());
|
|
382
|
+
this.element.appendChild(new ForeGroundColorStrip(this.context).onMount());
|
|
383
|
+
this.element.appendChild(new IconButton(this.context, this.context.options.images.bold, "Bold", this.boldSelection.bind(this)).onMount());
|
|
384
|
+
this.element.appendChild(new IconButton(this.context, this.context.options.images.italic, "Italic", this.italicSelection.bind(this)).onMount());
|
|
385
|
+
this.element.appendChild(new IconButton(this.context, this.context.options.images.underline, "Underline", this.underlineSelection.bind(this)).onMount());
|
|
386
|
+
this.element.appendChild(new IconButton(this.context, this.context.options.images.deleteline, "Delete Line", this.strokeThroughSelection.bind(this)).onMount());
|
|
387
|
+
this.element.appendChild(new IconButton(this.context, this.context.options.images.link, "Link", this.insertLink.bind(this)).onMount());
|
|
388
|
+
this.element.appendChild(new IconButton(this.context, this.context.options.images.clear, "Clear", this.clearFormatSelection.bind(this)).onMount());
|
|
389
|
+
return this.element;
|
|
390
|
+
}
|
|
391
|
+
onMounted() {
|
|
392
|
+
throw new Error("Method not implemented.");
|
|
393
|
+
}
|
|
394
|
+
getElement() {
|
|
395
|
+
return this.element;
|
|
396
|
+
}
|
|
397
|
+
insertLink() {
|
|
398
|
+
let range = this.context.selectionUtils.getSelectionRange();
|
|
399
|
+
if (range == null || range.collapsed) {
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
const url = prompt("请输入链接地址");
|
|
403
|
+
if (url) {
|
|
404
|
+
const selectedContent = range.extractContents();
|
|
405
|
+
const linkElement = document.createElement('a');
|
|
406
|
+
linkElement.href = url;
|
|
407
|
+
linkElement.appendChild(selectedContent);
|
|
408
|
+
range.insertNode(linkElement);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
boldSelection() {
|
|
412
|
+
// let range = this.context.selectionUtils.getSelectionRange();
|
|
413
|
+
// if (range == null || range.collapsed) {return;}
|
|
414
|
+
// // 检查是否已经在加粗标签内
|
|
415
|
+
// let parentElement = range.commonAncestorContainer;
|
|
416
|
+
// while (parentElement && parentElement.nodeType !== Node.ELEMENT_NODE && parentElement.parentElement !== null) {
|
|
417
|
+
// parentElement = parentElement.parentElement;
|
|
418
|
+
// }
|
|
419
|
+
// let tagName = parentElement.nodeName.toUpperCase();
|
|
420
|
+
// if (parentElement && (tagName === 'STRONG' || tagName === 'B')) {
|
|
421
|
+
// // 如果已经在加粗标签内,则取消加粗
|
|
422
|
+
// if(parentElement.textContent != null && parentElement.parentNode !== null) {
|
|
423
|
+
// const textNode = document.createTextNode(parentElement.textContent);
|
|
424
|
+
// parentElement.parentNode.replaceChild(textNode, parentElement);
|
|
425
|
+
// }
|
|
426
|
+
// } else {
|
|
427
|
+
// // 执行加粗
|
|
428
|
+
// //document.execCommand('bold', false);
|
|
429
|
+
// const selectedContent = range.extractContents();
|
|
430
|
+
// // 创建加粗元素并包裹选中内容
|
|
431
|
+
// const boldElement = document.createElement('strong');
|
|
432
|
+
// boldElement.appendChild(selectedContent);
|
|
433
|
+
// // 将加粗的内容插入回文档
|
|
434
|
+
// range.insertNode(boldElement);
|
|
435
|
+
// }
|
|
436
|
+
document.execCommand('bold', false, undefined);
|
|
437
|
+
}
|
|
438
|
+
italicSelection() {
|
|
439
|
+
document.execCommand('italic', false, undefined);
|
|
440
|
+
}
|
|
441
|
+
underlineSelection() {
|
|
442
|
+
document.execCommand('underline', false, undefined);
|
|
443
|
+
}
|
|
444
|
+
strokeThroughSelection() {
|
|
445
|
+
document.execCommand('strikeThrough', false, undefined);
|
|
446
|
+
}
|
|
447
|
+
clearFormatSelection() {
|
|
448
|
+
document.execCommand('removeFormat', false, undefined);
|
|
449
|
+
document.execCommand('unlink', false, undefined);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
exports.EditorToolbar = EditorToolbar;
|
|
453
|
+
class EditorCollapse {
|
|
454
|
+
static COLLAPSE_STYLE = "collapsed";
|
|
455
|
+
element;
|
|
456
|
+
context;
|
|
457
|
+
title;
|
|
458
|
+
header;
|
|
459
|
+
button;
|
|
460
|
+
body;
|
|
461
|
+
container;
|
|
462
|
+
constructor(editor, element) {
|
|
463
|
+
this.context = editor;
|
|
464
|
+
if (element) {
|
|
465
|
+
this.element = element;
|
|
466
|
+
this.container = element.querySelector('.yang-editor-collapse-container');
|
|
467
|
+
this.header = element.querySelector('.yang-editor-collapse-header');
|
|
468
|
+
this.body = element.querySelector('.yang-editor-collapse-body');
|
|
469
|
+
this.button = element.querySelector('.yang-editor-collapse-button');
|
|
470
|
+
this.title = element.querySelector('.yang-editor-collapse-title');
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
this.element = document.createElement("div");
|
|
474
|
+
this.container = document.createElement('div');
|
|
475
|
+
this.header = document.createElement('div');
|
|
476
|
+
this.body = document.createElement('div');
|
|
477
|
+
this.button = document.createElement('button');
|
|
478
|
+
this.title = document.createElement('div');
|
|
479
|
+
this.element.classList.add(ComponentFactory.COLLAPSE_STYLE);
|
|
480
|
+
this.container.classList.add("yang-editor-collapse-container");
|
|
481
|
+
this.header.classList.add("yang-editor-collapse-header");
|
|
482
|
+
this.body.classList.add("yang-editor-collapse-body");
|
|
483
|
+
this.button.classList.add("yang-editor-collapse-button");
|
|
484
|
+
this.title.classList.add("yang-editor-collapse-title");
|
|
485
|
+
this.title.innerText = "折叠面板标题";
|
|
486
|
+
this.button.innerText = "";
|
|
487
|
+
this.header.appendChild(this.button);
|
|
488
|
+
this.header.appendChild(this.title);
|
|
489
|
+
this.container.appendChild(this.header);
|
|
490
|
+
this.container.appendChild(this.body);
|
|
491
|
+
this.element.appendChild(this.container);
|
|
492
|
+
this.container.contentEditable = "false";
|
|
493
|
+
this.button.style.backgroundImage = `url(${this.context.options.images.down})`;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
getElement() {
|
|
497
|
+
return this.element;
|
|
498
|
+
}
|
|
499
|
+
onMount() {
|
|
500
|
+
if (this.context.options.mode === "edit") {
|
|
501
|
+
this.title.contentEditable = "true";
|
|
502
|
+
this.body.contentEditable = "true";
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
this.title.contentEditable = "false";
|
|
506
|
+
this.body.contentEditable = "false";
|
|
507
|
+
}
|
|
508
|
+
this.button.onclick = () => {
|
|
509
|
+
this.switchState();
|
|
510
|
+
this.renderState();
|
|
511
|
+
};
|
|
512
|
+
this.renderState();
|
|
513
|
+
return this.getElement();
|
|
514
|
+
}
|
|
515
|
+
switchState() {
|
|
516
|
+
if (this.element.classList.contains(EditorCollapse.COLLAPSE_STYLE)) {
|
|
517
|
+
this.element.classList.remove(EditorCollapse.COLLAPSE_STYLE);
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
this.element.classList.add(EditorCollapse.COLLAPSE_STYLE);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
isCollapsed() {
|
|
524
|
+
return this.element.classList.contains(EditorCollapse.COLLAPSE_STYLE);
|
|
525
|
+
}
|
|
526
|
+
renderState() {
|
|
527
|
+
if (this.isCollapsed()) {
|
|
528
|
+
this.button.classList.add(EditorCollapse.COLLAPSE_STYLE);
|
|
529
|
+
this.body.classList.add(EditorCollapse.COLLAPSE_STYLE);
|
|
530
|
+
this.header.classList.add(EditorCollapse.COLLAPSE_STYLE);
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
this.button.classList.remove(EditorCollapse.COLLAPSE_STYLE);
|
|
534
|
+
this.body.classList.remove(EditorCollapse.COLLAPSE_STYLE);
|
|
535
|
+
this.header.classList.remove(EditorCollapse.COLLAPSE_STYLE);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
onMounted() {
|
|
539
|
+
throw new Error("Method not implemented.");
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
exports.EditorCollapse = EditorCollapse;
|
|
543
|
+
class EditorParagraphMenu {
|
|
544
|
+
element;
|
|
545
|
+
context;
|
|
546
|
+
target;
|
|
547
|
+
constructor(editor) {
|
|
548
|
+
this.context = editor;
|
|
549
|
+
this.element = document.createElement("div");
|
|
550
|
+
}
|
|
551
|
+
onMounted() {
|
|
552
|
+
throw new Error("Method not implemented.");
|
|
553
|
+
}
|
|
554
|
+
getElement() {
|
|
555
|
+
return this.element;
|
|
556
|
+
}
|
|
557
|
+
onMount() {
|
|
558
|
+
this.element.classList.add("yang-editor-paragraph-menu");
|
|
559
|
+
let ul = document.createElement("ul");
|
|
560
|
+
let lis = new Array();
|
|
561
|
+
let images = [this.context.options.images.delete, this.context.options.images.copy, this.context.options.images.cut];
|
|
562
|
+
let texts = ["Delete", "Copy", "Cut"];
|
|
563
|
+
for (let i = 0; i < images.length; i++) {
|
|
564
|
+
let li = document.createElement("li");
|
|
565
|
+
let icon = document.createElement("button");
|
|
566
|
+
let text = document.createElement("p");
|
|
567
|
+
li.classList.add("yang-editor-paragraph-menu-item");
|
|
568
|
+
icon.classList.add("yang-editor-paragraph-menu-item-icon");
|
|
569
|
+
text.classList.add("yang-editor-paragraph-menu-item-text");
|
|
570
|
+
let title = texts[i];
|
|
571
|
+
if (title !== undefined) {
|
|
572
|
+
icon.style.backgroundImage = `url(${images[i]})`;
|
|
573
|
+
icon.style.backgroundSize = "16px 16px";
|
|
574
|
+
icon.style.backgroundRepeat = "no-repeat";
|
|
575
|
+
icon.style.backgroundPosition = "center";
|
|
576
|
+
icon.style.width = "20px";
|
|
577
|
+
icon.style.height = "20px";
|
|
578
|
+
icon.style.cursor = "pointer";
|
|
579
|
+
icon.style.border = "none";
|
|
580
|
+
icon.style.outline = "none";
|
|
581
|
+
li.style.display = "flex";
|
|
582
|
+
li.style.alignItems = "center";
|
|
583
|
+
text.innerText = title;
|
|
584
|
+
li.appendChild(icon);
|
|
585
|
+
li.appendChild(text);
|
|
586
|
+
}
|
|
587
|
+
ul.appendChild(li);
|
|
588
|
+
lis.push(li);
|
|
589
|
+
}
|
|
590
|
+
if (lis[0]) {
|
|
591
|
+
lis[0].onclick = this.delete.bind(this);
|
|
592
|
+
}
|
|
593
|
+
if (lis[1]) {
|
|
594
|
+
lis[1].onclick = this.copy.bind(this);
|
|
595
|
+
}
|
|
596
|
+
if (lis[2]) {
|
|
597
|
+
lis[2].onclick = this.cut.bind(this);
|
|
598
|
+
}
|
|
599
|
+
this.element.onmouseleave = this.hide.bind(this);
|
|
600
|
+
this.element.appendChild(ul);
|
|
601
|
+
return this.getElement();
|
|
602
|
+
}
|
|
603
|
+
hide() {
|
|
604
|
+
this.element.style.display = "none";
|
|
605
|
+
}
|
|
606
|
+
show(target) {
|
|
607
|
+
this.element.style.display = "block";
|
|
608
|
+
if (target) {
|
|
609
|
+
this.target = target;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
delete(ev) {
|
|
613
|
+
if (this.target !== undefined) {
|
|
614
|
+
this.target.remove();
|
|
615
|
+
this.hide();
|
|
616
|
+
ev.stopPropagation();
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
copy(ev) {
|
|
620
|
+
if (this.target !== undefined) {
|
|
621
|
+
navigator.clipboard.write([new ClipboardItem({
|
|
622
|
+
"text/html": this.target.outerHTML
|
|
623
|
+
})]).then(() => {
|
|
624
|
+
this.hide();
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
cut(ev) {
|
|
629
|
+
if (this.target !== undefined) {
|
|
630
|
+
navigator.clipboard.write([new ClipboardItem({
|
|
631
|
+
"text/html": this.target.outerHTML
|
|
632
|
+
})]).then(() => {
|
|
633
|
+
this.target?.remove();
|
|
634
|
+
this.hide();
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
exports.EditorParagraphMenu = EditorParagraphMenu;
|
|
640
|
+
class EditorParagraph {
|
|
641
|
+
context;
|
|
642
|
+
element;
|
|
643
|
+
constructor(context, element) {
|
|
644
|
+
this.context = context;
|
|
645
|
+
if (element !== undefined) {
|
|
646
|
+
this.element = element;
|
|
647
|
+
}
|
|
648
|
+
else {
|
|
649
|
+
this.element = document.createElement("p");
|
|
650
|
+
this.element.classList.add(ComponentFactory.PARAGRAPH_STYLE);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
onMounted() {
|
|
654
|
+
throw new Error("Method not implemented.");
|
|
655
|
+
}
|
|
656
|
+
onMount() {
|
|
657
|
+
return this.element;
|
|
658
|
+
}
|
|
659
|
+
getElement() {
|
|
660
|
+
return this.element;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
exports.EditorParagraph = EditorParagraph;
|
|
664
|
+
class ComponentFactory {
|
|
665
|
+
static PARAGRAPH_STYLE = "yang-editor-paragraph";
|
|
666
|
+
static COLLAPSE_STYLE = "yang-editor-collapse";
|
|
667
|
+
static STYLES = [this.PARAGRAPH_STYLE, this.COLLAPSE_STYLE];
|
|
668
|
+
context;
|
|
669
|
+
constructor(context) {
|
|
670
|
+
this.context = context;
|
|
671
|
+
}
|
|
672
|
+
createComponent(typeClassName, element) {
|
|
673
|
+
switch (typeClassName) {
|
|
674
|
+
case ComponentFactory.PARAGRAPH_STYLE:
|
|
675
|
+
return new EditorParagraph(this.context, element);
|
|
676
|
+
case ComponentFactory.COLLAPSE_STYLE:
|
|
677
|
+
return new EditorCollapse(this.context, element);
|
|
678
|
+
default:
|
|
679
|
+
throw new Error(`Unknown component type: ${typeClassName}`);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
exports.ComponentFactory = ComponentFactory;
|
|
684
|
+
//# sourceMappingURL=componets.js.map
|