@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/src/utils.ts CHANGED
@@ -1,61 +1,47 @@
1
- import { TextEditor } from './TextEditor';
2
-
3
- // 获取编辑器和quill实例的辅助函数
4
- function getEditorContext() {
5
- const quill = TextEditor.quill;
6
- return { quill };
7
- }
8
-
1
+ import { quillManager } from '.'
9
2
  // 更新text样式
10
3
  export const updataHtmlText = async (e?: any, base64font?: any, fontObj?: any) => {
11
- const { scaleX, scaleY } = e.worldTransform;
12
- const zoomScale = Math.max(Math.abs(scaleX), Math.abs(scaleY));
13
- const dom: HTMLElement | null = document.querySelector('#textInnerEditor');
14
-
4
+ const { scaleX, scaleY } = e.worldTransform
5
+ const zoomScale = Math.max(Math.abs(scaleX), Math.abs(scaleY))
6
+ const dom: any = document.querySelector('#textInnerEditor')
15
7
  if (dom && e.data.textData?.fontFamily) {
16
- dom.style.fontFamily = e.data.textData.fontFamily;
8
+ dom.style.fontFamily = e.data.textData.fontFamily
17
9
  }
18
10
  if (dom && e.data.textData?.fontSize) {
19
- dom.style.fontSize = `${e.data.textData.fontSize * zoomScale}px`;
11
+ dom.style.fontSize = `${e.data.textData.fontSize * zoomScale}px`
20
12
  }
21
13
  if (dom && e.data.textData?.lineHeight) {
22
- dom.style.lineHeight = e.data.textData.lineHeight;
14
+ dom.style.lineHeight = e.data.textData.lineHeight
23
15
  }
24
16
  if (dom && e.data.textData?.letterSpacing) {
25
- dom.style.letterSpacing = `${e.data.textData.letterSpacing}px`;
17
+ dom.style.letterSpacing = `${e.data.textData.letterSpacing}px`
26
18
  }
27
19
  if (dom && e.data.textData?.textShadow) {
28
- dom.style.textShadow = e.data.textData.textShadow;
29
- } else if (dom) {
30
- dom.style.textShadow = 'none';
20
+ dom.style.textShadow = e.data.textData.textShadow
21
+ } else {
22
+ dom.style.textShadow = 'none'
31
23
  }
32
24
  if (dom && e.data.textData?.alignContent) {
33
- const qlEditor: any = dom.querySelector('.ql-editor');
34
- if (qlEditor) {
35
- qlEditor.style.alignContent = e.data.textData.alignContent;
36
- }
25
+ const qlEditor: any = dom.querySelector('.ql-editor')
26
+ qlEditor.style.alignContent = e.data.textData.alignContent
37
27
  }
38
-
39
- const { quill } = getEditorContext();
40
- if (!quill) return;
41
-
42
- // 如果是空的就把text置空
43
- const html = quill.getSemanticHTML();
28
+ // 如果是空的就把text滞空
29
+ const quill = quillManager.getQuill()
30
+ const html = quill.getSemanticHTML()
44
31
  if (html === '<p></p>') {
45
32
  if (e.text.includes('<style>@font-face')) {
46
- e.text = e.text.split('</style>')[0] + '</style>';
33
+ e.text = e.text.split('</style>')[0] + '</style>'
47
34
  } else {
48
- e.text = '';
35
+ e.text = ''
49
36
  }
50
- return;
37
+ return
51
38
  }
52
-
53
39
  if (e.text.includes('<style>@font-face')) {
54
40
  // text已经加载过字体
55
- const style = e.text.split('</style>')[0];
41
+ const style = e.text.split('</style>')[0]
56
42
  if (fontObj && !style.includes(fontObj.code)) {
57
43
  // 在字体style标签检索是否有当前字体 没有就添加字体style
58
- const addStyle = `@font-face {font-family: ${fontObj.code};src: url(${base64font}) format('woff2') } .ql-font-${fontObj?.code?.replace(/\s+/g, '')} {font-family: ${fontObj.name};}`;
44
+ const addStyle = `@font-face {font-family: ${fontObj.code};src: url(${base64font}) format('woff2') } .ql-font-${fontObj?.code?.replace(/\s+/g, '')} {font-family: ${fontObj.name};}`
59
45
  e.text =
60
46
  style +
61
47
  addStyle +
@@ -68,7 +54,7 @@ export const updataHtmlText = async (e?: any, base64font?: any, fontObj?: any) =
68
54
  e.data.textData.letterSpacing,
69
55
  e.data.textData.textShadow,
70
56
  e.data.textData?.alignContent
71
- );
57
+ )
72
58
  } else {
73
59
  // 样式当前字体已经存在
74
60
  e.text =
@@ -82,11 +68,11 @@ export const updataHtmlText = async (e?: any, base64font?: any, fontObj?: any) =
82
68
  e.data.textData.letterSpacing,
83
69
  e.data.textData.textShadow,
84
70
  e.data.textData?.alignContent
85
- );
71
+ )
86
72
  }
