@lobehub/editor 1.11.0 → 1.13.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/es/editor-kernel/inode/helper.d.ts +9 -6
- package/es/editor-kernel/inode/helper.js +27 -0
- package/es/editor-kernel/inode/text-node.d.ts +2 -9
- package/es/plugins/code/plugin/index.d.ts +1 -1
- package/es/plugins/code/plugin/index.js +12 -1
- package/es/plugins/codeblock/command/index.d.ts +6 -0
- package/es/plugins/codeblock/command/index.js +1 -0
- package/es/plugins/codeblock/plugin/CodeHighlighterShiki.d.ts +7 -0
- package/es/plugins/codeblock/plugin/CodeHighlighterShiki.js +43 -2
- package/es/plugins/codeblock/plugin/FacadeShiki.d.ts +8 -1
- package/es/plugins/codeblock/plugin/FacadeShiki.js +95 -6
- package/es/plugins/codeblock/plugin/index.js +74 -29
- package/es/plugins/common/data-source/json-data-source.d.ts +2 -2
- package/es/plugins/common/data-source/json-data-source.js +2 -10
- package/es/plugins/common/index.d.ts +1 -1
- package/es/plugins/common/index.js +1 -1
- package/es/plugins/common/node/cursor.d.ts +3 -1
- package/es/plugins/common/node/cursor.js +9 -0
- package/es/plugins/common/plugin/index.d.ts +1 -1
- package/es/plugins/common/plugin/index.js +28 -1
- package/es/plugins/common/plugin/mdReader.d.ts +2 -0
- package/es/plugins/common/plugin/mdReader.js +84 -0
- package/es/plugins/common/react/ReactPlainText.d.ts +1 -1
- package/es/plugins/common/utils/index.d.ts +2 -2
- package/es/plugins/hr/plugin/index.js +26 -22
- package/es/plugins/link/plugin/index.js +42 -26
- package/es/plugins/list/plugin/index.js +121 -63
- package/es/plugins/list/utils/index.d.ts +3 -3
- package/es/plugins/markdown/data-source/markdown/parse.d.ts +21 -0
- package/es/plugins/markdown/data-source/markdown/parse.js +231 -0
- package/es/plugins/markdown/data-source/markdown/supersub.d.ts +1 -0
- package/es/plugins/markdown/data-source/markdown/supersub.js +14 -0
- package/es/plugins/markdown/data-source/markdown-data-source.d.ts +4 -4
- package/es/plugins/markdown/data-source/markdown-data-source.js +8 -2
- package/es/plugins/markdown/index.d.ts +2 -1
- package/es/plugins/markdown/index.js +1 -1
- package/es/plugins/markdown/plugin/index.js +135 -2
- package/es/plugins/markdown/service/shortcut.d.ts +19 -85
- package/es/plugins/markdown/service/shortcut.js +49 -293
- package/es/plugins/markdown/service/transformers.d.ts +60 -0
- package/es/plugins/markdown/service/transformers.js +286 -0
- package/es/plugins/markdown/utils/index.d.ts +45 -1
- package/es/plugins/markdown/utils/index.js +147 -1
- package/es/plugins/markdown/utils/logger.d.ts +7 -0
- package/es/plugins/markdown/utils/logger.js +2 -0
- package/es/plugins/math/plugin/index.js +64 -45
- package/es/plugins/mention/index.d.ts +1 -0
- package/es/plugins/mention/plugin/index.d.ts +4 -1
- package/es/plugins/mention/plugin/index.js +27 -11
- package/es/plugins/table/plugin/index.js +71 -26
- package/es/react/hooks/useEditorState/index.js +43 -21
- package/es/types/global.d.ts +64 -0
- package/package.json +2 -1
|
@@ -1,301 +1,28 @@
|
|
|
1
1
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
3
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
4
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
5
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
6
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
7
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
8
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
2
9
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
3
10
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
4
11
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
5
12
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
13
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
7
14
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
8
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
9
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
11
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
12
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
13
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
14
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
15
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
16
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
17
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
18
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
19
15
|
/* eslint-disable no-redeclare */
|
|
20
16
|
/* eslint-disable @typescript-eslint/no-redeclare */
|
|
21
|
-
|
|
17
|
+
|
|
22
18
|
import { genServiceId } from "../../../editor-kernel";
|
|
23
19
|
import { createDebugLogger } from "../../../utils/debug";
|
|
24
|
-
import {
|
|
20
|
+
import { indexBy } from "../utils";
|
|
21
|
+
import { $runTextFormatTransformers, runElementTransformers, runTextMatchTransformers, testElementTransformers } from "./transformers";
|
|
22
|
+
export var MARKDOWN_WRITER_LEVEL_MAX = 0;
|
|
23
|
+
export var MARKDOWN_READER_LEVEL_HIGH = 1;
|
|
24
|
+
export var MARKDOWN_READER_LEVEL_NORMAL = 2;
|
|
25
25
|
export var IMarkdownShortCutService = genServiceId('MarkdownShortCutService');
|
|
26
|
-
function testElementTransformers(parentNode, anchorNode, anchorOffset, elementTransformers, fromTrigger) {
|
|
27
|
-
var grandParentNode = parentNode.getParent();
|
|
28
|
-
if (!$isRootOrShadowRoot(grandParentNode) || parentNode.getFirstChild() !== anchorNode) {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
var textContent = anchorNode.getTextContent();
|
|
32
|
-
|
|
33
|
-
// Checking for anchorOffset position to prevent any checks for cases when caret is too far
|
|
34
|
-
// from a line start to be a part of block-level markdown trigger.
|
|
35
|
-
//
|
|
36
|
-
// TODO:
|
|
37
|
-
// Can have a quick check if caret is close enough to the beginning of the string (e.g. offset less than 10-20)
|
|
38
|
-
// since otherwise it won't be a markdown shortcut, but tables are exception
|
|
39
|
-
if (fromTrigger !== 'enter' && textContent[anchorOffset - 1] !== ' ') {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
var _iterator = _createForOfIteratorHelper(elementTransformers),
|
|
43
|
-
_step;
|
|
44
|
-
try {
|
|
45
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
46
|
-
var _step$value = _step.value,
|
|
47
|
-
regExp = _step$value.regExp,
|
|
48
|
-
trigger = _step$value.trigger;
|
|
49
|
-
var _match = textContent.match(regExp);
|
|
50
|
-
if (fromTrigger === trigger && _match && _match[0].length === (fromTrigger === 'enter' || _match[0].endsWith(' ') ? anchorOffset : anchorOffset - 1)) {
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
} catch (err) {
|
|
55
|
-
_iterator.e(err);
|
|
56
|
-
} finally {
|
|
57
|
-
_iterator.f();
|
|
58
|
-
}
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
function runElementTransformers(parentNode, anchorNode, anchorOffset, elementTransformers, fromTrigger) {
|
|
62
|
-
var grandParentNode = parentNode.getParent();
|
|
63
|
-
if (!$isRootOrShadowRoot(grandParentNode) || parentNode.getFirstChild() !== anchorNode) {
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
var textContent = anchorNode.getTextContent();
|
|
67
|
-
|
|
68
|
-
// Checking for anchorOffset position to prevent any checks for cases when caret is too far
|
|
69
|
-
// from a line start to be a part of block-level markdown trigger.
|
|
70
|
-
//
|
|
71
|
-
// TODO:
|
|
72
|
-
// Can have a quick check if caret is close enough to the beginning of the string (e.g. offset less than 10-20)
|
|
73
|
-
// since otherwise it won't be a markdown shortcut, but tables are exception
|
|
74
|
-
if (fromTrigger !== 'enter' && textContent[anchorOffset - 1] !== ' ') {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
var _iterator2 = _createForOfIteratorHelper(elementTransformers),
|
|
78
|
-
_step2;
|
|
79
|
-
try {
|
|
80
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
81
|
-
var _step2$value = _step2.value,
|
|
82
|
-
regExp = _step2$value.regExp,
|
|
83
|
-
replace = _step2$value.replace,
|
|
84
|
-
trigger = _step2$value.trigger;
|
|
85
|
-
var _match2 = textContent.match(regExp);
|
|
86
|
-
if (fromTrigger === trigger && _match2 && _match2[0].length === (fromTrigger === 'enter' || _match2[0].endsWith(' ') ? anchorOffset : anchorOffset - 1)) {
|
|
87
|
-
var nextSiblings = anchorNode.getNextSiblings();
|
|
88
|
-
var _anchorNode$splitText = anchorNode.splitText(anchorOffset),
|
|
89
|
-
_anchorNode$splitText2 = _slicedToArray(_anchorNode$splitText, 2),
|
|
90
|
-
leadingNode = _anchorNode$splitText2[0],
|
|
91
|
-
remainderNode = _anchorNode$splitText2[1];
|
|
92
|
-
var siblings = remainderNode ? [remainderNode].concat(_toConsumableArray(nextSiblings)) : nextSiblings;
|
|
93
|
-
if (replace(parentNode, siblings, _match2, false) !== false) {
|
|
94
|
-
leadingNode.remove();
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
} catch (err) {
|
|
100
|
-
_iterator2.e(err);
|
|
101
|
-
} finally {
|
|
102
|
-
_iterator2.f();
|
|
103
|
-
}
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
function runTextMatchTransformers(anchorNode, anchorOffset, transformersByTrigger) {
|
|
107
|
-
var textContent = anchorNode.getTextContent();
|
|
108
|
-
var lastChar = textContent[anchorOffset - 1];
|
|
109
|
-
var transformers = transformersByTrigger[lastChar];
|
|
110
|
-
if (!transformers) {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// If typing in the middle of content, remove the tail to do
|
|
115
|
-
// reg exp match up to a string end (caret position)
|
|
116
|
-
if (anchorOffset < textContent.length) {
|
|
117
|
-
textContent = textContent.slice(0, anchorOffset);
|
|
118
|
-
}
|
|
119
|
-
var _iterator3 = _createForOfIteratorHelper(transformers),
|
|
120
|
-
_step3;
|
|
121
|
-
try {
|
|
122
|
-
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
123
|
-
var _transformer = _step3.value;
|
|
124
|
-
if (!_transformer.replace || !_transformer.regExp) {
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
var _match3 = textContent.match(_transformer.regExp);
|
|
128
|
-
if (_match3 === null) {
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
var startIndex = _match3.index || 0;
|
|
132
|
-
var endIndex = startIndex + _match3[0].length;
|
|
133
|
-
var replaceNode = void 0;
|
|
134
|
-
if (startIndex === 0) {
|
|
135
|
-
var _anchorNode$splitText3 = anchorNode.splitText(endIndex);
|
|
136
|
-
var _anchorNode$splitText4 = _slicedToArray(_anchorNode$splitText3, 1);
|
|
137
|
-
replaceNode = _anchorNode$splitText4[0];
|
|
138
|
-
} else {
|
|
139
|
-
var _anchorNode$splitText5 = anchorNode.splitText(startIndex, endIndex);
|
|
140
|
-
var _anchorNode$splitText6 = _slicedToArray(_anchorNode$splitText5, 2);
|
|
141
|
-
replaceNode = _anchorNode$splitText6[1];
|
|
142
|
-
}
|
|
143
|
-
replaceNode.selectNext(0, 0);
|
|
144
|
-
_transformer.replace(replaceNode, _match3);
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
} catch (err) {
|
|
148
|
-
_iterator3.e(err);
|
|
149
|
-
} finally {
|
|
150
|
-
_iterator3.f();
|
|
151
|
-
}
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
function $runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformers) {
|
|
155
|
-
var textContent = anchorNode.getTextContent();
|
|
156
|
-
var closeTagEndIndex = anchorOffset - 1;
|
|
157
|
-
var closeChar = textContent[closeTagEndIndex];
|
|
158
|
-
// Quick check if we're possibly at the end of inline markdown style
|
|
159
|
-
var matchers = textFormatTransformers[closeChar];
|
|
160
|
-
if (!matchers) {
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
var _iterator4 = _createForOfIteratorHelper(matchers),
|
|
164
|
-
_step4;
|
|
165
|
-
try {
|
|
166
|
-
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
167
|
-
var matcher = _step4.value;
|
|
168
|
-
var tag = matcher.tag;
|
|
169
|
-
var tagLength = tag.length;
|
|
170
|
-
var closeTagStartIndex = closeTagEndIndex - tagLength + 1;
|
|
171
|
-
|
|
172
|
-
// If tag is not single char check if rest of it matches with text content
|
|
173
|
-
if (tagLength > 1 && !isEqualSubString(textContent, closeTagStartIndex, tag, 0, tagLength)) {
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Space before closing tag cancels inline markdown
|
|
178
|
-
if (textContent[closeTagStartIndex - 1] === ' ') {
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// Some tags can not be used within words, hence should have newline/space/punctuation after it
|
|
183
|
-
var afterCloseTagChar = textContent[closeTagEndIndex + 1];
|
|
184
|
-
if (matcher.intraword === false && afterCloseTagChar && !PUNCTUATION_OR_SPACE.test(afterCloseTagChar)) {
|
|
185
|
-
continue;
|
|
186
|
-
}
|
|
187
|
-
var closeNode = anchorNode;
|
|
188
|
-
var openNode = closeNode;
|
|
189
|
-
var openTagStartIndex = getOpenTagStartIndex(textContent, closeTagStartIndex, tag);
|
|
190
|
-
|
|
191
|
-
// Go through text node siblings and search for opening tag
|
|
192
|
-
// if haven't found it within the same text node as closing tag
|
|
193
|
-
var sibling = openNode;
|
|
194
|
-
while (openTagStartIndex < 0 && (sibling = sibling.getPreviousSibling())) {
|
|
195
|
-
if ($isLineBreakNode(sibling)) {
|
|
196
|
-
break;
|
|
197
|
-
}
|
|
198
|
-
if ($isTextNode(sibling)) {
|
|
199
|
-
if (sibling.hasFormat('code')) {
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
var siblingTextContent = sibling.getTextContent();
|
|
203
|
-
openNode = sibling;
|
|
204
|
-
openTagStartIndex = getOpenTagStartIndex(siblingTextContent, siblingTextContent.length, tag);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Opening tag is not found
|
|
209
|
-
if (openTagStartIndex < 0) {
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// No content between opening and closing tag
|
|
214
|
-
if (openNode === closeNode && openTagStartIndex + tagLength === closeTagStartIndex) {
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// Checking longer tags for repeating chars (e.g. *** vs **)
|
|
219
|
-
var prevOpenNodeText = openNode.getTextContent();
|
|
220
|
-
if (openTagStartIndex > 0 && prevOpenNodeText[openTagStartIndex - 1] === closeChar) {
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// Some tags can not be used within words, hence should have newline/space/punctuation before it
|
|
225
|
-
var beforeOpenTagChar = prevOpenNodeText[openTagStartIndex - 1];
|
|
226
|
-
if (matcher.intraword === false && beforeOpenTagChar && !PUNCTUATION_OR_SPACE.test(beforeOpenTagChar)) {
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Clean text from opening and closing tags (starting from closing tag
|
|
231
|
-
// to prevent any offset shifts if we start from opening one)
|
|
232
|
-
var prevCloseNodeText = closeNode.getTextContent();
|
|
233
|
-
var closeNodeText = prevCloseNodeText.slice(0, closeTagStartIndex) + prevCloseNodeText.slice(closeTagEndIndex + 1);
|
|
234
|
-
closeNode.setTextContent(closeNodeText);
|
|
235
|
-
var openNodeText = openNode === closeNode ? closeNodeText : prevOpenNodeText;
|
|
236
|
-
openNode.setTextContent(openNodeText.slice(0, openTagStartIndex) + openNodeText.slice(openTagStartIndex + tagLength));
|
|
237
|
-
var _selection = $getSelection();
|
|
238
|
-
var nextSelection = $createRangeSelection();
|
|
239
|
-
$setSelection(nextSelection);
|
|
240
|
-
// Adjust offset based on deleted chars
|
|
241
|
-
var newOffset = closeTagEndIndex - tagLength * (openNode === closeNode ? 2 : 1) + 1;
|
|
242
|
-
nextSelection.anchor.set(openNode.__key, openTagStartIndex, 'text');
|
|
243
|
-
nextSelection.focus.set(closeNode.__key, newOffset, 'text');
|
|
244
|
-
if (matcher.process) {
|
|
245
|
-
matcher.process(nextSelection);
|
|
246
|
-
return true;
|
|
247
|
-
} else if (matcher.format) {
|
|
248
|
-
// Apply formatting to selected text
|
|
249
|
-
var _iterator5 = _createForOfIteratorHelper(matcher.format),
|
|
250
|
-
_step5;
|
|
251
|
-
try {
|
|
252
|
-
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
253
|
-
var format = _step5.value;
|
|
254
|
-
if (!nextSelection.hasFormat(format)) {
|
|
255
|
-
nextSelection.formatText(format);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// Collapse selection up to the focus point
|
|
260
|
-
} catch (err) {
|
|
261
|
-
_iterator5.e(err);
|
|
262
|
-
} finally {
|
|
263
|
-
_iterator5.f();
|
|
264
|
-
}
|
|
265
|
-
nextSelection.anchor.set(nextSelection.focus.key, nextSelection.focus.offset, nextSelection.focus.type);
|
|
266
|
-
|
|
267
|
-
// Remove formatting from collapsed selection
|
|
268
|
-
var _iterator6 = _createForOfIteratorHelper(matcher.format),
|
|
269
|
-
_step6;
|
|
270
|
-
try {
|
|
271
|
-
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
|
|
272
|
-
var _format = _step6.value;
|
|
273
|
-
if (nextSelection.hasFormat(_format)) {
|
|
274
|
-
nextSelection.toggleFormat(_format);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
} catch (err) {
|
|
278
|
-
_iterator6.e(err);
|
|
279
|
-
} finally {
|
|
280
|
-
_iterator6.f();
|
|
281
|
-
}
|
|
282
|
-
if ($isRangeSelection(_selection)) {
|
|
283
|
-
nextSelection.format = _selection.format;
|
|
284
|
-
}
|
|
285
|
-
} else {
|
|
286
|
-
// No format or process specified, nothing to do
|
|
287
|
-
$setSelection(_selection);
|
|
288
|
-
continue;
|
|
289
|
-
}
|
|
290
|
-
return true;
|
|
291
|
-
}
|
|
292
|
-
} catch (err) {
|
|
293
|
-
_iterator4.e(err);
|
|
294
|
-
} finally {
|
|
295
|
-
_iterator4.f();
|
|
296
|
-
}
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
299
26
|
export var MarkdownShortCutService = /*#__PURE__*/function () {
|
|
300
27
|
function MarkdownShortCutService(kernel) {
|
|
301
28
|
_classCallCheck(this, MarkdownShortCutService);
|
|
@@ -304,6 +31,7 @@ export var MarkdownShortCutService = /*#__PURE__*/function () {
|
|
|
304
31
|
_defineProperty(this, "textMatchTransformers", []);
|
|
305
32
|
_defineProperty(this, "logger", createDebugLogger('service', 'markdown'));
|
|
306
33
|
_defineProperty(this, "_markdownWriters", {});
|
|
34
|
+
_defineProperty(this, "_markdownReaders", [{}, {}, {}]);
|
|
307
35
|
_defineProperty(this, "_textFormatTransformersByTrigger", null);
|
|
308
36
|
_defineProperty(this, "_textMatchTransformersByTrigger", null);
|
|
309
37
|
this.kernel = kernel;
|
|
@@ -313,6 +41,22 @@ export var MarkdownShortCutService = /*#__PURE__*/function () {
|
|
|
313
41
|
get: function get() {
|
|
314
42
|
return this._markdownWriters;
|
|
315
43
|
}
|
|
44
|
+
}, {
|
|
45
|
+
key: "markdownReaders",
|
|
46
|
+
get: function get() {
|
|
47
|
+
return this._markdownReaders.reduce(function (acc, curr) {
|
|
48
|
+
// @ts-expect-error not error
|
|
49
|
+
Object.keys(curr).forEach(function (key) {
|
|
50
|
+
if (!acc[key]) {
|
|
51
|
+
acc[key] = [];
|
|
52
|
+
}
|
|
53
|
+
var existing = acc[key];
|
|
54
|
+
var adding = curr[key];
|
|
55
|
+
existing.push.apply(existing, _toConsumableArray(adding));
|
|
56
|
+
});
|
|
57
|
+
return acc;
|
|
58
|
+
}, {});
|
|
59
|
+
}
|
|
316
60
|
}, {
|
|
317
61
|
key: "textMatchTransformersByTrigger",
|
|
318
62
|
get: function get() {
|
|
@@ -363,17 +107,17 @@ export var MarkdownShortCutService = /*#__PURE__*/function () {
|
|
|
363
107
|
}, {
|
|
364
108
|
key: "registerMarkdownShortCuts",
|
|
365
109
|
value: function registerMarkdownShortCuts(transformers) {
|
|
366
|
-
var
|
|
367
|
-
|
|
110
|
+
var _iterator = _createForOfIteratorHelper(transformers),
|
|
111
|
+
_step;
|
|
368
112
|
try {
|
|
369
|
-
for (
|
|
370
|
-
var
|
|
371
|
-
this.registerMarkdownShortCut(
|
|
113
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
114
|
+
var _transformer = _step.value;
|
|
115
|
+
this.registerMarkdownShortCut(_transformer);
|
|
372
116
|
}
|
|
373
117
|
} catch (err) {
|
|
374
|
-
|
|
118
|
+
_iterator.e(err);
|
|
375
119
|
} finally {
|
|
376
|
-
|
|
120
|
+
_iterator.f();
|
|
377
121
|
}
|
|
378
122
|
}
|
|
379
123
|
}, {
|
|
@@ -413,6 +157,18 @@ export var MarkdownShortCutService = /*#__PURE__*/function () {
|
|
|
413
157
|
}
|
|
414
158
|
throw new Error("Markdown writer for type \"".concat(type, "\" is already registered."));
|
|
415
159
|
}
|
|
160
|
+
}, {
|
|
161
|
+
key: "registerMarkdownReader",
|
|
162
|
+
value: function registerMarkdownReader(type, reader) {
|
|
163
|
+
var level = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : MARKDOWN_READER_LEVEL_NORMAL;
|
|
164
|
+
if (!this._markdownReaders[level][type]) {
|
|
165
|
+
this._markdownReaders[level][type] = [];
|
|
166
|
+
}
|
|
167
|
+
if (this._markdownReaders[level]) {
|
|
168
|
+
var _this$_markdownReader;
|
|
169
|
+
(_this$_markdownReader = this._markdownReaders[level][type]) === null || _this$_markdownReader === void 0 || _this$_markdownReader.push(reader);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
416
172
|
}]);
|
|
417
173
|
return MarkdownShortCutService;
|
|
418
174
|
}();
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ElementNode, LexicalNode, RangeSelection, TextFormatType, TextNode } from 'lexical';
|
|
2
|
+
export type TextFormatTransformer = Readonly<{
|
|
3
|
+
format?: ReadonlyArray<TextFormatType>;
|
|
4
|
+
intraword?: boolean;
|
|
5
|
+
process?: (selection: RangeSelection) => void;
|
|
6
|
+
tag: string;
|
|
7
|
+
type: 'text-format';
|
|
8
|
+
}>;
|
|
9
|
+
export type TextMatchTransformer = Readonly<{
|
|
10
|
+
/**
|
|
11
|
+
* For import operations, this function can be used to determine the end index of the match, after `importRegExp` has matched.
|
|
12
|
+
* Without this function, the end index will be determined by the length of the match from `importRegExp`. Manually determining the end index can be useful if
|
|
13
|
+
* the match from `importRegExp` is not the entire text content of the node. That way, `importRegExp` can be used to match only the start of the node, and `getEndIndex`
|
|
14
|
+
* can be used to match the end of the node.
|
|
15
|
+
*
|
|
16
|
+
* @returns The end index of the match, or false if the match was unsuccessful and a different transformer should be tried.
|
|
17
|
+
*/
|
|
18
|
+
getEndIndex?: (node: TextNode, match: RegExpMatchArray) => number | false;
|
|
19
|
+
/**
|
|
20
|
+
* This regex determines what text is matched during markdown imports
|
|
21
|
+
*/
|
|
22
|
+
importRegExp?: RegExp;
|
|
23
|
+
/**
|
|
24
|
+
* This regex determines what text is matched for markdown shortcuts while typing in the editor
|
|
25
|
+
*/
|
|
26
|
+
regExp: RegExp;
|
|
27
|
+
/**
|
|
28
|
+
* Determines how the matched markdown text should be transformed into a node during the markdown import process
|
|
29
|
+
*
|
|
30
|
+
* @returns nothing, or a TextNode that may be a child of the new node that is created.
|
|
31
|
+
* If a TextNode is returned, text format matching will be applied to it (e.g. bold, italic, etc.)
|
|
32
|
+
*/
|
|
33
|
+
replace?: (node: TextNode, match: RegExpMatchArray) => void | TextNode;
|
|
34
|
+
/**
|
|
35
|
+
* Single character that allows the transformer to trigger when typed in the editor. This does not affect markdown imports outside of the markdown shortcut plugin.
|
|
36
|
+
* If the trigger is matched, the `regExp` will be used to match the text in the second step.
|
|
37
|
+
*/
|
|
38
|
+
trigger?: string;
|
|
39
|
+
type: 'text-match';
|
|
40
|
+
}>;
|
|
41
|
+
export type ElementTransformer = {
|
|
42
|
+
regExp: RegExp;
|
|
43
|
+
/**
|
|
44
|
+
* `replace` is called when markdown is imported or typed in the editor
|
|
45
|
+
*
|
|
46
|
+
* @return return false to cancel the transform, even though the regex matched. Lexical will then search for the next transformer.
|
|
47
|
+
*/
|
|
48
|
+
replace: (parentNode: ElementNode, children: Array<LexicalNode>, match: Array<string>,
|
|
49
|
+
/**
|
|
50
|
+
* Whether the match is from an import operation (e.g. through `$convertFromMarkdownString`) or not (e.g. through typing in the editor).
|
|
51
|
+
*/
|
|
52
|
+
isImport: boolean) => boolean | void;
|
|
53
|
+
trigger?: 'enter';
|
|
54
|
+
type: 'element';
|
|
55
|
+
};
|
|
56
|
+
export type Transformer = ElementTransformer | TextFormatTransformer | TextMatchTransformer;
|
|
57
|
+
export declare function testElementTransformers(parentNode: ElementNode, anchorNode: TextNode, anchorOffset: number, elementTransformers: ReadonlyArray<ElementTransformer>, fromTrigger?: 'enter'): boolean;
|
|
58
|
+
export declare function runElementTransformers(parentNode: ElementNode, anchorNode: TextNode, anchorOffset: number, elementTransformers: ReadonlyArray<ElementTransformer>, fromTrigger?: 'enter'): boolean;
|
|
59
|
+
export declare function runTextMatchTransformers(anchorNode: TextNode, anchorOffset: number, transformersByTrigger: Readonly<Record<string, Array<TextMatchTransformer>>>): boolean;
|
|
60
|
+
export declare function $runTextFormatTransformers(anchorNode: TextNode, anchorOffset: number, textFormatTransformers: Readonly<Record<string, ReadonlyArray<TextFormatTransformer>>>): boolean;
|