vue-editify 0.1.37 → 0.1.40

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. package/examples/App.vue +6 -10
  2. package/lib/components/button/button.vue.d.ts +13 -13
  3. package/lib/components/button/props.d.ts +1 -1
  4. package/lib/components/checkbox/checkbox.vue.d.ts +10 -10
  5. package/lib/components/checkbox/props.d.ts +1 -1
  6. package/lib/components/colors/colors.vue.d.ts +6 -6
  7. package/lib/components/colors/props.d.ts +1 -1
  8. package/lib/components/icon/icon.vue.d.ts +1 -1
  9. package/lib/components/insertImage/insertImage.vue.d.ts +11 -11
  10. package/lib/components/insertLink/insertLink.vue.d.ts +4 -4
  11. package/lib/components/insertTable/insertTable.vue.d.ts +4 -4
  12. package/lib/components/insertVideo/insertVideo.vue.d.ts +11 -11
  13. package/lib/components/layer/layer.vue.d.ts +9 -9
  14. package/lib/components/menu/menu.vue.d.ts +6 -6
  15. package/lib/components/menu/props.d.ts +1 -1
  16. package/lib/components/toolbar/props.d.ts +1 -1
  17. package/lib/components/toolbar/toolbar.vue.d.ts +11 -11
  18. package/lib/components/tooltip/tooltip.vue.d.ts +1 -1
  19. package/lib/components/triangle/triangle.vue.d.ts +4 -4
  20. package/lib/core/function.d.ts +1 -1
  21. package/lib/core/rule.d.ts +1 -1
  22. package/lib/core/tool.d.ts +8 -11
  23. package/lib/editify/editify.vue.d.ts +110 -84
  24. package/lib/editify/props.d.ts +9 -5
  25. package/lib/editify.es.js +15225 -404
  26. package/lib/editify.umd.js +1 -1
  27. package/lib/index.d.ts +4 -2
  28. package/lib/plugins/attachment/index.d.ts +3 -2
  29. package/lib/plugins/attachment/insertAttachment/insertAttachment.vue.d.ts +11 -11
  30. package/lib/plugins/mathformula/index.d.ts +49 -0
  31. package/lib/plugins/mathformula/insertMathformula/insertMathformula.vue.d.ts +27 -0
  32. package/lib/plugins/mathformula/insertMathformula/props.d.ts +13 -0
  33. package/lib/style.css +1 -1
  34. package/package.json +4 -2
  35. package/src/components/insertImage/insertImage.less +1 -0
  36. package/src/components/insertLink/insertLink.less +1 -0
  37. package/src/components/insertVideo/insertVideo.less +1 -0
  38. package/src/core/function.ts +4 -12
  39. package/src/core/rule.ts +7 -0
  40. package/src/core/tool.ts +3 -33
  41. package/src/editify/editify.less +28 -3
  42. package/src/editify/editify.vue +111 -26
  43. package/src/editify/props.ts +11 -4
  44. package/src/icon/iconfont.css +4 -0
  45. package/src/icon/iconfont.ttf +0 -0
  46. package/src/icon/iconfont.woff +0 -0
  47. package/src/index.ts +4 -1
  48. package/src/locale/en_US.ts +6 -1
  49. package/src/locale/zh_CN.ts +7 -2
  50. package/src/plugins/attachment/index.ts +26 -15
  51. package/src/plugins/attachment/insertAttachment/insertAttachment.less +1 -0
  52. package/src/plugins/mathformula/index.ts +298 -0
  53. package/src/plugins/mathformula/insertMathformula/insertMathformula.less +64 -0
  54. package/src/plugins/mathformula/insertMathformula/insertMathformula.vue +50 -0
  55. package/src/plugins/mathformula/insertMathformula/props.ts +16 -0