87
73
  } else if (base64font && fontObj) {
88
74
  // 未加载过字体初次添加字体 存在base64font && fontObj才触发
89
- const style = `<style>@font-face {font-family: ${e.data.textData.fontFamily.split(',')[0]};src: url(${base64font}) format('woff2') } .ql-font-${fontObj?.code?.replace(/\s+/g, '')} {font-family: ${fontObj.name};}</style>`;
75
+ const style = `<style>@font-face {font-family: ${e.data.textData.fontFamily.split(',')[0]};src: url(${base64font}) format('woff2') } .ql-font-${fontObj?.code?.replace(/\s+/g, '')} {font-family: ${fontObj.name};}</style>`
90
76
  e.text =
91
77
  style +
92
78
  addFontSizeToP(
@@ -97,7 +83,7 @@ export const updataHtmlText = async (e?: any, base64font?: any, fontObj?: any) =
97
83
  e.data.textData.letterSpacing,
98
84
  e.data.textData.textShadow,
99
85
  e.data.textData?.alignContent
100
- );
86
+ )
101
87
  } else {
102
88
  // text没有包含字体就不加字体style
103
89
  e.text = addFontSizeToP(
@@ -108,224 +94,200 @@ export const updataHtmlText = async (e?: any, base64font?: any, fontObj?: any) =
108
94
  e.data.textData.letterSpacing,
109
95
  e.data.textData.textShadow,
110
96
  e.data.textData?.alignContent
111
- );
97
+ )
112
98
  }
113
- };
114
-
115
- const addFontSizeToP = (
116
- e: any,
117
- html: any,
118
- fontSize = 16,
119
- lineHeight = '1.5',
120
- letterSpacing = '0',
121
- textShadow = 'none',
122
- alignContent = 'start'
123
- ) => {
124
- const { quill } = getEditorContext();
125
- if (!quill) return html;
126
-
127
- const { scaleX, scaleY } = e.worldTransform;
128
- const zoomScale = Math.max(Math.abs(scaleX), Math.abs(scaleY));
129
- const wrapper = document.createElement('div');
130
- wrapper.innerHTML = html;
131
-
99
+ }
100
+ const addFontSizeToP = (e: any, html: any, fontSize = 16, lineHeight = '1.5', letterSpacing = '0', textShadow = 'none', alignContent = 'start') => {
101
+ const { scaleX, scaleY } = e.worldTransform
102
+ const zoomScale = Math.max(Math.abs(scaleX), Math.abs(scaleY))
103
+ const wrapper = document.createElement('div')
104
+ wrapper.innerHTML = html
132
105
  // 添加的样式 用于HTMLText渲染
133
106
  const wrapperStyle: any = {
134
107
  fontSize: `${fontSize}px`,
135
108
  lineHeight,
136
109
  letterSpacing: `${letterSpacing}px`,
137
110
  textShadow
138
- };
139
-
111
+ }
140
112
  wrapper.querySelectorAll('p,ol,ul').forEach((p: any) => {
141
- Object.assign(p.style, wrapperStyle);
142
- });
113
+ Object.assign(p.style, wrapperStyle)
114
+ })
143
115
 
144
- // 这个上下标还是要控制一下尺寸 因为html和canvas规范不一样 所以这里要控制一下
145
- let str = wrapper.innerHTML;
146
-
116
+ // 这个上下标还是要控制一下尺寸 因为html和canvas规范不一样 所以这里要控制一下 当然如果存在别的style例如color的style就合并style
117
+ let str = wrapper.innerHTML
147
118
  // 排查如果有回车换行的加上一个字符
