vue-editify 0.1.37 → 0.1.39

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 +15154 -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 +31 -0
  31. package/lib/plugins/mathformula/insertMathformula/insertMathformula.vue.d.ts +18 -0
  32. package/lib/plugins/mathformula/insertMathformula/props.d.ts +9 -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 +1 -1
  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 +210 -0
  53. package/src/plugins/mathformula/insertMathformula/insertMathformula.less +64 -0
  54. package/src/plugins/mathformula/insertMathformula/insertMathformula.vue +40 -0
  55. package/src/plugins/mathformula/insertMathformula/props.ts +11 -0
@@ -0,0 +1,210 @@
1
+ import { common as DapCommon, element as DapElement } 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
+
26
+ /**
27
+ * 是否公式元素
28
+ * @param el
29
+ * @returns
30
+ */
31
+ export const isMathformula = (el: AlexElement) => {
32
+ return el.parsedom == 'span' && el.hasMarks() && el.marks!['data-editify-mathformula']
33
+ }
34
+
35
+ //是否在公式元素下
36
+ export const isUnderMathformula = (el: AlexElement): boolean => {
37
+ if (isMathformula(el)) {
38
+ return true
39
+ }
40
+ if (el.parent) {
41
+ return isUnderMathformula(el.parent)
42
+ }
43
+ return false
44
+ }
45
+
46
+ //获取公式元素
47
+ export const getMathformulaElement = (el: AlexElement): AlexElement | null => {
48
+ if (isMathformula(el)) {
49
+ return el
50
+ }
51
+ if (el.parent) {
52
+ return getMathformulaElement(el.parent)
53
+ }
54
+ return null
55
+ }
56
+
57
+ /**
58
+ * 选区是否含有公式元素
59
+ * @param editor
60
+ * @param dataRangeCaches
61
+ * @returns
62
+ */
63
+ export const hasMathformulaInRange = (editor: AlexEditor, dataRangeCaches: AlexElementsRangeType) => {
64
+ if (!editor.range) {
65
+ return false
66
+ }
67
+ if (editor.range.anchor.isEqual(editor.range.focus)) {
68
+ return isUnderMathformula(editor.range.anchor.element)
69
+ }
70
+ return dataRangeCaches.flatList.some(item => {
71
+ return isUnderMathformula(item.element)
72
+ })
73
+ }
74
+
75
+ /**
76
+ * 数学公式插件
77
+ * @param options
78
+ * @returns
79
+ */
80
+ export const mathformula = (options?: MathformulaOptionsType) => {
81
+ if (!DapCommon.isObject(options)) {
82
+ options = {}
83
+ }
84
+ const plugin: PluginType = (editifyInstance: ComponentInternalInstance, editTrans: (key: string) => any) => {
85
+ let isDisabled = false
86
+ //如果光标范围内有数学公式、链接、代码块则禁用
87
+ if (editifyInstance.exposed!.editor.value) {
88
+ isDisabled = hasMathformulaInRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value) || hasPreInRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value) || hasLinkInRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value)
89
+ }
90
+ return {
91
+ name: 'mathformula',
92
+ //菜单项配置
93
+ menu: {
94
+ sequence: options!.sequence || 101,
95
+ extraDisabled: (name: string) => {
96
+ //如果光标选区内有数学公式则禁用链接菜单、代码块菜单
97
+ if (name == 'link' || name == 'image' || name == 'video' || name == 'table' || name == 'codeBlock') {
98
+ return hasMathformulaInRange(editifyInstance.exposed!.editor.value, editifyInstance.exposed!.dataRangeCaches.value)
99
+ }
100
+ return false
101
+ },
102
+ extend: {
103
+ type: 'select',
104
+ title: options!.title || editTrans('insertMathformula'),
105
+ leftBorder: options!.leftBorder,
106
+ rightBorder: options!.rightBorder,
107
+ hideScroll: true,
108
+ active: false,
109
+ disabled: isDisabled || options!.disabled,
110
+ default: () => h(Icon, { value: 'mathformula' }),
111
+ layer: (_name: string, btnInstance: InstanceType<typeof Button>) => {
112
+ return h(InsertMathformula, {
113
+ color: <string | null>editifyInstance.props.color,
114
+ onInsert: (content: string) => {
115
+ if (content) {
116
+ //获取编辑器对象
117
+ const editor = <AlexEditor>editifyInstance.exposed!.editor.value
118
+ //渲染LaTex为mathml并转为dom
119
+ const dom = DapElement.string2dom(
120
+ KaTex.renderToString(content, {
121
+ output: 'mathml',
122
+ throwOnError: false
123
+ })
124
+ ) as HTMLElement
125
+ //设置最终的html内容
126
+ const html = `<span data-editify-mathformula="true" class="katex" >${dom.innerHTML}</span>`
127
+ //html内容转为元素数组
128
+ const elements = editor.parseHtml(html)
129
+ //插入编辑器
130
+ editor.insertElement(elements[0])
131
+ //移动光标到新插入的元素
132
+ editor.range!.anchor.moveToEnd(elements[0])
133
+ editor.range!.focus.moveToEnd(elements[0])
134
+ //渲染
135
+ editor.formatElementStack()
136
+ editor.domRender()
137
+ editor.rangeRender()
138
+ }
139
+ //关闭浮层
140
+ btnInstance.show = false
141
+ }
142
+ })
143
+ }
144
+ }
145
+ },
146
+ //额外保留的标签
147
+ 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'],
148
+ //粘贴保留的属性
149
+ pasteKeepMarks: el => {
150
+ let marks: ObjectType = {}
151
+ if (isMathformula(el) || isUnderMathformula(el)) {
152
+ marks = cloneData(el.marks!)
153
+ }
154
+ return marks
155
+ },
156
+ //粘贴保留的样式
157
+ pasteKeepStyles: el => {
158
+ let styles: ObjectType = {}
159
+ if (isMathformula(el) || isUnderMathformula(el)) {
160
+ styles = cloneData(el.styles!)
161
+ }
162
+ return styles
163
+ },
164
+ //node转元素的额外处理
165
+ customParseNode: (el: AlexElement) => {
166
+ if (el.parsedom == 'span' && el.hasMarks() && el.marks!['data-editify-mathformula']) {
167
+ AlexElement.flatElements(el.children!).forEach(item => {
168
+ //锁定元素防止合并
169
+ item.locked = true
170
+ //没有子元素的非文本元素设为自闭合元素
171
+ if (!item.isText() && !item.hasChildren()) {
172
+ item.type = 'closed'
173
+ }
174
+ })
175
+ }
176
+ return el
177
+ },
178
+ //自定义渲染规范
179
+ renderRule: (el: AlexElement) => {
180
+ //给元素设置两侧的空白字符
181
+ if (el.parsedom == 'span' && el.hasMarks() && el.marks!['data-editify-mathformula']) {
182
+ //获取editor对象
183
+ const editor = <AlexEditor>editifyInstance.exposed!.editor.value
184
+ //前一个元素
185
+ const previousElement = editor.getPreviousElement(el)
186
+ //后一个元素
187
+ const newTextElement = editor.getNextElement(el)
188
+ //如果不存在前一个元素或者前一个元素不是空白元素则设置空白元素
189
+ if (!previousElement || !previousElement.isSpaceText()) {
190
+ const spaceText = AlexElement.getSpaceElement()
191
+ editor.addElementBefore(spaceText, el)
192
+ }
193
+ //如果不存在后一个元素或者后一个元素不是空白元素则设置空白元素
194
+ if (!newTextElement || !newTextElement.isSpaceText()) {
195
+ const spaceText = AlexElement.getSpaceElement()
196
+ editor.addElementAfter(spaceText, el)
197
+ }
198
+ //如果光标在元素上则更新光标位置
199
+ if (editor.range && el.isContains(editor.range.anchor.element)) {
200
+ editor.range.anchor.moveToEnd(editor.getNextElement(el)!)
201
+ }
202
+ if (editor.range && el.isContains(editor.range.focus.element)) {
203
+ editor.range.focus.moveToEnd(editor.getNextElement(el)!)
204
+ }
205
+ }
206
+ }
207
+ }
208
+ }
209
+ return plugin
210
+ }
@@ -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,40 @@
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 } 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
+ </script>
40
+ <style scoped src="./insertMathformula.less"></style>
@@ -0,0 +1,11 @@
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
+ }
10
+
11
+ export type InsertMathformulaPropsType = ExtractPublicPropTypes<typeof InsertMathformulaProps>