goodteditor-ui 1.0.17 → 1.0.18

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.
Files changed (46) hide show
  1. package/index.js +2 -0
  2. package/package.json +15 -2
  3. package/src/components/ui/WysiwygEditor/WysiwygEditor.d.ts +119 -0
  4. package/src/components/ui/WysiwygEditor/constants.js +209 -0
  5. package/src/components/ui/WysiwygEditor/extensions/blockquote.js +15 -0
  6. package/src/components/ui/WysiwygEditor/extensions/bold.js +15 -0
  7. package/src/components/ui/WysiwygEditor/extensions/bullet-list.js +15 -0
  8. package/src/components/ui/WysiwygEditor/extensions/code-block.js +13 -0
  9. package/src/components/ui/WysiwygEditor/extensions/code.js +13 -0
  10. package/src/components/ui/WysiwygEditor/extensions/font-size.js +34 -0
  11. package/src/components/ui/WysiwygEditor/extensions/formatting.js +14 -0
  12. package/src/components/ui/WysiwygEditor/extensions/heading.js +13 -0
  13. package/src/components/ui/WysiwygEditor/extensions/horizontal-rule.js +15 -0
  14. package/src/components/ui/WysiwygEditor/extensions/image.js +15 -0
  15. package/src/components/ui/WysiwygEditor/extensions/index.d.ts +32 -0
  16. package/src/components/ui/WysiwygEditor/extensions/index.js +31 -0
  17. package/src/components/ui/WysiwygEditor/extensions/italic.js +15 -0
  18. package/src/components/ui/WysiwygEditor/extensions/link.js +16 -0
  19. package/src/components/ui/WysiwygEditor/extensions/list-item.js +15 -0
  20. package/src/components/ui/WysiwygEditor/extensions/ordered-list.js +15 -0
  21. package/src/components/ui/WysiwygEditor/extensions/paragraph.js +23 -0
  22. package/src/components/ui/WysiwygEditor/extensions/strike.js +15 -0
  23. package/src/components/ui/WysiwygEditor/extensions/table-cell.js +13 -0
  24. package/src/components/ui/WysiwygEditor/extensions/table-header.js +15 -0
  25. package/src/components/ui/WysiwygEditor/extensions/table-row.js +15 -0
  26. package/src/components/ui/WysiwygEditor/extensions/table.js +29 -0
  27. package/src/components/ui/WysiwygEditor/extensions/text-align.js +5 -0
  28. package/src/components/ui/WysiwygEditor/extensions/text-style.js +15 -0
  29. package/src/components/ui/WysiwygEditor/extensions/underline.js +15 -0
  30. package/src/components/ui/WysiwygEditor/index.d.ts +4 -0
  31. package/src/components/ui/WysiwygEditor/index.js +4 -0
  32. package/src/components/ui/WysiwygEditor/renders/Button.vue +26 -0
  33. package/src/components/ui/WysiwygEditor/renders/ColorPicker.vue +42 -0
  34. package/src/components/ui/WysiwygEditor/renders/InputAuto.vue +33 -0
  35. package/src/components/ui/WysiwygEditor/renders/InputBrowse.vue +35 -0
  36. package/src/components/ui/WysiwygEditor/renders/InputUnits.vue +37 -0
  37. package/src/components/ui/WysiwygEditor/renders/Select.vue +45 -0
  38. package/src/components/ui/WysiwygEditor/renders/ToolbarPopover.vue +50 -0
  39. package/src/components/ui/WysiwygEditor/renders/index.d.ts +7 -0
  40. package/src/components/ui/WysiwygEditor/renders/index.js +7 -0
  41. package/src/components/ui/WysiwygEditor/renders/mixins/RenderMixin.js +39 -0
  42. package/src/components/ui/WysiwygEditor/renders/mixins/index.js +1 -0
  43. package/src/components/ui/WysiwygEditor/tools-and-commands.js +709 -0
  44. package/src/components/ui/WysiwygEditor/utils.js +72 -0
  45. package/src/components/ui/WysiwygEditor.md +18 -0
  46. package/src/components/ui/WysiwygEditor.vue +236 -0