148
119
  if (/<p\b[^>]*><\/p>/.test(str)) {
149
- str = str.replace(/<p\b([^>]*)><\/p>/g, '<p$1>&nbsp;</p>');
120
+ str = str.replace(/<p\b([^>]*)><\/p>/g, '<p$1>&nbsp;</p>')
150
121
  }
151
-
152
- let height;
153
- const div: any = document.querySelector('#textInnerEditor');
154
-
122
+ let height
123
+ const div: any = document.querySelector('#textInnerEditor')
124
+ const quill = quillManager.getQuill()
125
+ // 这里别动
155
126
  // 获取包含自动换行的实际内容高度
156
- const actualHeight = Number((quill.scroll.domNode.scrollHeight / zoomScale).toFixed(0));
157
- const actualWidth = Number((quill.scroll.domNode.scrollWidth / zoomScale).toFixed(0));
158
-
127
+ const actualHeight = Number((quill.scroll.domNode.scrollHeight / zoomScale).toFixed(0))
128
+ const actualWidth = Number((quill.scroll.domNode.scrollWidth / zoomScale).toFixed(0))
159
129
  if (['center', 'end'].includes(e.data.textData.alignContent)) {
160
130
  if (e.parent.height < actualHeight) {
161
- e.data.textData.alignContent = 'start';
162
- height = (actualHeight || e.__layout.boxBounds.height) + 'px';
131
+ e.data.textData.alignContent = 'start'
132
+ height = (actualHeight || e.__layout.boxBounds.height) + 'px'
163
133
  } else {
164
- height = `${e.parent.height}px`;
134
+ height = `${e.parent.height}px`
165
135
  }
166
136
  } else {
167
137
  if (e.parent.height < actualHeight) {
168
- height = (actualHeight || e.__layout.boxBounds.height) + 'px';
138
+ height = (actualHeight || e.__layout.boxBounds.height) + 'px'
169
139
  } else {
170
- height = `${e.parent.height}px`;
140
+ height = `${e.parent.height}px`
171
141
  }
172
142
  }
173
143
 
174
- // 水平居中相关样式内嵌
175
- 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>`;
176
-
177
- let divBox = '';
144
+ // 水平居中相关样式内嵌 因为这个quill居中用的class没用style
145
+ 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>`
146
+ let divBox = ''
178
147
  if (e.parent.children[0].tag.includes('Shape')) {
179
148
  divBox =
180
149
  style +
181
- `<div style="width: ${e.parent.width}px;height: ${e.parent.height}px;overflow-wrap:break-word;word-break:break-all;align-content:center;">${str}</div>`;
150
+ `<div style="width: ${e.parent.width}px;height: ${e.parent.height}px;overflow-wrap:break-word;word-break:break-all;align-content:center;">${str}</div>`
182
151
  } else if (e.data.canChangeBox) {
183
152
  divBox =
184
153
  style +
185
- `<div style="width: ${e.parent.width}px;height:${height};overflow-wrap:break-word;word-break:break-all;align-content:${alignContent};">${str}</div>`;
154
+ `<div style="width: ${e.parent.width}px;height:${height};overflow-wrap:break-word;word-break:break-all;align-content:${alignContent};">${str}</div>`
186
155
  } else {
187
156
  // 斜体补偿宽度 控制
188
157
  if (e.data.textData.italic) {
189
- divBox = style + str;
158
+ divBox = style + str
190
159
  } else {
191
- divBox = style + `<div style="width: ${actualWidth - 10}px">${str}</div>`;
160
+ divBox = style + `<div style="width: ${actualWidth - 10}px">${str}</div>`
192
161
  }
193
162
  }
