@fastkit/vui 0.7.56 → 0.7.61

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.
@@ -1,5 +1,12 @@
1
1
  import './VWysiwygEditor.scss';
2
- import { defineComponent, Fragment, PropType, computed, ref, watch } from 'vue';
2
+ import {
3
+ defineComponent,
4
+ PropType,
5
+ computed,
6
+ ref,
7
+ watch,
8
+ onBeforeUnmount,
9
+ } from 'vue';
3
10
  import {
4
11
  createFormControlProps,
5
12
  FormControlSlots,
@@ -34,10 +41,15 @@ import {
34
41
  import StarterKit from '@tiptap/starter-kit';
35
42
  import { VButton, VButtonGroup } from '../VButton';
36
43
  import {
44
+ RawWysiwygExtension,
45
+ resolveRawWysiwygExtensions,
37
46
  RawWysiwygEditorTool,
38
47
  resolveRawWysiwygEditorTools,
39
48
  WysiwygEditorContext,
49
+ // EditorEventsOptions,
50
+ WysiwygEditorInitializeContext,
40
51
  } from './schemes';
52
+ // import { Linter, BadWords, Punctuation, HeadingLevel } from './extensions';
41
53
 
42
54
  export const VWysiwygEditor = defineComponent({
43
55
  name: 'VWysiwygEditor',
@@ -49,6 +61,10 @@ export const VWysiwygEditor = defineComponent({
49
61
  ...createControlFieldProviderProps(),
50
62
  ...createControlProps(),
51
63
  ...defineSlotsProps<FormControlSlots & InputBoxSlots>(),
64
+ extensions: {
65
+ type: Array as PropType<RawWysiwygExtension[]>,
66
+ default: () => [],
67
+ },
52
68
  tools: {
53
69
  type: Array as PropType<RawWysiwygEditorTool[]>,
54
70
  default: () => [],
@@ -78,6 +94,23 @@ export const VWysiwygEditor = defineComponent({
78
94
  validationValue: () => textRef.value,
79
95
  });
80
96
 
97
+ const initializeCtx = new WysiwygEditorInitializeContext({
98
+ onCreate: ({ editor }) => {
99
+ textRef.value = editor.getText();
100
+ updateEditable();
101
+ },
102
+ onFocus: ({ event }) => {
103
+ inputControl.focusHandler(event);
104
+ },
105
+ onBlur: ({ event }) => {
106
+ inputControl.blurHandler(event);
107
+ },
108
+ onUpdate: ({ editor }) => {
109
+ inputControl.value = editor.getHTML();
110
+ textRef.value = editor.getText();
111
+ },
112
+ });
113
+
81
114
  const extensions = [
82
115
  StarterKit.configure({
83
116
  bold: false,
@@ -86,6 +119,10 @@ export const VWysiwygEditor = defineComponent({
86
119
  history: false,
87
120
  italic: false,
88
121
  }),
122
+ // Linter.configure({
123
+ // plugins: [BadWords(['abc', 'evidently']), Punctuation, HeadingLevel],
124
+ // }),
125
+ ...resolveRawWysiwygExtensions(props.extensions, initializeCtx),
89
126
  ...wysiwygSettings.value.extensions,
90
127
  ];
91
128
 
@@ -110,21 +147,13 @@ export const VWysiwygEditor = defineComponent({
110
147
  },
111
148
  );
112
149
 
150
+ initializeCtx.on('create', ({ editor }) => {
151
+ textRef.value = editor.getText();
152
+ updateEditable();
153
+ });
154
+
113
155
  const editor = useEditor({
114
- onCreate: ({ editor }) => {
115
- textRef.value = editor.getText();
116
- updateEditable();
117
- },
118
- onFocus: (ctx) => {
119
- inputControl.focusHandler(ctx.event);
120
- },
121
- onBlur: (ctx) => {
122
- inputControl.blurHandler(ctx.event);
123
- },
124
- onUpdate: (ev) => {
125
- inputControl.value = ev.editor.getHTML();
126
- textRef.value = ev.editor.getText();
127
- },
156
+ ...initializeCtx.editorOptions(),
128
157
  autofocus: props.autofocus,
129
158
  content: props.modelValue,
130
159
  extensions,
@@ -214,6 +243,10 @@ export const VWysiwygEditor = defineComponent({
214
243
  );
215
244
  };
216
245
 
246
+ onBeforeUnmount(() => {
247
+ editor.value && editor.value.destroy();
248
+ });
249
+
217
250
  return {
218
251
  editor,
219
252
  ...inputControl.expose(),
@@ -253,40 +286,38 @@ export const VWysiwygEditor = defineComponent({
253
286
  return (
254
287
  <div class="v-wysiwyg-editor__wrapper">
255
288
  {!this.isReadonly && this.createTools()}
256
- <div class="v-wysiwyg-editor__body">
257
- <VControlField
258
- class="v-wysiwyg-editor__input"
259
- autoHeight
260
- startAdornment={this.startAdornment}
261
- endAdornment={this.endAdornment}
262
- size={this.size}
263
- // onClickHost={(ev) => {
264
- // this.focus();
265
- // }}
266
- v-slots={{
267
- ...this.$slots,
268
- default: () => {
269
- return (
270
- <Fragment>
271
- <EditorContent
272
- class="v-wysiwyg-editor__input__element wysiwyg"
273
- editor={editor}
274
- />
275
- {!this.floatingToolbar &&
276
- !!editor &&
277
- !this.isReadonly && (
278
- <BubbleMenu
279
- class="v-wysiwyg-editor__bubble-menu"
280
- editor={editor}>
281
- {this.createTools(true)}
282
- </BubbleMenu>
283
- )}
284
- </Fragment>
285
- );
286
- },
287
- }}
288
- />
289
- </div>
289
+ <VControlField
290
+ class="v-wysiwyg-editor__input"
291
+ autoHeight
292
+ startAdornment={this.startAdornment}
293
+ endAdornment={this.endAdornment}
294
+ size={this.size}
295
+ // onClickHost={(ev) => {
296
+ // this.focus();
297
+ // }}
298
+ v-slots={{
299
+ ...this.$slots,
300
+ default: () => {
301
+ return (
302
+ <div class="v-wysiwyg-editor__body">
303
+ <EditorContent
304
+ class="v-wysiwyg-editor__input__element wysiwyg"
305
+ editor={editor}
306
+ />
307
+ {!this.floatingToolbar &&
308
+ !!editor &&
309
+ !this.isReadonly && (
310
+ <BubbleMenu
311
+ class="v-wysiwyg-editor__bubble-menu"
312
+ editor={editor}>
313
+ {this.createTools(true)}
314
+ </BubbleMenu>
315
+ )}
316
+ </div>
317
+ );
318
+ },
319
+ }}
320
+ />
290
321
  </div>
291
322
  );
292
323
  },
@@ -0,0 +1 @@
1
+ export * from './linter';
@@ -0,0 +1,75 @@
1
+ .v-linter {
2
+ &__problem {
3
+ margin-bottom: -1px;
4
+ cursor: help;
5
+ background: var(--deep-color);
6
+ border-bottom: 1px solid var(--main-color);
7
+ }
8
+
9
+ &__issue-icon {
10
+ position: absolute;
11
+ right: 2px;
12
+ display: inline-flex;
13
+ align-items: center;
14
+ justify-content: center;
15
+ width: 1.2em;
16
+ height: 1.2em;
17
+ margin-top: 0.2em;
18
+ // padding-left: 0.5px;
19
+ font-family: times, georgia, serif;
20
+ font-size: 15px;
21
+ font-weight: 700;
22
+ line-height: 1;
23
+ color: var(--text-color);
24
+ text-align: center;
25
+ cursor: pointer;
26
+ background: var(--main-color);
27
+ border-radius: 100px;
28
+
29
+ &::before {
30
+ content: '!';
31
+ }
32
+ }
33
+
34
+ &__issue-menu-wrapper {
35
+ position: relative;
36
+ display: inline-block;
37
+ width: 0px;
38
+ height: 1em;
39
+ overflow: visible;
40
+ }
41
+
42
+ &__issue-menu {
43
+ position: absolute;
44
+ top: calc(100% + 8px);
45
+ left: 0;
46
+ min-width: 200px;
47
+ max-width: calc(100vw - 40px);
48
+ padding: 8px;
49
+ // font-size: 12px;
50
+ font-size: 87.5%;
51
+ line-height: 1.5;
52
+ pointer-events: none;
53
+ background: #fff;
54
+ border-radius: 4px;
55
+ opacity: 0;
56
+ transition: opacity 0.15s;
57
+
58
+ &--active {
59
+ pointer-events: auto;
60
+ opacity: 1;
61
+ }
62
+
63
+ &__fix {
64
+ display: inline;
65
+ margin-top: 8px;
66
+ color: var(--palette-link);
67
+ text-decoration: underline;
68
+ cursor: pointer;
69
+ background: transparent;
70
+ border: 0;
71
+ outline: 0;
72
+ appearance: none;
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,262 @@
1
+ import './Linter.scss';
2
+
3
+ import { Extension } from '@tiptap/core';
4
+ import { Decoration, DecorationSet } from 'prosemirror-view';
5
+ import { Plugin, PluginKey, TextSelection } from 'prosemirror-state';
6
+ import { Node as ProsemirrorNode } from 'prosemirror-model';
7
+ import { LinterPlugin, LinterResult as Issue } from './LinterPlugin';
8
+ import { debounce } from '@fastkit/helpers';
9
+ import { EditorView } from 'prosemirror-view';
10
+
11
+ const PROBLEM_CLASS_NAME = 'v-linter__problem';
12
+ const ISSUE_ICON_CLASS_NAME = 'v-linter__issue-icon';
13
+ const ISSUE_MENU_CLASS_NAME = 'v-linter__issue-menu';
14
+
15
+ interface IssueElement extends HTMLDivElement {
16
+ issue?: Issue;
17
+ }
18
+
19
+ function renderIcon(view: EditorView<any>, issue: Issue) {
20
+ const { level = 'warning' } = issue;
21
+ const icon: IssueElement = document.createElement('div');
22
+
23
+ icon.className = `${ISSUE_ICON_CLASS_NAME} ${level}-scope`;
24
+ icon.title = issue.message;
25
+ icon.issue = issue;
26
+
27
+ return icon;
28
+ }
29
+
30
+ function renderMenu(view: EditorView<any>, issue: Issue) {
31
+ const menuWrapper = document.createElement('div');
32
+ menuWrapper.className = 'v-linter__issue-menu-wrapper';
33
+ const menu: IssueElement = document.createElement('div');
34
+
35
+ menu.className = `${ISSUE_MENU_CLASS_NAME} elevation-3`;
36
+ menu.innerText = issue.message;
37
+
38
+ const { fix, fixMessage } = issue;
39
+
40
+ if (fix && fixMessage) {
41
+ const $fix = document.createElement('button');
42
+ $fix.type = 'button';
43
+ $fix.innerHTML = fixMessage;
44
+ $fix.addEventListener('click', () => {
45
+ fix(view, issue);
46
+ });
47
+ $fix.className = `v-linter__issue-menu__fix`;
48
+ menu.appendChild($fix);
49
+ }
50
+
51
+ menu.issue = issue;
52
+
53
+ menuWrapper.appendChild(menu);
54
+ return menuWrapper;
55
+ }
56
+
57
+ function getIssueElement(
58
+ source: HTMLElement | EventTarget | null,
59
+ className: string,
60
+ ): IssueElement | undefined {
61
+ if (!source || !(source instanceof HTMLElement)) {
62
+ return;
63
+ }
64
+ if (source.classList.contains(className)) {
65
+ return source as IssueElement;
66
+ }
67
+ const el = source.closest(`.${className}`);
68
+ if (el) {
69
+ return el as IssueElement;
70
+ }
71
+ }
72
+
73
+ function getIconElement(
74
+ source: HTMLElement | EventTarget | null,
75
+ ): IssueElement | undefined {
76
+ return getIssueElement(source, ISSUE_ICON_CLASS_NAME);
77
+ }
78
+
79
+ function getMenuElement(
80
+ source: HTMLElement | EventTarget | null,
81
+ ): IssueElement | undefined {
82
+ return getIssueElement(source, ISSUE_MENU_CLASS_NAME);
83
+ }
84
+
85
+ function getProblemElement(
86
+ source: HTMLElement | EventTarget | null,
87
+ ): IssueElement | undefined {
88
+ return getIssueElement(source, PROBLEM_CLASS_NAME);
89
+ }
90
+
91
+ function runAllLinterPlugins(
92
+ doc: ProsemirrorNode,
93
+ plugins: Array<typeof LinterPlugin>,
94
+ ) {
95
+ const decorations: [any?] = [];
96
+
97
+ const results = plugins
98
+ .map((RegisteredLinterPlugin) => {
99
+ return new RegisteredLinterPlugin(doc).scan().getResults();
100
+ })
101
+ .flat();
102
+
103
+ results.forEach((issue) => {
104
+ const { level = 'warning' } = issue;
105
+ decorations.push(
106
+ Decoration.inline(issue.from, issue.to, {
107
+ class: `${PROBLEM_CLASS_NAME} ${level}-scope`,
108
+ 'data-issue': JSON.stringify(issue),
109
+ title: issue.message,
110
+ }),
111
+ Decoration.widget(issue.from, (view) => renderIcon(view, issue)),
112
+ Decoration.widget(issue.from, (view) => renderMenu(view, issue)),
113
+ );
114
+ });
115
+
116
+ return DecorationSet.create(doc, decorations);
117
+ }
118
+
119
+ export interface LinterOptions {
120
+ plugins: Array<typeof LinterPlugin>;
121
+ }
122
+
123
+ function updateMenusPosition(editorElement: Element) {
124
+ const { left, right } = editorElement.getBoundingClientRect();
125
+ const menus = Array.from(
126
+ editorElement.querySelectorAll(`.${ISSUE_MENU_CLASS_NAME}`),
127
+ );
128
+ menus.forEach((menu) => {
129
+ const { left: menuLeft, right: menuRight } = menu.getBoundingClientRect();
130
+ const overflow = menuRight - right;
131
+ let offset = 0;
132
+ if (overflow > 0) {
133
+ offset = -overflow;
134
+
135
+ if (menuLeft + offset < left) {
136
+ offset = 0;
137
+ }
138
+ }
139
+ (menu as HTMLElement).style.transform = `translateX(${offset}px)`;
140
+ });
141
+ }
142
+
143
+ const debouncedUpdateMenusPosition = debounce(updateMenusPosition, 250);
144
+
145
+ export const Linter = Extension.create<LinterOptions>({
146
+ name: 'linter',
147
+
148
+ addOptions() {
149
+ return {
150
+ plugins: [],
151
+ };
152
+ },
153
+
154
+ onCreate() {
155
+ const { dom } = this.editor.view;
156
+ debouncedUpdateMenusPosition(dom);
157
+ },
158
+
159
+ onUpdate() {
160
+ const { dom } = this.editor.view;
161
+ debouncedUpdateMenusPosition(dom);
162
+ },
163
+
164
+ addProseMirrorPlugins() {
165
+ const { plugins } = this.options;
166
+
167
+ return [
168
+ new Plugin({
169
+ key: new PluginKey('linter'),
170
+ state: {
171
+ init(_, { doc }) {
172
+ return runAllLinterPlugins(doc, plugins);
173
+ },
174
+ apply(transaction, oldState) {
175
+ return transaction.docChanged
176
+ ? runAllLinterPlugins(transaction.doc, plugins)
177
+ : oldState;
178
+ },
179
+ },
180
+ props: {
181
+ decorations(state) {
182
+ return this.getState(state);
183
+ },
184
+ handleClick(view, _, event) {
185
+ const activeMenus = Array.from(
186
+ view.dom.querySelectorAll(`.${ISSUE_MENU_CLASS_NAME}--active`),
187
+ );
188
+
189
+ activeMenus.forEach((menu) =>
190
+ menu.classList.remove(`${ISSUE_MENU_CLASS_NAME}--active`),
191
+ );
192
+
193
+ const problem = getProblemElement(event.target);
194
+
195
+ const issueString = problem && problem.dataset['issue'];
196
+
197
+ const issue = issueString && JSON.parse(issueString);
198
+ if (issue) {
199
+ const menuWrapper = problem.previousElementSibling;
200
+ const menu =
201
+ menuWrapper &&
202
+ menuWrapper.querySelector(`.${ISSUE_MENU_CLASS_NAME}`);
203
+ menu && menu.classList.add(`${ISSUE_MENU_CLASS_NAME}--active`);
204
+
205
+ const { from, to } = issue;
206
+ view.dispatch(
207
+ view.state.tr
208
+ .setSelection(TextSelection.create(view.state.doc, from, to))
209
+ .scrollIntoView(),
210
+ );
211
+
212
+ return true;
213
+ }
214
+
215
+ const menu = getMenuElement(event.target);
216
+
217
+ if (menu) {
218
+ menu.classList.add(`${ISSUE_MENU_CLASS_NAME}--active`);
219
+ }
220
+
221
+ const target = getIconElement(event.target);
222
+
223
+ if (target && target.issue) {
224
+ const menuWrapper = target.nextElementSibling;
225
+ const menu =
226
+ menuWrapper &&
227
+ menuWrapper.querySelector(`.${ISSUE_MENU_CLASS_NAME}`);
228
+ menu && menu.classList.add(`${ISSUE_MENU_CLASS_NAME}--active`);
229
+
230
+ const { from, to } = target.issue;
231
+
232
+ view.dispatch(
233
+ view.state.tr
234
+ .setSelection(TextSelection.create(view.state.doc, from, to))
235
+ .scrollIntoView(),
236
+ );
237
+
238
+ return true;
239
+ }
240
+
241
+ return false;
242
+ },
243
+ handleDoubleClick(view, _, event) {
244
+ const target = getIconElement(event.target);
245
+
246
+ if (target && target.issue) {
247
+ const prob = target.issue;
248
+
249
+ if (prob.fix) {
250
+ prob.fix(view, prob);
251
+ view.focus();
252
+ return true;
253
+ }
254
+ }
255
+
256
+ return false;
257
+ },
258
+ },
259
+ }),
260
+ ];
261
+ },
262
+ });
@@ -0,0 +1,44 @@
1
+ import { Node as ProsemirrorNode } from 'prosemirror-model';
2
+ import { EditorView } from 'prosemirror-view';
3
+
4
+ export type FixFn = (view: EditorView, issue: LinterResult) => any;
5
+
6
+ export type LinterResultLevel = 'warning' | 'error';
7
+
8
+ export interface LinterResult {
9
+ level: LinterResultLevel;
10
+ message: string;
11
+ from: number;
12
+ to: number;
13
+ fix?: FixFn;
14
+ fixMessage?: string;
15
+ }
16
+
17
+ export interface RawLinterResult extends Omit<LinterResult, 'level'> {
18
+ level?: LinterResultLevel;
19
+ }
20
+
21
+ export class LinterPlugin {
22
+ protected doc: ProsemirrorNode;
23
+
24
+ private results: Array<LinterResult> = [];
25
+
26
+ constructor(doc: ProsemirrorNode) {
27
+ this.doc = doc;
28
+ }
29
+
30
+ record(result: RawLinterResult) {
31
+ this.results.push({
32
+ level: 'error',
33
+ ...result,
34
+ });
35
+ }
36
+
37
+ scan() {
38
+ return this;
39
+ }
40
+
41
+ getResults() {
42
+ return this.results;
43
+ }
44
+ }
@@ -0,0 +1,5 @@
1
+ export { Linter } from './Linter';
2
+ export { LinterPlugin } from './LinterPlugin';
3
+ export type { LinterOptions } from './Linter';
4
+
5
+ export * from './plugins';
@@ -0,0 +1,34 @@
1
+ import { LinterPlugin } from '../LinterPlugin';
2
+
3
+ export function BadWords(words: string[]): typeof LinterPlugin {
4
+ const regex = new RegExp(`\\b(${words.join('|')})\\b`);
5
+
6
+ return class BadWords extends LinterPlugin {
7
+ scan() {
8
+ this.doc.descendants((node: any, position: number) => {
9
+ if (!node.isText) {
10
+ return;
11
+ }
12
+
13
+ const matches = regex.exec(node.text);
14
+
15
+ if (matches) {
16
+ const fixValue = matches[0] + '!!!!!';
17
+
18
+ this.record({
19
+ level: 'warning',
20
+ message: `Try not to say '${matches[0]}'`,
21
+ from: position + matches.index,
22
+ to: position + matches.index + matches[0].length,
23
+ fix: () => {
24
+ console.log('hoge');
25
+ },
26
+ fixMessage: `「${fixValue}」に修正する。`,
27
+ });
28
+ }
29
+ });
30
+
31
+ return this;
32
+ }
33
+ };
34
+ }
@@ -0,0 +1,33 @@
1
+ import { EditorView } from 'prosemirror-view';
2
+ import { LinterPlugin, LinterResult as Issue } from '../LinterPlugin';
3
+
4
+ export class HeadingLevel extends LinterPlugin {
5
+ fixHeader(level: number) {
6
+ return function ({ state, dispatch }: EditorView, issue: Issue) {
7
+ dispatch(state.tr.setNodeMarkup(issue.from - 1, undefined, { level }));
8
+ };
9
+ }
10
+
11
+ scan() {
12
+ let lastHeadLevel: number | null = null;
13
+
14
+ this.doc.descendants((node, position) => {
15
+ if (node.type.name === 'heading') {
16
+ // Check whether heading levels fit under the current level
17
+ const { level } = node.attrs;
18
+
19
+ if (lastHeadLevel != null && level > lastHeadLevel + 1) {
20
+ this.record({
21
+ message: `Heading too small (${level} under ${lastHeadLevel})`,
22
+ from: position + 1,
23
+ to: position + 1 + node.content.size,
24
+ fix: this.fixHeader(lastHeadLevel + 1),
25
+ });
26
+ }
27
+ lastHeadLevel = level;
28
+ }
29
+ });
30
+
31
+ return this;
32
+ }
33
+ }
@@ -0,0 +1,43 @@
1
+ import { EditorView } from 'prosemirror-view';
2
+ import { LinterPlugin, LinterResult as Issue } from '../LinterPlugin';
3
+
4
+ export class Punctuation extends LinterPlugin {
5
+ public regex = / ([,.!?:]) ?/g;
6
+
7
+ fix(replacement: any) {
8
+ return function ({ state, dispatch }: EditorView, issue: Issue) {
9
+ dispatch(
10
+ state.tr.replaceWith(
11
+ issue.from,
12
+ issue.to,
13
+ state.schema.text(replacement),
14
+ ),
15
+ );
16
+ };
17
+ }
18
+
19
+ scan() {
20
+ this.doc.descendants((node, position) => {
21
+ if (!node.isText) {
22
+ return;
23
+ }
24
+
25
+ if (!node.text) {
26
+ return;
27
+ }
28
+
29
+ const matches = this.regex.exec(node.text);
30
+
31
+ if (matches) {
32
+ this.record({
33
+ message: 'Suspicious spacing around punctuation',
34
+ from: position + matches.index,
35
+ to: position + matches.index + matches[0].length,
36
+ fix: this.fix(`${matches[1]} `),
37
+ });
38
+ }
39
+ });
40
+
41
+ return this;
42
+ }
43
+ }
@@ -0,0 +1,3 @@
1
+ export { BadWords } from './BadWords';
2
+ export { HeadingLevel } from './HeadingLevel';
3
+ export { Punctuation } from './Punctuation';