@@ -0,0 +1,298 @@
1
+ import { common as DapCommon } from 'dap-util'
2
+ import { ObjectType, PluginType, cloneData } from '../../core/tool'
3
+ import { ComponentInternalInstance, h } from 'vue'
4
+
5
+ import { AlexEditor, AlexElement, AlexElementsRangeType } from 'alex-editor'
6
+ import 'katex/dist/katex.css'
7
+ import KaTex from 'katex'
8
+ import Icon from '../../components/icon/icon.vue'
9
+ import Button from '../../components/button/button.vue'
10
+ import InsertMathformula from './insertMathformula/insertMathformula.vue'
11
+ import { hasLinkInRange, hasPreInRange } from '../../core/function'
12
+
13
+ export type MathformulaOptionsType = {
14
+ //排序
15
+ sequence?: number
16
+ //工具提示内容
17
+ title?: string
18
+ //按钮是否显示左侧边框
19
+ leftBorder?: boolean
20
+ //按钮是否显示右侧边框
21
+ rightBorder?: boolean
22
+ //按钮是否禁用
23
+ disabled?: boolean
24
+ //公式异常处理
25
+ handleError?: (error: Error) => void
26
+ }
27
+
28
+ /**
29
+ * 是否公式元素
30
+ * @param el
31
+ * @returns
32
+ */
33
+ export const isMathformula = (el: AlexElement) => {
34
+ return el.parsedom == 'span' && el.hasMarks() && el.marks!['data-editify-mathformula']
35
+ }
36
+
37
+ /**
38
+ * 判断某个元素是否在公式元素内
39
+ * @param el
40
+ * @returns
41
+ */
42
+ export const isUnderMathformula = (el: AlexElement): boolean => {
43
+ if (isMathformula(el)) {
44
+ return true
45
+ }
46
+ if (el.parent) {
47
+ return isUnderMathformula(el.parent)
48
+ }
49
+ return false
50
+ }
51
+
52
+ /**
53
+ * 根据某个元素获取所在的公式元素,如果不在公式元素内则返回null
54
+ * @param el
55
+ * @returns
56
+ */
57
+ export const getMathformulaElement = (el: AlexElement): AlexElement | null => {
58
+ if (isMathformula(el)) {
59
+ return el
60
+ }
61
+ if (el.parent) {
62
+ return getMathformulaElement(el.parent)
63
+ }
64
+ return null
65
+ }
66
+
67
+ /**
68
+ * 选区是否含有公式元素
69
+ * @param editor
70
+ * @param dataRangeCaches
71
+ * @returns
72
+ */
73
+ export const hasMathformulaInRange = (editor: AlexEditor, dataRangeCaches: AlexElementsRangeType) => {
74
+ if (!editor.range) {
75
+ return false
76
+ }
77
+ if (editor.range.anchor.isEqual(editor.range.focus)) {
78
+ return isUnderMathformula(editor.range.anchor.element)
79
+ }
80
+ return dataRangeCaches.flatList.some(item => {
81
+ return isUnderMathformula(item.element)
82
+ })
83
+ }
84
+
85
+ /**
86
+ * 选区是否在某个公式元素下,如果是返回该公式元素否则返回null
87
+ * @param editor
88
+ * @param dataRangeCaches
89
+ * @returns
90
+ */
91
+ export const getMathformulaElementByRange = (editor: AlexEditor, dataRangeCaches: AlexElementsRangeType) => {
92
+ if (!editor.range) {
93
+ return null
94
+ }
95
+ if (editor.range.anchor.element.isEqual(editor.range.focus.element)) {
96
+ return getMathformulaElement(editor.range.anchor.element)
97
+ }
98
+ const arr = dataRangeCaches.list.map(item => {
99
+ return getMathformulaElement(item.element)
100
+ })
101
+ let hasNull = arr.some(el => {
102
+ return el == null
103
+ })
104
+ //如果存在null,则表示有的选区元素不在公式元素下,返回null
105
+ if (hasNull) {
106
+ return null
107
+ }
108
+ //如果只有一个元素,则返回该元素
109
+ if (arr.length == 1) {
110
+ return arr[0]!
111
+ }
112
+ //默认数组中的元素都相等
113
+ let flag = true
114
+ for (let i = 1; i < arr.length; i++) {
115
+ if (!arr[i]!.isEqual(arr[0]!)) {
116
+ flag = false
117
+ break
118
+ }
119
+ }
120
+ //如果相等,则返回该元素
121
+ if (flag) {
122
+ return arr[0]
123
+ }
124
+ return null
125
+ }
126
+
127
+ /**
128
+ * 数学公式插件
129
+ * @param options
130
+ * @returns
131
+ */
132
+ export const mathformula = (options?: MathformulaOptionsType) => {
133
+ if (!DapCommon.isObject(options)) {
134
+ options = {}
135
+ }
136
+ const plugin: PluginType = (editifyInstance: ComponentInternalInstance, editTrans: (key: string) => any) => {
137
+ //是否禁用该插件按钮
138
+ let isDisabled: boolean = false
139
+ //如果光标范围内有链接、代码块则禁用
140
+ if (editifyInstance.exposed!.editor.value) {
141
+ isDisabled = hasPreInRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value) || hasLinkInRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value)
142
+ }
143
+ //数学公式文本框内置LaTex文本内容
144
+ let defaultLaTexContent: string = ''
145
+
146
+ return {
147
+ //插件名称
148
+ name: 'mathformula',
149
+ //菜单项配置
150
+ menu: {
151
+ sequence: options!.sequence || 101,
152
+ extraDisabled: (name: string) => {
153
+ //如果光标选区内有数学公式则禁用链接、图片、视频、表格和代码块菜单
154
+ if (name == 'link' || name == 'image' || name == 'video' || name == 'table' || name == 'codeBlock') {
155
+ return hasMathformulaInRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value)
156
+ }
157
+ return false
158
+ },
159
+ extend: {
160
+ type: 'select',
161
+ title: options!.title || editTrans('insertMathformula'),
162
+ leftBorder: options!.leftBorder,
163
+ rightBorder: options!.rightBorder,
164
+ hideScroll: true,
165
+ active: editifyInstance.exposed!.editor.value ? hasMathformulaInRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value) : false,
166
+ disabled: isDisabled || options!.disabled,
167
+ //浮层展开时触发的事件
168
+ onLayerShow() {
169
+ //获取选区所在的数学公式元素
170
+ const mathformulaElement = getMathformulaElementByRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value)
171
+ //如果该元素存在,则设置默认的LaTex文本内容
172
+ if (mathformulaElement) {
173
+ defaultLaTexContent = mathformulaElement.marks!['data-editify-mathformula'] || ''
174
+ }
175
+ },
176
+ default: () => h(Icon, { value: 'mathformula' }),
177
+ layer: (_name: string, btnInstance: InstanceType<typeof Button>) => {
178
+ return h(InsertMathformula, {
179
+ color: <string | null>editifyInstance.props.color,
180
+ defaultLaTexContent: defaultLaTexContent,
181
+ onInsert: (content: string) => {
182
+ //如果公式文本框有内容则进行下一步处理
183
+ if (content) {
184
+ //获取编辑器对象
185
+ const editor = <AlexEditor>editifyInstance.exposed!.editor.value
186
+ //获取选区所在的数学公式元素
187
+ const mathformulaElement = getMathformulaElementByRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value)
188
+ //如果在数学公式下
189
+ if (mathformulaElement) {
190
+ //清除该数学公式
191
+ mathformulaElement.toEmpty()
192
+ //移动光标到后一个元素上
193
+ editor.range!.anchor.moveToStart(editor.getNextElement(mathformulaElement)!)
194
+ editor.range!.focus.moveToStart(editor.getNextElement(mathformulaElement)!)
195
+ }
196
+ //定义转换后的mathml内容
197
+ let mathml: string = ''
198
+ try {
199
+ //获取转换后的mathml
200
+ mathml = KaTex.renderToString(content, {
201
+ output: 'mathml',
202
+ throwOnError: true
203
+ })
204
+ } catch (error) {
205
+ mathml = ''
206
+ if (typeof options!.handleError == 'function') {
207
+ options!.handleError(error as Error)
208
+ }
209
+ }
210
+ //如果mathml存在则表示数学公式渲染成功则插入到编辑器
211
+ if (mathml) {
212
+ //设置最终的html内容
213
+ const html = `<span data-editify-mathformula="${content}" contenteditable="false">${mathml}</span>`
214
+ //html内容转为元素数组
215
+ const elements = editor.parseHtml(html)
216
+ //插入编辑器
217
+ editor.insertElement(elements[0])
218
+ //移动光标到新插入的元素
219
+ editor.range!.anchor.moveToEnd(elements[0])
220
+ editor.range!.focus.moveToEnd(elements[0])
221
+ //渲染
222
+ editor.formatElementStack()
223
+ editor.domRender()
224
+ editor.rangeRender()
225
+ }
226
+ }
227
+ //关闭浮层
228
+ btnInstance.show = false
229
+ }
230
+ })
231
+ }
232
+ }
233
+ },
234
+ //额外保留的标签
235
+ extraKeepTags: ['math', 'mrow', 'mi', 'mo', 'mn', 'msup', 'msub', 'mfrac', 'msqrt', 'mroot', 'munder', 'mover', 'munderover', 'mtable', 'mtr', 'mtd', 'mtext', 'mspace', 'mmultiscripts', 'menclose', 'mglyph', 'maction', 'maligngroup', 'malignmark', 'mprescripts', 'none', 'mpadded', 'ms', 'mphantom', 'mstyle', 'merror', 'mscarries', 'mscarry', 'msline', 'msgroup', 'msrow', 'mscolumn', 'mstack', 'mlongdiv', 'mlabeledtr', 'mlabeledmultiscripts', 'semantics', 'msubsup'],
236
+ //粘贴保留的属性
237
+ pasteKeepMarks: el => {
238
+ let marks: ObjectType = {}
239
+ if (isMathformula(el) || isUnderMathformula(el)) {
240
+ marks = cloneData(el.marks!)
241
+ }
242
+ return marks
243
+ },
244
+ //粘贴保留的样式
245
+ pasteKeepStyles: el => {
246
+ let styles: ObjectType = {}
247
+ if (isMathformula(el) || isUnderMathformula(el)) {
248
+ styles = cloneData(el.styles!)
249
+ }
250
+ return styles
251
+ },
252
+ //node转元素的额外处理
253
+ customParseNode: (el: AlexElement) => {
254
+ if (el.parsedom == 'span' && el.hasMarks() && el.marks!['data-editify-mathformula']) {
255
+ AlexElement.flatElements(el.children!).forEach(item => {
256
+ //锁定元素防止合并
257
+ item.locked = true
258
+ //没有子元素的非文本元素设为自闭合元素
259
+ if (!item.isText() && !item.hasChildren()) {
260
+ item.type = 'closed'
261
+ }
262
+ })
263
+ }
264
+ return el
265
+ },
266
+ //自定义渲染规范
267
+ renderRule: (el: AlexElement) => {
268
+ //给元素设置两侧的空白字符
269
+ if (el.parsedom == 'span' && el.hasMarks() && el.marks!['data-editify-mathformula']) {
270
+ //获取editor对象
271
+ const editor = <AlexEditor>editifyInstance.exposed!.editor.value
272
+ //前一个元素
273
+ const previousElement = editor.getPreviousElement(el)
274
+ //后一个元素
275
+ const newTextElement = editor.getNextElement(el)
276
+ //如果不存在前一个元素或者前一个元素不是空白元素则设置空白元素
277
+ if (!previousElement || !previousElement.isSpaceText()) {
278
+ const spaceText = AlexElement.getSpaceElement()
279
+ editor.addElementBefore(spaceText, el)
280
+ }
281
+ //如果不存在后一个元素或者后一个元素不是空白元素则设置空白元素
282
+ if (!newTextElement || !newTextElement.isSpaceText()) {
283
+ const spaceText = AlexElement.getSpaceElement()
284
+ editor.addElementAfter(spaceText, el)
285
+ }
286
+ //如果光标在元素上则更新光标位置
287
+ if (editor.range && el.isContains(editor.range.anchor.element)) {
288
+ editor.range.anchor.moveToEnd(editor.getNextElement(el)!)
289
+ }
290
+ if (editor.range && el.isContains(editor.range.focus.element)) {
291
+ editor.range.focus.moveToEnd(editor.getNextElement(el)!)
292
+ }
293
+ }
294
+ }
295
+ }
296
+ }
297
+ return plugin
298
+ }
@@ -0,0 +1,64 @@
1
+ .editify-mathformula {
2
+ display: block;
3
+ width: 320px;
4
+ padding: 10px 14px;
5
+
6
+ .editify-mathformula-label {
7
+ display: block;
8
+ text-align: left;
9
+ margin-bottom: 10px;
10
+ font-size: @font-size;
11
+ color: @font-color;
12
+ }
13
+
14
+ .editify-mathformula-textarea {
15
+ display: block;
16
+ appearance: none;
17
+ -webkit-appearance: none;
18
+ -moz-appearance: none;
19
+ border: 1px solid @border-color;
20
+ width: 100%;
21
+ height: 100px;
22
+ overflow-x: hidden;
23
+ overflow-y: auto;
24
+ border-radius: 2px;
25
+ margin: 0;
26
+ padding: 6px;
27
+ font-size: @font-size;
28
+ color: @font-color;
29
+ line-height: 1.5;
30
+ transition: border-color 500ms;
31
+ background-color: transparent;
32
+ outline: none;
33
+ box-sizing: border-box;
34
+ resize: none;
35
+ font-family: Consolas, monospace, Monaco, Andale Mono, Ubuntu Mono;
36
+
37
+ &::-webkit-input-placeholder,
38
+ &::placeholder {
39
+ color: @font-color-disabled;
40
+ font-family: inherit;
41
+ font-size: inherit;
42
+ vertical-align: middle;
43
+ }
44
+ }
45
+
46
+ .editify-mathformula-footer {
47
+ display: flex;
48
+ justify-content: flex-end;
49
+ align-items: center;
50
+ width: 100%;
51
+ margin-top: 10px;
52
+
53
+ span {
54
+ cursor: pointer;
55
+ opacity: 0.8;
56
+ transition: all 200ms;
57
+ font-size: @font-size;
58
+
59
+ &:hover {
60
+ opacity: 1;
61
+ }
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,50 @@
1
+ <template>
2
+ <div class="editify-mathformula">
3
+ <div class="editify-mathformula-label">{{ $editTrans('insertMathformula') }}</div>
4
+ <textarea class="editify-mathformula-textarea" v-model.trim="latexContent" :placeholder="$editTrans('mathformulaPlaceholder')" @focus="handleInputFocus" @blur="handleInputBlur"></textarea>
5
+ <div class="editify-mathformula-footer">
6
+ <span :style="{ color: color || '' }" @click="insertMathformula">{{ $editTrans('confirm') }}</span>
7
+ </div>
8
+ </div>
9
+ </template>
10
+ <script setup lang="ts">
11
+ import { inject, ref, watch } from 'vue'
12
+ import { InsertMathformulaProps } from './props'
13
+
14
+ defineOptions({
15
+ name: 'InsertMathformula'
16
+ })
17
+ const props = defineProps(InsertMathformulaProps)
18
+ const emits = defineEmits(['insert'])
19
+
20
+ const $editTrans = inject<(key: string) => any>('$editTrans')!
21
+
22
+ const latexContent = ref<string>('')
23
+
24
+ //输入框获取焦点
25
+ const handleInputFocus = (e: Event) => {
26
+ if (props.color) {
27
+ ;(e.currentTarget as HTMLInputElement).style.borderColor = props.color
28
+ }
29
+ }
30
+ //输入框失去焦点
31
+ const handleInputBlur = (e: Event) => {
32
+ ;(e.currentTarget as HTMLInputElement).style.borderColor = ''
33
+ }
34
+
35
+ //插入数学公式
36
+ const insertMathformula = () => {
37
+ emits('insert', latexContent.value)
38
+ }
39
+
40
+ watch(
41
+ () => props.defaultLaTexContent,
42
+ newVal => {
43
+ latexContent.value = newVal
44
+ },
45
+ {
46
+ immediate: true
47
+ }
48
+ )
49
+ </script>
50
+ <style scoped src="./insertMathformula.less"></style>
@@ -0,0 +1,16 @@
1
+ import { ExtractPublicPropTypes, PropType } from 'vue'
2
+
3
+ export const InsertMathformulaProps = {
4
+ //主题色
5
+ color: {
6
+ type: String as PropType<string | null>,
7
+ default: ''
8
+ },
9
+ //预置的LaTex文本内容
10
+ defaultLaTexContent: {
11
+ type: String,
12
+ default: ''
13
+ }
14
+ }
15
+
16
+ export type InsertMathformulaPropsType = ExtractPublicPropTypes<typeof InsertMathformulaProps>