194
- console.log(divBox, '最终的html内容');
195
- return divBox;
196
- };
197
-
198
- export const setHTMLText = (
199
- key: string,
200
- value?: any,
201
- base64font?: any,
202
- editor?: any,
203
- isInnerEditor?: boolean
204
- ) => {
205
- const { quill } = getEditorContext();
163
+ console.log(divBox, '最终的html内容')
164
+ return divBox
165
+ }
166
+ export const setHTMLText = (key: string, value?: any, base64font?: any) => {
167
+ const quill = quillManager.getQuill()
168
+ const { editor, canvas } = quillManager.getCanvas()
169
+ console.log(editor, quillManager.getCanvas(), 'editor')
206
170
  if (!quill) {
207
- console.error('Quill editor not initialized');
208
- return;
209
- }
210
-
211
- const rangeStr = localStorage.getItem('selection-change');
212
- const range = rangeStr ? JSON.parse(rangeStr) : null;
213
-
214
- if (range && isInnerEditor) {
215
- quill.setSelection(range.index, range.length);
171
+ return
216
172
  }
173
+ const range = JSON.parse(localStorage.getItem('selection-change') || '{}')
217
174
 
218
- if (!editor || !editor.dateEdit) {
219
- console.warn('Editor context not available for dateEdit');
220
- return;
221
- }
222
-
223
- editor.dateEdit(async (e: any) => {
175
+ quillManager.dateEdit(async (e: any) => {
224
176
  if (key === 'font') {
225
- const fontSimpleName = value.code.replace(/\s+/g, '');
226
- if (isInnerEditor) {
177
+ const fontSimpleName = value.code.replace(/\s+/g, '')
178
+ if (editor.innerEditing) {
227
179
  if (range && range.length) {
228
- quill.formatText(range.index, range.length, key, fontSimpleName);
180
+ quill.formatText(range.index, range.length, key, fontSimpleName)
229
181
  } else {
230
- quill.formatText(0, quill.getLength() - 1, key, fontSimpleName);
182
+ quill.formatText(0, quill.getLength() - 1, key, fontSimpleName)
231
183
  }
232
- updataHtmlText(e, base64font ?? null, value ?? null);
184
+ updataHtmlText(e, base64font ?? null, value ?? null)
233
185
  } else {
234
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
235
- quill.formatText(0, quill.getLength() - 1, key, fontSimpleName);
236
- updataHtmlText(e, base64font ?? null, value ?? null);
186
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text)
187
+ quill.formatText(0, quill.getLength() - 1, key, fontSimpleName)
188
+ updataHtmlText(e, base64font ?? null, value ?? null)
237
189
  }
238
190
  } else if (key === 'fontSize') {
239
- e.data.textData[key] = value;
240
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
241
- updataHtmlText(e, base64font ?? null);
191
+ // 字体和字号
192
+ e.data.textData[key] = value
193
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text)
194
+ updataHtmlText(e, base64font ?? null)
242
195
  } else if (key === 'textCase') {
243
- if (isInnerEditor && range) {
244
- const text = quill.getText(range.index, range.length);
245
- const formats = quill.getFormat(range.index, range.length);
246
- quill.deleteText(range.index, range.length);
247
- let convertedText;
196
+ // 大小写
197
+ if (editor.innerEditing) {
198
+ const text = quill.getText(range.index, range.length)
199
+ const formats = quill.getFormat(range.index, range.length)
200
+ quill.deleteText(range.index, range.length)
201
+ let convertedText
248
202
  if (text === text.toUpperCase() && /[A-Z]/.test(text)) {
249
- convertedText = text.toLowerCase();
203
+ convertedText = text.toLowerCase()
250
204
  } else if (text === text.toLowerCase() && /[a-z]/.test(text)) {
251
- convertedText = text.toUpperCase();
205
+ convertedText = text.toUpperCase()
252
206
  } else {
253
- convertedText = text.toUpperCase();
207
+ convertedText = text.toUpperCase()
254
208
  }
255
209
 
256
- quill.insertText(range.index, convertedText, formats);
257
- if (range && isInnerEditor) {
258
- quill.setSelection(range.index, range.length);
259
- }
210
+ quill.insertText(range.index, convertedText, formats)
211
+ range && editor.innerEditing && quill.setSelection(range.index, range.length)
260
212
  }
261
213
  } else if (key === 'script') {
262
- let val = 'sub';
263
- if (value === 'super') val = 'sup';
264
- if (isInnerEditor) {
214
+ // 上下标
215
+ let val = 'sub'
216
+ if (value === 'super') val = 'sup'
217
+ if (editor.innerEditing) {
265
218
  if (range && range.length) {
266
- quill.formatText(range.index, range.length, key, quill.getFormat(range).script === value ? false : val);
219
+ quill.formatText(range.index, range.length, key, quill.getFormat(range).script === value ? false : val)
267
220
  } else {
268
- quill.formatText(0, quill.getLength() - 1, key, quill.getFormat().script === value ? false : val);
221
+ quill.formatText(0, quill.getLength() - 1, key, quill.getFormat().script === value ? false : val)
269
222
  }
270
223
  } else {
271
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
272
- quill.formatText(0, quill.getLength() - 1, key, quill.getFormat().script === value ? false : val);
273
- updataHtmlText(e);
224
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text)
225
+ quill.formatText(0, quill.getLength() - 1, key, quill.getFormat().script === value ? false : val)
226
+ updataHtmlText(e)
274
227
  }
275
228
  } else if (key === 'align') {
276
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
277
- if (isInnerEditor) {
278
- quill.format(key, value);
229
+ // 水平居中
230
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text)
231
+ if (editor.innerEditing) {
232
+ quill.format(key, value)
279
233
  } else {
280
- quill.formatLine(0, quill.getLength(), key, value);
234
+ quill.formatLine(0, quill.getLength(), key, value)
281
235
  }
282
- updataHtmlText(e);
236
+ updataHtmlText(e)
283
237
  } else if (key === 'alignContent') {
284
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
285
- e.data.textData[key] = value;
286
- updataHtmlText(e);
238
+ // 水平居中
239
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text)
240
+ e.data.textData[key] = value
241
+ updataHtmlText(e)
287
242
  } else if (key === 'color') {
288
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
289
- quill.formatText(0, quill.getLength() - 1, key, value);
243
+ // 颜色
244
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text)
245
+ quill.formatText(0, quill.getLength() - 1, key, value)
290
246
  if (e.tag === 'HTMLText') {
291
- updataHtmlText(e);
292
- } else if (e.parent.findOne && e.parent.findOne('HTMLText')) {
293
- updataHtmlText(e.parent.findOne('HTMLText'));
247
+ updataHtmlText(e)
248
+ } else if (e.parent.findOne('HTMLText')) {
249
+ updataHtmlText(e.parent.findOne('HTMLText'))
294
250
  }
295
251
  } else if (key === 'textShadow') {
296
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
297
- e.data.textData[key] = value;
298
- updataHtmlText(e);
252
+ // 阴影
253
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text)
254
+ e.data.textData[key] = value
255
+ updataHtmlText(e)
299
256
  } else if (key === 'list') {
300
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
301
- if (isInnerEditor) {
302
- const [line] = quill.getLine(range?.index || 0);
257
+ // 有序无序列表
258
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text)
259
+ if (editor.innerEditing) {
260
+ const [line]: any = quill.getLine(range?.index || 0)
303
261
  if (line.formats().list) {
304
- quill.format(key, false);
262
+ quill.format(key, false)
305
263
  } else {
306
- quill.format(key, value);
264
+ quill.format(key, value)
307
265
  }
308
266
  } else {
309
- const [line] = quill.getLine(range?.index || 0);
267
+ const [line]: any = quill.getLine(range?.index || 0)
310
268
  if (line.formats().list) {
311
- quill.formatLine(0, quill.getLength(), key, false);
269
+ quill.formatLine(0, quill.getLength(), key, false)
312
270
  } else {
313
- quill.formatLine(0, quill.getLength(), key, value);
271
+ quill.formatLine(0, quill.getLength(), key, value)
314
272
  }
315
273
  }
316
- updataHtmlText(e);
274
+ updataHtmlText(e)
317
275
  } else {
318
- if (isInnerEditor) {
276
+ if (editor.innerEditing) {
319
277
  if (range && range.length) {
320
- quill.formatText(range.index, range.length, key, !quill.getFormat(range)[key]);
278
+ quill.formatText(range.index, range.length, key, !quill.getFormat(range)[key])
321
279
  } else {
322
- quill.formatText(0, quill.getLength() - 1, key, !quill.getFormat()[key]);
280
+ quill.formatText(0, quill.getLength() - 1, key, !quill.getFormat()[key])
323
281
  }
324
282
  } else {
325
- editor.isMultiSelect && editor.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text);
326
- quill.formatText(0, quill.getLength() - 1, key, !quill.getFormat()[key]);
327
- updataHtmlText(e);
283
+ quillManager.isMultiSelect() && quill.clipboard.dangerouslyPasteHTML(e.text)
284
+ quill.formatText(0, quill.getLength() - 1, key, !quill.getFormat()[key])
285
+ updataHtmlText(e)
328
286
  }
329
287
  }
330
- }, 1);
331
- };
288
+
289
+ // if (e.parent.tag === 'Box' && e.parent.findOne('HTMLText') && e.parent.findOne('Box')) {
290
+ // handleShowCurve(e.parent, false)
291
+ // }
292
+ }, 1)
293
+ }