@chenyomi/leafer-htmltext-editor 1.0.0 → 1.0.2

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/dist/esm/utils.js CHANGED
@@ -1,8 +1,4 @@
1
- import { TextEditor } from './TextEditor';
2
- function getEditorContext() {
3
- const quill = TextEditor.quill;
4
- return { quill };
5
- }
1
+ import { quillManager } from '.';
6
2
  export const updataHtmlText = async (e, base64font, fontObj) => {
7
3
  const { scaleX, scaleY } = e.worldTransform;
8
4
  const zoomScale = Math.max(Math.abs(scaleX), Math.abs(scaleY));
@@ -22,18 +18,14 @@ export const updataHtmlText = async (e, base64font, fontObj) => {
22
18
  if (dom && e.data.textData?.textShadow) {
23
19
  dom.style.textShadow = e.data.textData.textShadow;
24
20
  }
25
- else if (dom) {
21
+ else {
26
22
  dom.style.textShadow = 'none';
27
23
  }
28
24
  if (dom && e.data.textData?.alignContent) {
29
25
  const qlEditor = dom.querySelector('.ql-editor');
30
- if (qlEditor) {
31
- qlEditor.style.alignContent = e.data.textData.alignContent;
32
- }
26
+ qlEditor.style.alignContent = e.data.textData.alignContent;
33
27
  }
34
- const { quill } = getEditorContext();
35
- if (!quill)
36
- return;
28
+ const quill = quillManager.getQuill();
37
29
  const html = quill.getSemanticHTML();
38
30
  if (html === '<p></p>') {
39
31
  if (e.text.includes('<style>@font-face')) {
@@ -72,9 +64,6 @@ export const updataHtmlText = async (e, base64font, fontObj) => {
72
64
  }
73
65
  };
74
66
  const addFontSizeToP = (e, html, fontSize = 16, lineHeight = '1.5', letterSpacing = '0', textShadow = 'none', alignContent = 'start') => {
75
- const { quill } = getEditorContext();
76
- if (!quill)
77
- return html;
78
67
  const { scaleX, scaleY } = e.worldTransform;
79
68
  const zoomScale = Math.max(Math.abs(scaleX), Math.abs(scaleY));
80
69
  const wrapper = document.createElement('div');
@@ -94,6 +83,7 @@ const addFontSizeToP = (e, html, fontSize = 16, lineHeight = '1.5', letterSpacin
94
83
  }
95
84
  let height;
96
85
  const div = document.querySelector('#textInnerEditor');
86
+ const quill = quillManager.getQuill();
97
87
  const actualHeight = Number((quill.scroll.domNode.scrollHeight / zoomScale).toFixed(0));
98
88
  const actualWidth = Number((quill.scroll.domNode.scrollWidth / zoomScale).toFixed(0));
99
89
  if (['center', 'end'].includes(e.data.textData.alignContent)) {
@@ -113,7 +103,7 @@ const addFontSizeToP = (e, html, fontSize = 16, lineHeight = '1.5', letterSpacin
113
103
  height = `${e.parent.height}px`;
114
104
  }
115
105
  }
116
- const style = `<style>sub,sup{font-size:63%;}.ql-ui{position:absolute}ol,ul{counter-reset:list-0;padding-left:1.5em;margin:0}ol>li,ul>li{counter-increment:list-0;list-style-type:none;position:relative;padding-left:0;margin:0}ol>li::before{content:counter(list-0,decimal) '. ';position:absolute;left:-1.5em;width:1.2em;text-align:right}ul>li::before{content:'\\u2022';position:absolute;left:-1.5em;width:1.2em;text-align:right}li[data-list]{counter-set:list-1 list-2 list-3 list-4 list-5 list-6 list-7 list-8 list-9}.ql-align-center{text-align:center}.ql-align-right{text-align:right}.ql-align-left{text-align:left}.ql-align-justify{text-align:justify}</style>`;
106
+ const style = `<style>sub,sup{font-size:63%;}.ql-ui{position:absolute}ol,ul{counter-reset:list-0;padding-left:1.5em;margin:0}ol>li,ul>li{counter-increment:list-0;list-style-type:none;position:relative;padding-left:0;margin:0}ol>li::before{content:counter(list-0,decimal) '. ';position:absolute;left:-1.5em;width:1.2em;text-align:right}ul>li::before{content:'\u2022';position:absolute;left:-1.5em;width:1.2em;text-align:right}li[data-list]{counter-set:list-1 list-2 list-3 list-4 list-5 list-6 list-7 list-8 list-9}.ql-align-center{text-align:center}.ql-align-right{text-align:right}.ql-align-left{text-align:left}.ql-align-justify{text-align:justify}</style>`;
117
107
  let divBox = '';
118
108
  if (e.parent.children[0].tag.includes('Shape')) {
119
109
  divBox =
@@ -136,25 +126,18 @@ const addFontSizeToP = (e, html, fontSize = 16, lineHeight = '1.5', letterSpacin
136
126
  console.log(divBox, '最终的html内容');
137
127
  return divBox;
138
128
  };
139
- export const setHTMLText = (key, value, base64font, editor, isInnerEditor) => {
140
- const { quill } = getEditorContext();
129
+ export const setHTMLText = (key, value, base64font) => {
130
+ const quill = quillManager.getQuill();
131
+ const { editor, canvas } = quillManager.getCanvas();
132
+ console.log(editor, quillManager.getCanvas(), 'editor');
141
133
  if (!quill) {
142
- console.error('Quill editor not initialized');
143
134
  return;
144
135
  }
145
- const rangeStr = localStorage.getItem('selection-change');
146
- const range = rangeStr ? JSON.parse(rangeStr) : null;
147
- if (range && isInnerEditor) {
148
- quill.setSelection(range.index, range.length);
149
- }
150
- if (!editor || !editor.dateEdit) {
151
- console.warn('Editor context not available for dateEdit');
152
- return;
153
- }
154
- editor.dateEdit(async (e) => {
136
+ const range = JSON.parse(localStorage.getItem('selection-change') || '{}');
137
+ quillManager.dateEdit(async (e) => {
155
138
  if (key === 'font') {
156
139
  const fontSimpleName = value.code.replace(/\s+/g, '');
157
- if (isInnerEditor) {
140
+ if (editor.innerEditing) {
158
141
  if (range && range.length) {
159
142
  quill.formatText(range.index, range.length, key, fontSimpleName);
160
143
  }
@@ -164,18 +147,18 @@ export const setHTMLText = (key, value, base64font, editor, isInnerEditor) => {
164
147
  updataHtmlText(e, base64font ?? null, value ?? null);
165
148
  }
166
149
  else {
167
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
150
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
168
151
  quill.formatText(0, quill.getLength() - 1, key, fontSimpleName);
169
152
  updataHtmlText(e, base64font ?? null, value ?? null);
170
153
  }
171
154
  }
172
155
  else if (key === 'fontSize') {
173
156
  e.data.textData[key] = value;
174
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
157
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
175
158
  updataHtmlText(e, base64font ?? null);
176
159
  }
177
160
  else if (key === 'textCase') {
178
- if (isInnerEditor && range) {
161
+ if (editor.innerEditing) {
179
162
  const text = quill.getText(range.index, range.length);
180
163
  const formats = quill.getFormat(range.index, range.length);
181
164
  quill.deleteText(range.index, range.length);
@@ -190,16 +173,14 @@ export const setHTMLText = (key, value, base64font, editor, isInnerEditor) => {
190
173
  convertedText = text.toUpperCase();
191
174
  }
192
175
  quill.insertText(range.index, convertedText, formats);
193
- if (range && isInnerEditor) {
194
- quill.setSelection(range.index, range.length);
195
- }
176
+ range && editor.innerEditing && quill.setSelection(range.index, range.length);
196
177
  }
197
178
  }
198
179
  else if (key === 'script') {
199
180
  let val = 'sub';
200
181
  if (value === 'super')
201
182
  val = 'sup';
202
- if (isInnerEditor) {
183
+ if (editor.innerEditing) {
203
184
  if (range && range.length) {
204
185
  quill.formatText(range.index, range.length, key, quill.getFormat(range).script === value ? false : val);
205
186
  }
@@ -208,14 +189,14 @@ export const setHTMLText = (key, value, base64font, editor, isInnerEditor) => {
208
189
  }
209
190
  }
210
191
  else {
211
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
192
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
212
193
  quill.formatText(0, quill.getLength() - 1, key, quill.getFormat().script === value ? false : val);
213
194
  updataHtmlText(e);
214
195
  }
215
196
  }
216
197
  else if (key === 'align') {
217
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
218
- if (isInnerEditor) {
198
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
199
+ if (editor.innerEditing) {
219
200
  quill.format(key, value);
220
201
  }
221
202
  else {
@@ -224,28 +205,28 @@ export const setHTMLText = (key, value, base64font, editor, isInnerEditor) => {
224
205
  updataHtmlText(e);
225
206
  }
226
207
  else if (key === 'alignContent') {
227
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
208
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
228
209
  e.data.textData[key] = value;
229
210
  updataHtmlText(e);
230
211
  }
231
212
  else if (key === 'color') {
232
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
213
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
233
214
  quill.formatText(0, quill.getLength() - 1, key, value);
234
215
  if (e.tag === 'HTMLText') {
235
216
  updataHtmlText(e);
236
217
  }
237
- else if (e.parent.findOne && e.parent.findOne('HTMLText')) {
218
+ else if (e.parent.findOne('HTMLText')) {
238
219
  updataHtmlText(e.parent.findOne('HTMLText'));
239
220
  }
240
221
  }
241
222
  else if (key === 'textShadow') {
242
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
223
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
243
224
  e.data.textData[key] = value;
244
225
  updataHtmlText(e);
245
226
  }
246
227
  else if (key === 'list') {
247
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
248
- if (isInnerEditor) {
228
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
229
+ if (editor.innerEditing) {
249
230
  const [line] = quill.getLine(range?.index || 0);
250
231
  if (line.formats().list) {
251
232
  quill.format(key, false);
@@ -266,7 +247,7 @@ export const setHTMLText = (key, value, base64font, editor, isInnerEditor) => {
266
247
  updataHtmlText(e);
267
248
  }
268
249
  else {
269
- if (isInnerEditor) {
250
+ if (editor.innerEditing) {
270
251
  if (range && range.length) {
271
252
  quill.formatText(range.index, range.length, key, !quill.getFormat(range)[key]);
272
253
  }
@@ -275,7 +256,7 @@ export const setHTMLText = (key, value, base64font, editor, isInnerEditor) => {
275
256
  }
276
257
  }
277
258
  else {
278
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
259
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
279
260
  quill.formatText(0, quill.getLength() - 1, key, !quill.getFormat()[key]);
280
261
  updataHtmlText(e);
281
262
  }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,19 @@
1
- export { TextEditor } from "./TextEditor";
1
+ import Quill from "quill";
2
2
  import "./TextEditTool";
3
- import "quill/dist/quill.core.css";
4
- export { updataHtmlText, setHTMLText } from "./utils";
5
- export { fontManager, defaultFonts, FontManager } from "./fonts/font";
6
- export declare function initTextEditorQuill(container?: HTMLElement): any;
3
+ import './TextEditor';
4
+ declare class QuillManager {
5
+ private static instance;
6
+ private quill;
7
+ app_: any | null;
8
+ private constructor();
9
+ static getInstance(): QuillManager;
10
+ init(app: any): Promise<Quill>;
11
+ getQuill(): Quill;
12
+ getCanvas(): any;
13
+ private registerFonts;
14
+ isMultiSelect(): boolean;
15
+ dateEdit(callback: (leaf: any) => void, level?: number, listNew?: any): void;
16
+ }
17
+ export declare const quillManager: QuillManager;
18
+ export {};
7
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,gBAAgB,CAAC;AAIxB,OAAO,2BAA2B,CAAC;AAGnC,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAItE,wBAAgB,mBAAmB,CAAC,SAAS,CAAC,EAAE,WAAW,OA6E1D"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAgB,MAAM,OAAO,CAAC;AACrC,OAAO,gBAAgB,CAAC;AACxB,OAAO,cAAc,CAAA;AACrB,cAAM,YAAY;IAChB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAe;IACtC,OAAO,CAAC,KAAK,CAAsB;IAC5B,IAAI,EAAE,GAAG,GAAG,IAAI,CAAQ;IAE/B,OAAO;IAEP,MAAM,CAAC,WAAW;IAOZ,IAAI,CAAC,GAAG,EAAE,GAAG;IAiDnB,QAAQ;IAOR,SAAS;IAOT,OAAO,CAAC,aAAa;IA+Bd,aAAa,IAAI,OAAO;IAQxB,QAAQ,CACb,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,EAC7B,KAAK,SAAI,EACT,OAAO,CAAC,EAAE,GAAG,GACZ,IAAI;CA4BR;AAED,eAAO,MAAM,YAAY,cAA6B,CAAC"}
package/dist/index.esm.js CHANGED
@@ -1,79 +1,137 @@
1
- import { TextEditor } from "./TextEditor";
2
- export { TextEditor } from "./TextEditor";
1
+ import Quill, { Delta } from "quill";
3
2
  import "./TextEditTool";
4
- import { Plugin } from "@leafer-ui/core";
5
- import { Delta } from "quill";
6
- import Quill from "quill";
7
- import "quill/dist/quill.core.css";
8
- export { updataHtmlText, setHTMLText } from "./utils";
9
- export { fontManager, defaultFonts, FontManager } from "./fonts/font";
10
- Plugin.add("leafer-htmltext-editor", "editor");
11
- export function initTextEditorQuill(container) {
12
- const textInner = document.getElementById("textInnerEditor");
13
- if (!textInner) {
14
- const el = document.createElement("div");
15
- el.id = "textInnerEditor";
16
- el.style.position = "fixed";
17
- el.style.transformOrigin = "left top";
18
- el.style.overflowWrap = "break-word";
19
- el.style.wordBreak = "break-all";
20
- el.style.visibility = "hidden";
21
- document.body.appendChild(el);
3
+ import './TextEditor';
4
+ class QuillManager {
5
+ constructor() {
6
+ this.quill = null;
7
+ this.app_ = null;
22
8
  }
23
- TextEditor.quill = new Quill("#textInnerEditor", {
24
- theme: null,
25
- modules: {
26
- toolbar: false,
27
- keyboard: {
28
- bindings: {
29
- enter: {
30
- key: "Enter",
31
- handler: (range, context) => {
32
- const [line] = TextEditor.quill.getLine(range.index);
33
- const BlockBlot = Quill.import("blots/block");
34
- if (!BlockBlot || !BlockBlot.bubbleFormats)
35
- return;
36
- const lineFormats = BlockBlot.bubbleFormats(line);
37
- const delta = new Delta()
38
- .retain(range.index)
39
- .delete(range.length)
40
- .insert("\n", lineFormats);
41
- TextEditor.quill.updateContents(delta, Quill.sources.USER);
42
- TextEditor.quill.setSelection(range.index + 1, Quill.sources.SILENT);
43
- return false;
9
+ static getInstance() {
10
+ if (!QuillManager.instance) {
11
+ QuillManager.instance = new QuillManager();
12
+ }
13
+ return QuillManager.instance;
14
+ }
15
+ async init(app) {
16
+ this.app_ = app;
17
+ if (this.quill)
18
+ return this.quill;
19
+ let el = document.getElementById("textInnerEditor");
20
+ if (!el) {
21
+ el = document.createElement("div");
22
+ el.id = "textInnerEditor";
23
+ el.style.position = "fixed";
24
+ el.style.transformOrigin = "left top";
25
+ el.style.overflowWrap = "break-word";
26
+ el.style.wordBreak = "break-all";
27
+ el.style.visibility = "hidden";
28
+ document.body.appendChild(el);
29
+ }
30
+ this.quill = new Quill("#textInnerEditor", {
31
+ theme: undefined,
32
+ modules: {
33
+ toolbar: false,
34
+ keyboard: {
35
+ bindings: {
36
+ enter: {
37
+ key: "Enter",
38
+ handler: (range) => {
39
+ const [line] = this.quill.getLine(range.index);
40
+ const BlockBlot = Quill.import("blots/block");
41
+ if (!BlockBlot?.bubbleFormats)
42
+ return true;
43
+ const lineFormats = BlockBlot.bubbleFormats(line);
44
+ const delta = new Delta()
45
+ .retain(range.index)
46
+ .delete(range.length)
47
+ .insert("\n", lineFormats);
48
+ this.quill.updateContents(delta, Quill.sources.USER);
49
+ this.quill.setSelection(range.index + 1, Quill.sources.SILENT);
50
+ return false;
51
+ },
44
52
  },
45
53
  },
46
54
  },
47
55
  },
48
- },
49
- });
50
- const FontAttributor = Quill.import("attributors/class/font");
51
- FontAttributor.whitelist = [
52
- "Roboto",
53
- "RobotoMono",
54
- "Inter",
55
- "OpenSans",
56
- "Montserrat",
57
- "RobotoCondensed",
58
- "Arimo",
59
- "NotoSans",
60
- "NotoSansSymbols",
61
- "Merriweather",
62
- "PlayfairDisplay",
63
- "NotoSerif",
64
- "Lato",
65
- "Spectral",
66
- "DancingScript",
67
- "NotoSansSimplifiedChinese",
68
- "NotoSerifSimplifiedChinese",
69
- "NotoSansTraditionalChinese",
70
- "NotoSansHongKong",
71
- "NotoSerifTraditionalChinese",
72
- "NotoSerifHongKong",
73
- "NotoSansJapanese",
74
- "NotoSansKorean",
75
- "Poppins",
76
- ];
77
- Quill.register(FontAttributor, true);
78
- return TextEditor.quill;
56
+ });
57
+ this.app_.editor.quill = this.quill;
58
+ this.registerFonts();
59
+ return this.quill;
60
+ }
61
+ getQuill() {
62
+ if (!this.quill) {
63
+ throw new Error("Quill editor not initialized. Call init() first.");
64
+ }
65
+ return this.quill;
66
+ }
67
+ getCanvas() {
68
+ if (!this.app_) {
69
+ throw new Error("app_ editor not initialized. Call init() first.");
70
+ }
71
+ return this.app_;
72
+ }
73
+ registerFonts() {
74
+ const FontAttributor = Quill.import("attributors/class/font");
75
+ FontAttributor.whitelist = [
76
+ "Roboto",
77
+ "RobotoMono",
78
+ "Inter",
79
+ "OpenSans",
80
+ "Montserrat",
81
+ "RobotoCondensed",
82
+ "Arimo",
83
+ "NotoSans",
84
+ "NotoSansSymbols",
85
+ "Merriweather",
86
+ "PlayfairDisplay",
87
+ "NotoSerif",
88
+ "Lato",
89
+ "Spectral",
90
+ "DancingScript",
91
+ "NotoSansSimplifiedChinese",
92
+ "NotoSerifSimplifiedChinese",
93
+ "NotoSansTraditionalChinese",
94
+ "NotoSansHongKong",
95
+ "NotoSerifTraditionalChinese",
96
+ "NotoSerifHongKong",
97
+ "NotoSansJapanese",
98
+ "NotoSansKorean",
99
+ "Poppins",
100
+ ];
101
+ Quill.register(FontAttributor, true);
102
+ }
103
+ isMultiSelect() {
104
+ if (!this.app_.editor)
105
+ return false;
106
+ if (this.app_.editor.multiple === true) {
107
+ return true;
108
+ }
109
+ else {
110
+ return false;
111
+ }
112
+ }
113
+ dateEdit(callback, level = 0, listNew) {
114
+ const { editor } = this.app_;
115
+ const list = listNew ? listNew : editor.leafList.list;
116
+ const applyCallback = (leaf) => {
117
+ if (level && (leaf.tag === "Box" || leaf.name === "Text")) {
118
+ callback(leaf.children?.[0] || leaf);
119
+ }
120
+ else {
121
+ callback(leaf);
122
+ }
123
+ };
124
+ if (!list.length)
125
+ return;
126
+ if (Array.isArray(list) && list.length > 1) {
127
+ this.app_.lockLayout();
128
+ list.forEach(applyCallback);
129
+ this.app_.unlockLayout();
130
+ editor.updateEditBox();
131
+ }
132
+ else {
133
+ applyCallback(list[0]);
134
+ }
135
+ }
79
136
  }
137
+ export const quillManager = QuillManager.getInstance();