@@ -0,0 +1,72 @@
1
+ import { ToolsMap } from './tools-and-commands';
2
+
3
+ /**
4
+ * @typedef {import('./WysiwygEditor').ToolDef} Tool
5
+ * @typedef {import('./WysiwygEditor').CommandDef} Command
6
+ */
7
+
8
+ /**
9
+ * @param {Tool | string} tool
10
+ * @param {string} toolName
11
+ * @return boolean
12
+ */
13
+ const findTool = (tool, toolName) => {
14
+ if (typeof tool === 'string') {
15
+ return toolName === tool;
16
+ }
17
+
18
+ return tool?.name === toolName;
19
+ };
20
+
21
+ /**
22
+ * @param {Tool[][]} tools
23
+ * @return Tool[][]
24
+ */
25
+ export const buildToolGroups = (tools) => {
26
+ const toolsFlatted = tools.flat();
27
+
28
+ if (toolsFlatted.length === 0) {
29
+ return [Object.values(ToolsMap)];
30
+ }
31
+
32
+ const availableTools = Object
33
+ .values(ToolsMap)
34
+ .filter(({ name }) => toolsFlatted.some((tool) => findTool(tool, name)));
35
+
36
+ /**
37
+ * @param {Tool[]} toolsGroup
38
+ * @return Tool[]
39
+ */
40
+ const populateToolsGroup = (toolsGroup) =>
41
+ toolsGroup.reduce((acc, tool) => {
42
+ const { name: toolName = tool, ...rest } = typeof tool === 'string' ? {} : tool;
43
+ const foundTool = availableTools.find(({ name }) => findTool(tool, name));
44
+
45
+ return [...acc, { ...foundTool, ...rest }];
46
+ }, []);
47
+
48
+ return tools.reduce((acc, toolsGroup) => [...acc, populateToolsGroup(toolsGroup)], []);
49
+ };
50
+
51
+ /**
52
+ * @param {Tool|Command} value
53
+ * @param {Record<string, any>} context
54
+ * @return {Tool|Command}
55
+ */
56
+ export const bindContext = (value, context) => {
57
+ if (value == null || typeof value !== 'object') {
58
+ return value;
59
+ }
60
+
61
+ return Object.entries(value).reduce((acc, [propName, propValue]) => {
62
+ if (typeof propValue === 'function') {
63
+ return { ...acc, [propName]: propValue.bind(context) };
64
+ }
65
+
66
+ if (Array.isArray(propValue)) {
67
+ return { ...acc, [propName]: propValue.map((childProp) => bindContext(childProp, context)) };
68
+ }
69
+
70
+ return { ...acc, [propName]: propValue };
71
+ }, {});
72
+ }
@@ -0,0 +1,18 @@
1
+ ```vue
2
+ <template>
3
+ <div class="pad-l5">
4
+ <ui-wysiwyg-editor v-model="model"></ui-wysiwyg-editor>
5
+ </div>
6
+ </template>
7
+ <script>
8
+ import UiWysiwygEditor from './WysiwygEditor.vue';
9
+ export default {
10
+ components: { UiWysiwygEditor },
11
+ data: () => ({
12
+ model: `<p>This WYSIWYG is based on
13
+ <a target="_blank" rel="noopener noreferrer nofollow" href="https://tiptap.dev/" class="color-link">tiptap</a>
14
+ editor framework.</p>`
15
+ })
16
+ };
17
+ </script>
18
+ ```
@@ -0,0 +1,236 @@
1
+ <template>
2
+ <grid class="editor" v-bind="$options.static.GridProps">
3
+ <template #editor-toolbar="{ style }">
4
+ <div :style="style" class="editor-toolbar">
5
+ <slot
6
+ v-if="editor != null"
7
+ name="toolbar"
8
+ v-bind="{ toolGroups, resolveGroupTitle, buildToolBinds }">
9
+ <div class="row row-gap-l1">
10
+ <div
11
+ v-for="(toolsGroup, groupIndex) of toolGroups"
12
+ :key="groupIndex"
13
+ class="col col-vmid col-auto">
14
+ <div class="d-flex flex-col flex-v-center">
15
+ <div class="group-header">
16
+ <slot name="group-header" v-bind="{ groupIndex, resolveGroupTitle }">
17
+ <div class="text-small text-center">{{ resolveGroupTitle(groupIndex) }}</div>
18
+ </slot>
19
+ </div>
20
+ <div>
21
+ <div
22
+ v-for="tool of toolsGroup"
23
+ :key="tool.name"
24
+ class="tool d-inline-block">
25
+ <slot name="tool" v-bind="buildToolBinds(tool)">
26
+ <component :is="tool.render" :tool="buildToolBinds(tool)" />
27
+ </slot>
28
+ </div>
29
+ </div>
30
+ </div>
31
+ </div>
32
+ </div>
33
+ </slot>
34
+ </div>
35
+ </template>
36
+ <template #editor-content="{ style }">
37
+ <div class="editor-content pad-v-l1" :style="style">
38
+ <div class="h-100">
39
+ <editor-content :editor="editor" class="h-100"/>
40
+ </div>
41
+ </div>
42
+ </template>
43
+ </grid>
44
+ </template>
45
+
46
+ <script>
47
+ import { Editor, EditorContent } from '@tiptap/vue-2';
48
+
49
+ import { debounce } from './utils/Helpers';
50
+ import Grid from './Grid.vue';
51
+ import * as Extensions from './WysiwygEditor/extensions';
52
+ import { buildToolGroups, bindContext } from './WysiwygEditor/utils';
53
+ import { DefaultTools, DefaultToolGroupsTitles } from './WysiwygEditor/constants';
54
+
55
+ /**
56
+ * @typedef {import('./WysiwygEditor/WysiwygEditor').ToolDef} Tool
57
+ */
58
+
59
+ export default {
60
+ components: {
61
+ Grid,
62
+ EditorContent
63
+ },
64
+ props: {
65
+ /**
66
+ * @model
67
+ */
68
+ value: {
69
+ type: String,
70
+ default: '',
71
+ },
72
+ /**
73
+ * 2d matrix of using tools
74
+ */
75
+ tools: {
76
+ type: Array,
77
+ default: () => [],
78
+ validator: (tools) => tools.flat().every((tool) =>
79
+ typeof tool === 'string' || (tool != null && typeof tool === 'object' && Array.isArray(tool) === false)
80
+ )
81
+ },
82
+ /**
83
+ * editor tool groups titles
84
+ */
85
+ groupsTitles: {
86
+ type: Array,
87
+ default: () => [],
88
+ validator: (titles) => titles.every((title) => typeof title === 'string')
89
+ },
90
+ /**
91
+ * @public
92
+ */
93
+ getImageUrl: {
94
+ type: Function,
95
+ default: async () => {}
96
+ }
97
+ },
98
+ data: () => ({
99
+ /** @type {Editor} */
100
+ editor: null,
101
+ /** @type {Tool[][]} */
102
+ appliedTools: null,
103
+ /** @type {string[]} */
104
+ appliedGroupsTitles: null,
105
+ /** @type {number} */
106
+ caretPosition: 0
107
+ }),
108
+ computed: {
109
+ /**
110
+ * @return {string}
111
+ */
112
+ content() {
113
+ return this.editor?.getHTML() ?? '';
114
+ },
115
+ /**
116
+ * @return {Tool[][]}
117
+ */
118
+ toolGroups() {
119
+ return buildToolGroups(this.appliedTools);
120
+ }
121
+ },
122
+ static: {
123
+ GridProps: {
124
+ areas: [['editor-toolbar'], ['editor-content']],
125
+ rows: ['auto', '1fr'],
126
+ gap: ['0.5rem', 0]
127
+ }
128
+ },
129
+ watch: {
130
+ /**
131
+ * @param {string} value
132
+ */
133
+ value(value) {
134
+ const isSame = this.content === value;
135
+
136
+ if (isSame) {
137
+ return;
138
+ }
139
+
140
+ this.editor.commands.setContent(value, false);
141
+ },
142
+ },
143
+ created() {
144
+ this.onSelectionUpdateDebounced = debounce(this.onSelectionUpdate, 300);
145
+ this.appliedTools = this.tools.length === 0 ? DefaultTools : this.tools;
146
+ this.appliedGroupsTitles = this.groupsTitles.length === 0 ? DefaultToolGroupsTitles : this.groupsTitles;
147
+
148
+ this.editor = new Editor({
149
+ content: this.value,
150
+ extensions: Object.values(Extensions),
151
+ autofocus: 'end',
152
+ editorProps: {
153
+ attributes: {
154
+ class: 'pad-3 scroll-y',
155
+ style: 'min-height: 100%; height: 0;'
156
+ }
157
+ },
158
+ onUpdate: () => {
159
+ this.onInput();
160
+ this.onChange();
161
+ },
162
+ onSelectionUpdate: this.onSelectionUpdateDebounced
163
+ });
164
+ },
165
+ activated() {
166
+ this.editor.commands.focus(this.caretPosition);
167
+ },
168
+ beforeDestroy() {
169
+ this.editor?.destroy();
170
+ },
171
+ methods: {
172
+ onInput() {
173
+ this.$emit('input', this.content);
174
+ },
175
+ onChange() {
176
+ this.$emit('change', this.content);
177
+ },
178
+ /**
179
+ * @param {Transaction} transaction
180
+ */
181
+ onSelectionUpdate({ transaction }) {
182
+ this.caretPosition = transaction.curSelection.to;
183
+ },
184
+ /**
185
+ * @param {Tool} tool
186
+ * @return Tool
187
+ */
188
+ buildToolBinds(tool) {
189
+ const { editor, toolGroups } = this;
190
+
191
+ const context = Object.freeze({
192
+ get editor() {
193
+ return editor;
194
+ },
195
+ get tool() {
196
+ return tool;
197
+ },
198
+ get tools() {
199
+ return toolGroups.flat();
200
+ },
201
+ change: this.change,
202
+ getImageUrl: this.getImageUrl
203
+ });
204
+
205
+ return bindContext(tool, context);
206
+ },
207
+ /**
208
+ * @param {number} groupIndex
209
+ * @return string
210
+ */
211
+ resolveGroupTitle(groupIndex) {
212
+ const title = this.appliedGroupsTitles[groupIndex];
213
+ return typeof title === 'string' ? title : `Group ${groupIndex + 1}`;
214
+ }
215
+ }
216
+ }
217
+ </script>
218
+ <style lang="less" scoped>
219
+ .editor {
220
+ &-toolbar {}
221
+
222
+ &-content {
223
+ min-height: 7.5rem;
224
+ }
225
+ }
226
+
227
+ .group-header {
228
+ min-height: calc(var(--line-height)*1rem)
229
+ }
230
+
231
+ .tool {
232
+ &:not(:last-child) {
233
+ margin-right: 0.25rem;
234
+ }
235
+ }
236
+ </style>