@gravity-ui/markdown-editor 13.18.1 → 13.19.0

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![Markdown Editor](https://github.com/user-attachments/assets/40be2902-a683-4167-bd8d-ffd8bff26e69)
1
+ ![Markdown Editor](https://github.com/user-attachments/assets/0b4e5f65-54cf-475f-9c68-557a4e9edb46)
2
2
 
3
3
  # @gravity-ui/markdown-editor · [![npm package](https://img.shields.io/npm/v/@gravity-ui/markdown-editor)](https://www.npmjs.com/package/@gravity-ui/markdown-editor) [![CI](https://img.shields.io/github/actions/workflow/status/gravity-ui/markdown-editor/ci.yml?branch=main&label=CI)](https://github.com/gravity-ui/markdown-editor/actions/workflows/ci.yml?query=branch:main) [![Release](https://img.shields.io/github/actions/workflow/status/gravity-ui/markdown-editor/release.yml?branch=main&label=Release)](https://github.com/gravity-ui/markdown-editor/actions/workflows/release.yml?query=branch:main) [![storybook](https://img.shields.io/badge/Storybook-deployed-ff4685)](https://preview.gravity-ui.com/md-editor/)
4
4
 
@@ -57,6 +57,8 @@ export declare type MarkupConfig = {
57
57
  disabledExtensions?: CreateCodemirrorParams['disabledExtensions'];
58
58
  /** Additional keymaps for codemirror instance */
59
59
  keymaps?: CreateCodemirrorParams['keymaps'];
60
+ /** Overrides the default placeholder content. */
61
+ placeholder?: CreateCodemirrorParams['placeholder'];
60
62
  /**
61
63
  * Additional language data for markdown language in codemirror.
62
64
  * Can be used to configure additional autocompletions and others.
@@ -150,10 +150,11 @@ class EditorImpl extends event_emitter_1.SafeEventEmitter {
150
150
  return tslib_1.__classPrivateFieldGet(this, _EditorImpl_wysiwygEditor, "f");
151
151
  }
152
152
  get markupEditor() {
153
+ var _a;
153
154
  if (!tslib_1.__classPrivateFieldGet(this, _EditorImpl_markupEditor, "f")) {
154
155
  tslib_1.__classPrivateFieldSet(this, _EditorImpl_markupEditor, new editor_1.Editor((0, codemirror_1.createCodemirror)({
155
156
  doc: tslib_1.__classPrivateFieldGet(this, _EditorImpl_markup, "f"),
156
- placeholderText: (0, bundle_1.i18n)('markup_placeholder'),
157
+ placeholder: (_a = tslib_1.__classPrivateFieldGet(this, _EditorImpl_markupConfig, "f").placeholder) !== null && _a !== void 0 ? _a : (0, bundle_1.i18n)('markup_placeholder'),
157
158
  onCancel: () => this.emit('cancel', null),
158
159
  onSubmit: () => this.emit('submit', null),
159
160
  onChange: () => this.emit('rerender-toolbar', null),
@@ -195,15 +195,19 @@ class MarkdownSerializerState {
195
195
  let trailing = '';
196
196
  const progress = (node, _, index) => {
197
197
  let marks = node ? node.marks : [];
198
- // Remove marks from breaks (hard_break or soft_break) that are the last node inside
198
+ // Remove marks from breaks (hard_break or soft_break) that are the edge node inside
199
199
  // that mark to prevent parser edge cases with new lines just
200
- // before closing marks.
200
+ // before closing or after opening marks.
201
201
  if (node && node.type.spec.isBreak) {
202
202
  marks = marks.filter(m => {
203
+ if (index === 0)
204
+ return false;
203
205
  if (index + 1 == parent.childCount)
204
206
  return false;
207
+ const prev = parent.child(index - 1);
205
208
  const next = parent.child(index + 1);
206
- return m.isInSet(next.marks) && (!next.isText || /\S/.test(next.text));
209
+ return ((m.isInSet(prev.marks) && (!prev.isText || /\S/.test(prev.text))) &&
210
+ (m.isInSet(next.marks) && (!next.isText || /\S/.test(next.text))));
207
211
  });
208
212
  }
209
213
  let leading = trailing;
@@ -1,5 +1,4 @@
1
1
  import type { NodeType } from 'prosemirror-model';
2
2
  import type { Command } from 'prosemirror-state';
3
3
  export declare function toList(listType: NodeType): Command;
4
- export declare const liftIfCursorIsAtBeginningOfItem: Command;
5
4
  export declare const joinPrevList: Command;
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.joinPrevList = exports.liftIfCursorIsAtBeginningOfItem = exports.toList = void 0;
3
+ exports.joinPrevList = exports.toList = void 0;
4
4
  const prosemirror_schema_list_1 = require("prosemirror-schema-list");
5
5
  const join_1 = require("../../../commands/join");
6
- const selection_1 = require("../../../utils/selection");
7
6
  const utils_1 = require("./utils");
8
7
  function toList(listType) {
9
8
  return (state, dispatch) => {
@@ -18,18 +17,6 @@ function toList(listType) {
18
17
  };
19
18
  }
20
19
  exports.toList = toList;
21
- const liftIfCursorIsAtBeginningOfItem = (state, dispatch) => {
22
- const $cursor = (0, selection_1.get$CursorAtBlockStart)(state.selection);
23
- if (!$cursor)
24
- return false;
25
- const { schema } = state;
26
- const parentBlock = $cursor.node($cursor.depth - 1);
27
- if (parentBlock.firstChild === $cursor.parent && (0, utils_1.isListItemNode)(parentBlock)) {
28
- return (0, prosemirror_schema_list_1.liftListItem)((0, utils_1.liType)(schema))(state, dispatch);
29
- }
30
- return false;
31
- };
32
- exports.liftIfCursorIsAtBeginningOfItem = liftIfCursorIsAtBeginningOfItem;
33
20
  exports.joinPrevList = (0, join_1.joinPreviousBlock)({
34
21
  checkPrevNode: utils_1.isListNode,
35
22
  skipNode: utils_1.isListOrItemNode,
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Lists = exports.olType = exports.liType = exports.blType = exports.ListsAttr = exports.ListNode = void 0;
4
- const prosemirror_commands_1 = require("prosemirror-commands");
5
4
  const prosemirror_schema_list_1 = require("prosemirror-schema-list");
6
5
  const keymap_1 = require("../../../utils/keymap");
7
6
  const ListsSpecs_1 = require("./ListsSpecs");
@@ -29,7 +28,7 @@ const Lists = (builder, opts) => {
29
28
  }, builder.Priority.High);
30
29
  builder.addKeymap(({ schema }) => ({
31
30
  Enter: (0, prosemirror_schema_list_1.splitListItem)((0, ListsSpecs_1.liType)(schema)),
32
- Backspace: (0, prosemirror_commands_1.chainCommands)(commands_1.liftIfCursorIsAtBeginningOfItem, commands_1.joinPrevList),
31
+ Backspace: commands_1.joinPrevList,
33
32
  }), builder.Priority.Low);
34
33
  builder.use(inputrules_1.ListsInputRulesExtension, { bulletListInputRule: opts === null || opts === void 0 ? void 0 : opts.ulInputRules });
35
34
  builder.addPlugin(MergeListsPlugin_1.mergeListsPlugin);
@@ -21,11 +21,15 @@ const mergeListsPlugin = () => new prosemirror_state_1.Plugin({
21
21
  });
22
22
  exports.mergeListsPlugin = mergeListsPlugin;
23
23
  function mergeAdjacentNodesWithSameType(tr, nodes) {
24
- for (let i = nodes.length - 1; i > 0; i--) {
25
- const prev = nodes[i - 1];
26
- const next = nodes[i];
27
- if (prev.node.type === next.node.type && prev.pos + prev.node.nodeSize === next.pos) {
28
- tr.join(next.pos);
24
+ const posAfterMap = {};
25
+ for (const item of nodes) {
26
+ const posBefore = item.pos;
27
+ const posAfter = posBefore + item.node.nodeSize;
28
+ posAfterMap[posAfter] = item.node;
29
+ const nodeBefore = posAfterMap[posBefore];
30
+ if ((nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.type) === item.node.type) {
31
+ tr.join(tr.mapping.map(posBefore));
32
+ posAfterMap[posBefore] = undefined;
29
33
  }
30
34
  }
31
35
  }
@@ -67,10 +67,23 @@ exports.serializerTokens = {
67
67
  state.ensureNewLine();
68
68
  });
69
69
  },
70
- [const_1.YfmTableNode.Row]: (_state, node) => {
71
- throw new Error(`Should not serialize ${node.type.name} node via serialize-token`);
70
+ [const_1.YfmTableNode.Row]: (state, node) => {
71
+ console.warn(`Should not serialize ${node.type.name} node via serialize-token`);
72
+ state.write('||');
73
+ state.ensureNewLine();
74
+ state.write('\n');
75
+ state.renderContent(node);
76
+ state.write('||');
77
+ state.ensureNewLine();
72
78
  },
73
- [const_1.YfmTableNode.Cell]: (_state, node) => {
74
- throw new Error(`Should not serialize ${node.type.name} node via serialize-token`);
79
+ [const_1.YfmTableNode.Cell]: (state, node, parent) => {
80
+ console.warn(`Should not serialize ${node.type.name} node via serialize-token`);
81
+ state.renderContent(node);
82
+ const isLastCellInRow = parent.lastChild === node;
83
+ if (!isLastCellInRow) {
84
+ state.write('|');
85
+ state.ensureNewLine();
86
+ state.write('\n');
87
+ }
75
88
  },
76
89
  };
@@ -1,6 +1,6 @@
1
1
  import { autocompletion } from '@codemirror/autocomplete';
2
2
  import type { Extension, StateCommand } from '@codemirror/state';
3
- import { EditorView, EditorViewConfig, KeyBinding } from '@codemirror/view';
3
+ import { EditorView, EditorViewConfig, KeyBinding, placeholder } from '@codemirror/view';
4
4
  import { EventMap } from '../../bundle/Editor';
5
5
  import { ReactRenderStorage } from '../../extensions';
6
6
  import { Receiver } from '../../utils';
@@ -10,7 +10,7 @@ export type { YfmLangOptions };
10
10
  declare type Autocompletion = Parameters<typeof autocompletion>[0];
11
11
  export declare type CreateCodemirrorParams = {
12
12
  doc: EditorViewConfig['doc'];
13
- placeholderText: string;
13
+ placeholder: Parameters<typeof placeholder>[0];
14
14
  onCancel: () => void;
15
15
  onSubmit: () => void;
16
16
  onChange: () => void;
@@ -16,8 +16,8 @@ const react_facet_1 = require("./react-facet");
16
16
  const plugin_1 = require("./search-plugin/plugin");
17
17
  const yfm_1 = require("./yfm");
18
18
  function createCodemirror(params) {
19
- const { doc, placeholderText, reactRenderer, onCancel, onScroll, onSubmit, onChange, onDocChange, extensions: extraExtensions, disabledExtensions = {}, keymaps = [], receiver, yfmLangOptions, autocompletion: autocompletionConfig, } = params;
20
- const extensions = [gravity_1.gravityTheme, (0, view_1.placeholder)(placeholderText)];
19
+ const { doc, reactRenderer, onCancel, onScroll, onSubmit, onChange, onDocChange, disabledExtensions = {}, keymaps = [], receiver, yfmLangOptions, extensions: extraExtensions, placeholder: placeholderContent, autocompletion: autocompletionConfig, } = params;
20
+ const extensions = [gravity_1.gravityTheme, (0, view_1.placeholder)(placeholderContent)];
21
21
  if (!disabledExtensions.history) {
22
22
  extensions.push((0, commands_1.history)());
23
23
  }
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
4
  /** During build process, the current version will be injected here */
5
- exports.VERSION = typeof '13.18.1' !== 'undefined' ? '13.18.1' : 'unknown';
5
+ exports.VERSION = typeof '13.19.0' !== 'undefined' ? '13.19.0' : 'unknown';
@@ -57,6 +57,8 @@ export declare type MarkupConfig = {
57
57
  disabledExtensions?: CreateCodemirrorParams['disabledExtensions'];
58
58
  /** Additional keymaps for codemirror instance */
59
59
  keymaps?: CreateCodemirrorParams['keymaps'];
60
+ /** Overrides the default placeholder content. */
61
+ placeholder?: CreateCodemirrorParams['placeholder'];
60
62
  /**
61
63
  * Additional language data for markdown language in codemirror.
62
64
  * Can be used to configure additional autocompletions and others.
@@ -147,10 +147,11 @@ export class EditorImpl extends SafeEventEmitter {
147
147
  return __classPrivateFieldGet(this, _EditorImpl_wysiwygEditor, "f");
148
148
  }
149
149
  get markupEditor() {
150
+ var _a;
150
151
  if (!__classPrivateFieldGet(this, _EditorImpl_markupEditor, "f")) {
151
152
  __classPrivateFieldSet(this, _EditorImpl_markupEditor, new MarkupEditor(createCodemirror({
152
153
  doc: __classPrivateFieldGet(this, _EditorImpl_markup, "f"),
153
- placeholderText: i18n('markup_placeholder'),
154
+ placeholder: (_a = __classPrivateFieldGet(this, _EditorImpl_markupConfig, "f").placeholder) !== null && _a !== void 0 ? _a : i18n('markup_placeholder'),
154
155
  onCancel: () => this.emit('cancel', null),
155
156
  onSubmit: () => this.emit('submit', null),
156
157
  onChange: () => this.emit('rerender-toolbar', null),
@@ -191,15 +191,19 @@ export class MarkdownSerializerState {
191
191
  let trailing = '';
192
192
  const progress = (node, _, index) => {
193
193
  let marks = node ? node.marks : [];
194
- // Remove marks from breaks (hard_break or soft_break) that are the last node inside
194
+ // Remove marks from breaks (hard_break or soft_break) that are the edge node inside
195
195
  // that mark to prevent parser edge cases with new lines just
196
- // before closing marks.
196
+ // before closing or after opening marks.
197
197
  if (node && node.type.spec.isBreak) {
198
198
  marks = marks.filter(m => {
199
+ if (index === 0)
200
+ return false;
199
201
  if (index + 1 == parent.childCount)
200
202
  return false;
203
+ const prev = parent.child(index - 1);
201
204
  const next = parent.child(index + 1);
202
- return m.isInSet(next.marks) && (!next.isText || /\S/.test(next.text));
205
+ return ((m.isInSet(prev.marks) && (!prev.isText || /\S/.test(prev.text))) &&
206
+ (m.isInSet(next.marks) && (!next.isText || /\S/.test(next.text))));
203
207
  });
204
208
  }
205
209
  let leading = trailing;
@@ -1,5 +1,4 @@
1
1
  import type { NodeType } from 'prosemirror-model';
2
2
  import type { Command } from 'prosemirror-state';
3
3
  export declare function toList(listType: NodeType): Command;
4
- export declare const liftIfCursorIsAtBeginningOfItem: Command;
5
4
  export declare const joinPrevList: Command;
@@ -1,7 +1,6 @@
1
- import { liftListItem, wrapInList } from 'prosemirror-schema-list';
1
+ import { wrapInList } from 'prosemirror-schema-list';
2
2
  import { joinPreviousBlock } from '../../../commands/join';
3
- import { get$CursorAtBlockStart } from '../../../utils/selection';
4
- import { findAnyParentList, isListItemNode, isListNode, isListOrItemNode, liType } from './utils';
3
+ import { findAnyParentList, isListNode, isListOrItemNode } from './utils';
5
4
  export function toList(listType) {
6
5
  return (state, dispatch) => {
7
6
  const parentList = findAnyParentList(state.schema)(state.selection);
@@ -14,17 +13,6 @@ export function toList(listType) {
14
13
  return wrapInList(listType)(state, dispatch);
15
14
  };
16
15
  }
17
- export const liftIfCursorIsAtBeginningOfItem = (state, dispatch) => {
18
- const $cursor = get$CursorAtBlockStart(state.selection);
19
- if (!$cursor)
20
- return false;
21
- const { schema } = state;
22
- const parentBlock = $cursor.node($cursor.depth - 1);
23
- if (parentBlock.firstChild === $cursor.parent && isListItemNode(parentBlock)) {
24
- return liftListItem(liType(schema))(state, dispatch);
25
- }
26
- return false;
27
- };
28
16
  export const joinPrevList = joinPreviousBlock({
29
17
  checkPrevNode: isListNode,
30
18
  skipNode: isListOrItemNode,
@@ -1,9 +1,8 @@
1
- import { chainCommands } from 'prosemirror-commands';
2
1
  import { liftListItem, sinkListItem, splitListItem } from 'prosemirror-schema-list';
3
2
  import { withLogAction } from '../../../utils/keymap';
4
3
  import { ListsSpecs, blType, liType, olType } from './ListsSpecs';
5
4
  import { actions } from './actions';
6
- import { joinPrevList, liftIfCursorIsAtBeginningOfItem, toList } from './commands';
5
+ import { joinPrevList, toList } from './commands';
7
6
  import { ListAction } from './const';
8
7
  import { ListsInputRulesExtension } from './inputrules';
9
8
  import { mergeListsPlugin } from './plugins/MergeListsPlugin';
@@ -21,7 +20,7 @@ export const Lists = (builder, opts) => {
21
20
  }, builder.Priority.High);
22
21
  builder.addKeymap(({ schema }) => ({
23
22
  Enter: splitListItem(liType(schema)),
24
- Backspace: chainCommands(liftIfCursorIsAtBeginningOfItem, joinPrevList),
23
+ Backspace: joinPrevList,
25
24
  }), builder.Priority.Low);
26
25
  builder.use(ListsInputRulesExtension, { bulletListInputRule: opts === null || opts === void 0 ? void 0 : opts.ulInputRules });
27
26
  builder.addPlugin(mergeListsPlugin);
@@ -17,11 +17,15 @@ export const mergeListsPlugin = () => new Plugin({
17
17
  },
18
18
  });
19
19
  function mergeAdjacentNodesWithSameType(tr, nodes) {
20
- for (let i = nodes.length - 1; i > 0; i--) {
21
- const prev = nodes[i - 1];
22
- const next = nodes[i];
23
- if (prev.node.type === next.node.type && prev.pos + prev.node.nodeSize === next.pos) {
24
- tr.join(next.pos);
20
+ const posAfterMap = {};
21
+ for (const item of nodes) {
22
+ const posBefore = item.pos;
23
+ const posAfter = posBefore + item.node.nodeSize;
24
+ posAfterMap[posAfter] = item.node;
25
+ const nodeBefore = posAfterMap[posBefore];
26
+ if ((nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.type) === item.node.type) {
27
+ tr.join(tr.mapping.map(posBefore));
28
+ posAfterMap[posBefore] = undefined;
25
29
  }
26
30
  }
27
31
  }
@@ -63,10 +63,23 @@ export const serializerTokens = {
63
63
  state.ensureNewLine();
64
64
  });
65
65
  },
66
- [YfmTableNode.Row]: (_state, node) => {
67
- throw new Error(`Should not serialize ${node.type.name} node via serialize-token`);
66
+ [YfmTableNode.Row]: (state, node) => {
67
+ console.warn(`Should not serialize ${node.type.name} node via serialize-token`);
68
+ state.write('||');
69
+ state.ensureNewLine();
70
+ state.write('\n');
71
+ state.renderContent(node);
72
+ state.write('||');
73
+ state.ensureNewLine();
68
74
  },
69
- [YfmTableNode.Cell]: (_state, node) => {
70
- throw new Error(`Should not serialize ${node.type.name} node via serialize-token`);
75
+ [YfmTableNode.Cell]: (state, node, parent) => {
76
+ console.warn(`Should not serialize ${node.type.name} node via serialize-token`);
77
+ state.renderContent(node);
78
+ const isLastCellInRow = parent.lastChild === node;
79
+ if (!isLastCellInRow) {
80
+ state.write('|');
81
+ state.ensureNewLine();
82
+ state.write('\n');
83
+ }
71
84
  },
72
85
  };
@@ -1,6 +1,6 @@
1
1
  import { autocompletion } from '@codemirror/autocomplete';
2
2
  import type { Extension, StateCommand } from '@codemirror/state';
3
- import { EditorView, EditorViewConfig, KeyBinding } from '@codemirror/view';
3
+ import { EditorView, EditorViewConfig, KeyBinding, placeholder } from '@codemirror/view';
4
4
  import { EventMap } from '../../bundle/Editor';
5
5
  import { ReactRenderStorage } from '../../extensions';
6
6
  import { Receiver } from '../../utils';
@@ -10,7 +10,7 @@ export type { YfmLangOptions };
10
10
  declare type Autocompletion = Parameters<typeof autocompletion>[0];
11
11
  export declare type CreateCodemirrorParams = {
12
12
  doc: EditorViewConfig['doc'];
13
- placeholderText: string;
13
+ placeholder: Parameters<typeof placeholder>[0];
14
14
  onCancel: () => void;
15
15
  onSubmit: () => void;
16
16
  onChange: () => void;
@@ -13,8 +13,8 @@ import { ReactRendererFacet } from './react-facet';
13
13
  import { SearchPanelPlugin } from './search-plugin/plugin';
14
14
  import { yfmLang } from './yfm';
15
15
  export function createCodemirror(params) {
16
- const { doc, placeholderText, reactRenderer, onCancel, onScroll, onSubmit, onChange, onDocChange, extensions: extraExtensions, disabledExtensions = {}, keymaps = [], receiver, yfmLangOptions, autocompletion: autocompletionConfig, } = params;
17
- const extensions = [gravityTheme, placeholder(placeholderText)];
16
+ const { doc, reactRenderer, onCancel, onScroll, onSubmit, onChange, onDocChange, disabledExtensions = {}, keymaps = [], receiver, yfmLangOptions, extensions: extraExtensions, placeholder: placeholderContent, autocompletion: autocompletionConfig, } = params;
17
+ const extensions = [gravityTheme, placeholder(placeholderContent)];
18
18
  if (!disabledExtensions.history) {
19
19
  extensions.push(history());
20
20
  }
@@ -1,2 +1,2 @@
1
1
  /** During build process, the current version will be injected here */
2
- export const VERSION = typeof '13.18.1' !== 'undefined' ? '13.18.1' : 'unknown';
2
+ export const VERSION = typeof '13.19.0' !== 'undefined' ? '13.19.0' : 'unknown';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/markdown-editor",
3
- "version": "13.18.1",
3
+ "version": "13.19.0",
4
4
  "description": "Markdown wysiwyg and markup editor",
5
5
  "license": "MIT",
6
6
  "repository": {