@ctzhian/tiptap 1.12.24 → 1.13.1
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/dist/Editor/demo.js +5 -2
- package/dist/EditorMarkdown/demo.js +4 -1
- package/dist/EditorMarkdown/index.d.ts +2 -0
- package/dist/EditorMarkdown/index.js +42 -12
- package/dist/component/CustomBubbleMenu/index.js +1 -1
- package/dist/component/Toolbar/EditorInsert/index.js +12 -1
- package/dist/contants/slash-commands.js +34 -31
- package/dist/extension/component/Flow/Edit.d.ts +10 -0
- package/dist/extension/component/Flow/Edit.js +97 -0
- package/dist/extension/component/Flow/Insert.d.ts +7 -0
- package/dist/extension/component/Flow/Insert.js +108 -0
- package/dist/extension/component/Flow/Readonly.d.ts +8 -0
- package/dist/extension/component/Flow/Readonly.js +65 -0
- package/dist/extension/component/Flow/index.d.ts +9 -0
- package/dist/extension/component/Flow/index.js +354 -0
- package/dist/extension/component/Flow/useMermaidRender.d.ts +16 -0
- package/dist/extension/component/Flow/useMermaidRender.js +138 -0
- package/dist/extension/component/Flow/utils.d.ts +9 -0
- package/dist/extension/component/Flow/utils.js +32 -0
- package/dist/extension/component/SlashCommandsList/index.js +1 -1
- package/dist/extension/component/UploadProgress/index.d.ts +1 -1
- package/dist/extension/index.js +8 -3
- package/dist/extension/node/CodeBlockLowlight.js +15 -0
- package/dist/extension/node/FileHandler.d.ts +1 -1
- package/dist/extension/node/Flow/index.d.ts +18 -0
- package/dist/extension/node/Flow/index.js +103 -0
- package/dist/extension/node/TableOfContents/index.d.ts +2 -7
- package/dist/extension/node/TableOfContents/index.js +5 -373
- package/dist/extension/node/index.d.ts +1 -0
- package/dist/extension/node/index.js +1 -0
- package/dist/hook/index.js +1 -1
- package/dist/util/index.d.ts +0 -1
- package/dist/util/index.js +0 -1
- package/package.json +34 -33
- package/dist/extension/node/TableOfContents/plugin.d.ts +0 -6
- package/dist/extension/node/TableOfContents/plugin.js +0 -58
- package/dist/extension/node/TableOfContents/type.d.ts +0 -45
- package/dist/extension/node/TableOfContents/type.js +0 -1
- package/dist/extension/node/TableOfContents/utils.d.ts +0 -6
- package/dist/extension/node/TableOfContents/utils.js +0 -70
- package/dist/util/migrateMathStrings.d.ts +0 -73
- package/dist/util/migrateMathStrings.js +0 -192
|
@@ -1,58 +0,0 @@
|
|
|
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
3
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
4
|
-
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; }
|
|
5
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
6
|
-
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); }
|
|
7
|
-
import { Plugin, PluginKey } from '@tiptap/pm/state';
|
|
8
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
9
|
-
export var TableOfContentsPlugin = function TableOfContentsPlugin(_ref) {
|
|
10
|
-
var getId = _ref.getId,
|
|
11
|
-
_ref$anchorTypes = _ref.anchorTypes,
|
|
12
|
-
anchorTypes = _ref$anchorTypes === void 0 ? ['heading'] : _ref$anchorTypes;
|
|
13
|
-
return new Plugin({
|
|
14
|
-
key: new PluginKey('tableOfContent'),
|
|
15
|
-
appendTransaction: function appendTransaction(transactions, oldState, newState) {
|
|
16
|
-
var _view;
|
|
17
|
-
// 若处于组合输入,完全避免在此事务中写入 id,交由扩展层异步生成
|
|
18
|
-
if ((_view = newState.view) !== null && _view !== void 0 && _view.composing || typeof document !== 'undefined' && document.composing || typeof window !== 'undefined' && window.compositionState) {
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// 检查是否有任何事务包含组合相关的变化
|
|
23
|
-
var hasCompositionChanges = transactions.some(function (tr) {
|
|
24
|
-
return tr.getMeta('composition') || tr.getMeta('compositionend') || tr.getMeta('compositionstart');
|
|
25
|
-
});
|
|
26
|
-
if (hasCompositionChanges) {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
var tr = newState.tr;
|
|
30
|
-
var modified = false;
|
|
31
|
-
if (transactions.some(function (transaction) {
|
|
32
|
-
return transaction.docChanged;
|
|
33
|
-
}) && !oldState.doc.eq(newState.doc)) {
|
|
34
|
-
var existingIds = [];
|
|
35
|
-
newState.doc.descendants(function (node, pos) {
|
|
36
|
-
var nodeId = node.attrs.id;
|
|
37
|
-
if (!anchorTypes.includes(node.type.name) || node.textContent.length === 0) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (nodeId === null || nodeId === undefined || existingIds.includes(nodeId)) {
|
|
41
|
-
var id = '';
|
|
42
|
-
if (getId) {
|
|
43
|
-
id = getId(node.textContent);
|
|
44
|
-
} else {
|
|
45
|
-
id = uuidv4();
|
|
46
|
-
}
|
|
47
|
-
tr.setNodeMarkup(pos, undefined, _objectSpread(_objectSpread({}, node.attrs), {}, {
|
|
48
|
-
id: id
|
|
49
|
-
}));
|
|
50
|
-
modified = true;
|
|
51
|
-
}
|
|
52
|
-
existingIds.push(nodeId);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
return modified ? tr : null;
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
};
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { Editor } from '@tiptap/core';
|
|
2
|
-
import type { Node } from '@tiptap/pm/model';
|
|
3
|
-
export type GetTableOfContentLevelFunction = (headline: {
|
|
4
|
-
node: Node;
|
|
5
|
-
pos: number;
|
|
6
|
-
}, previousItems: TableOfContentDataItem[]) => number;
|
|
7
|
-
export type GetTableOfContentIndexFunction = (headline: {
|
|
8
|
-
node: Node;
|
|
9
|
-
pos: number;
|
|
10
|
-
}, previousItems: TableOfContentDataItem[], currentLevel?: number) => number;
|
|
11
|
-
export type TableOfContentsOptions = {
|
|
12
|
-
getId?: (textContent: string) => string;
|
|
13
|
-
onUpdate?: (data: TableOfContentData, isCreate?: boolean) => void;
|
|
14
|
-
getLevel?: GetTableOfContentLevelFunction;
|
|
15
|
-
getIndex?: GetTableOfContentIndexFunction;
|
|
16
|
-
scrollParent?: (() => HTMLElement | Window) | HTMLElement | Window;
|
|
17
|
-
anchorTypes?: Array<string>;
|
|
18
|
-
};
|
|
19
|
-
export type TableOfContentsStorage = {
|
|
20
|
-
content: TableOfContentData;
|
|
21
|
-
anchors: Array<HTMLHeadingElement | HTMLElement>;
|
|
22
|
-
scrollHandler: () => void;
|
|
23
|
-
scrollPosition: number;
|
|
24
|
-
};
|
|
25
|
-
export type TableOfContentData = Array<TableOfContentDataItem>;
|
|
26
|
-
export type TableOfContentDataItem = {
|
|
27
|
-
dom: HTMLHeadingElement;
|
|
28
|
-
editor: Editor;
|
|
29
|
-
id: string;
|
|
30
|
-
isActive: boolean;
|
|
31
|
-
isScrolledOver: boolean;
|
|
32
|
-
itemIndex: number;
|
|
33
|
-
level: number;
|
|
34
|
-
node: Node;
|
|
35
|
-
originalLevel: number;
|
|
36
|
-
pos: number;
|
|
37
|
-
textContent: string;
|
|
38
|
-
};
|
|
39
|
-
declare module '@tiptap/core' {
|
|
40
|
-
interface Commands<ReturnType> {
|
|
41
|
-
tableOfContents: {
|
|
42
|
-
updateTableOfContents: () => ReturnType;
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { GetTableOfContentIndexFunction, GetTableOfContentLevelFunction, TableOfContentDataItem } from './type';
|
|
2
|
-
export declare const getLastHeadingOnLevel: (headings: TableOfContentDataItem[], level: number) => TableOfContentDataItem | undefined;
|
|
3
|
-
export declare const getHeadlineLevel: GetTableOfContentLevelFunction;
|
|
4
|
-
export declare const getLinearIndexes: GetTableOfContentIndexFunction;
|
|
5
|
-
export declare const getHierarchicalIndexes: GetTableOfContentIndexFunction;
|
|
6
|
-
export declare function debounce(func: (...args: any[]) => void, wait: number): (...args: any[]) => void;
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
2
|
-
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."); }
|
|
3
|
-
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); }
|
|
4
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
5
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
6
|
-
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; }
|
|
7
|
-
export var getLastHeadingOnLevel = function getLastHeadingOnLevel(headings, level) {
|
|
8
|
-
var heading = headings.filter(function (currentHeading) {
|
|
9
|
-
return currentHeading.level === level;
|
|
10
|
-
}).pop();
|
|
11
|
-
if (level === 0) {
|
|
12
|
-
return undefined;
|
|
13
|
-
}
|
|
14
|
-
if (!heading) {
|
|
15
|
-
heading = getLastHeadingOnLevel(headings, level - 1);
|
|
16
|
-
}
|
|
17
|
-
return heading;
|
|
18
|
-
};
|
|
19
|
-
export var getHeadlineLevel = function getHeadlineLevel(headline, previousItems) {
|
|
20
|
-
var level = 1;
|
|
21
|
-
var previousHeadline = previousItems.at(-1);
|
|
22
|
-
var highestHeadlineAbove = _toConsumableArray(previousItems).reverse().find(function (h) {
|
|
23
|
-
return h.originalLevel <= headline.node.attrs.level;
|
|
24
|
-
});
|
|
25
|
-
var highestLevelAbove = (highestHeadlineAbove === null || highestHeadlineAbove === void 0 ? void 0 : highestHeadlineAbove.level) || 1;
|
|
26
|
-
if (headline.node.attrs.level > ((previousHeadline === null || previousHeadline === void 0 ? void 0 : previousHeadline.originalLevel) || 1)) {
|
|
27
|
-
level = ((previousHeadline === null || previousHeadline === void 0 ? void 0 : previousHeadline.level) || 1) + 1;
|
|
28
|
-
} else if (headline.node.attrs.level < ((previousHeadline === null || previousHeadline === void 0 ? void 0 : previousHeadline.originalLevel) || 1)) {
|
|
29
|
-
level = highestLevelAbove;
|
|
30
|
-
} else {
|
|
31
|
-
level = (previousHeadline === null || previousHeadline === void 0 ? void 0 : previousHeadline.level) || 1;
|
|
32
|
-
}
|
|
33
|
-
return level;
|
|
34
|
-
};
|
|
35
|
-
export var getLinearIndexes = function getLinearIndexes(_headline, previousHeadlines) {
|
|
36
|
-
var previousHeadline = previousHeadlines.at(-1);
|
|
37
|
-
if (!previousHeadline) {
|
|
38
|
-
return 1;
|
|
39
|
-
}
|
|
40
|
-
return ((previousHeadline === null || previousHeadline === void 0 ? void 0 : previousHeadline.itemIndex) || 1) + 1;
|
|
41
|
-
};
|
|
42
|
-
export var getHierarchicalIndexes = function getHierarchicalIndexes(headline, previousHeadlines, currentLevel) {
|
|
43
|
-
var _previousHeadlinesOnL;
|
|
44
|
-
var level = currentLevel || headline.node.attrs.level || 1;
|
|
45
|
-
var itemIndex = 1;
|
|
46
|
-
var previousHeadlinesOnLevelOrBelow = previousHeadlines.filter(function (h) {
|
|
47
|
-
return h.level <= level;
|
|
48
|
-
});
|
|
49
|
-
if (((_previousHeadlinesOnL = previousHeadlinesOnLevelOrBelow.at(-1)) === null || _previousHeadlinesOnL === void 0 ? void 0 : _previousHeadlinesOnL.level) === level) {
|
|
50
|
-
var _previousHeadlinesOnL2;
|
|
51
|
-
itemIndex = (((_previousHeadlinesOnL2 = previousHeadlinesOnLevelOrBelow.at(-1)) === null || _previousHeadlinesOnL2 === void 0 ? void 0 : _previousHeadlinesOnL2.itemIndex) || 1) + 1;
|
|
52
|
-
} else {
|
|
53
|
-
itemIndex = 1;
|
|
54
|
-
}
|
|
55
|
-
return itemIndex;
|
|
56
|
-
};
|
|
57
|
-
export function debounce(func, wait) {
|
|
58
|
-
var timeout = null;
|
|
59
|
-
return function () {
|
|
60
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
61
|
-
args[_key] = arguments[_key];
|
|
62
|
-
}
|
|
63
|
-
if (timeout) {
|
|
64
|
-
clearTimeout(timeout);
|
|
65
|
-
}
|
|
66
|
-
timeout = setTimeout(function () {
|
|
67
|
-
func.apply(void 0, args);
|
|
68
|
-
}, wait);
|
|
69
|
-
};
|
|
70
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import type { Editor } from '@tiptap/core';
|
|
2
|
-
import type { Transaction } from '@tiptap/pm/state';
|
|
3
|
-
/**
|
|
4
|
-
* Regular expression to match LaTeX math strings wrapped in single dollar signs.
|
|
5
|
-
* This should not catch dollar signs which are not part of a math expression,
|
|
6
|
-
* like those used for currency or other purposes.
|
|
7
|
-
* It ensures that the dollar signs are not preceded or followed by digits,
|
|
8
|
-
* allowing for proper identification of inline math expressions.
|
|
9
|
-
*
|
|
10
|
-
* - `$x^2 + y^2 = z^2$` will match
|
|
11
|
-
* - `This is $inline math$ in text.` will match
|
|
12
|
-
* - `This is $100$ dollars.` will not match (as it is not a math expression)
|
|
13
|
-
* - `This is $x^2 + y^2 = z^2$ and $100$ dollars.` will match both math expressions
|
|
14
|
-
* - `$$\frac{a}{b}$$` will NOT match (block math should be handled separately)
|
|
15
|
-
*/
|
|
16
|
-
export declare const mathMigrationRegex: RegExp;
|
|
17
|
-
/**
|
|
18
|
-
* Regular expression to match LaTeX math strings wrapped in double dollar signs.
|
|
19
|
-
* This matches block-level math expressions that should be displayed on their own line.
|
|
20
|
-
*
|
|
21
|
-
* - `$$\frac{a}{b}$$` will match
|
|
22
|
-
* - `$$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$$` will match
|
|
23
|
-
* - `$$100$$ dollars` will not match (as it is not a math expression)
|
|
24
|
-
*/
|
|
25
|
-
export declare const blockMathMigrationRegex: RegExp;
|
|
26
|
-
/**
|
|
27
|
-
* Alternative approach: First find all math patterns, then classify them
|
|
28
|
-
* This is more reliable than trying to use complex regex patterns
|
|
29
|
-
*/
|
|
30
|
-
export declare const allMathPatternsRegex: RegExp;
|
|
31
|
-
/**
|
|
32
|
-
* Creates a transaction that migrates existing math strings in the document to new math nodes.
|
|
33
|
-
* This function traverses the document and replaces LaTeX math syntax with proper math nodes,
|
|
34
|
-
* preserving the mathematical content. It handles both inline and block math expressions.
|
|
35
|
-
*
|
|
36
|
-
* Note: Math formulas inside <pre> and <code> tags are ignored to preserve code formatting.
|
|
37
|
-
*
|
|
38
|
-
* @param editor - The editor instance containing the schema and configuration
|
|
39
|
-
* @param tr - The transaction to modify with the migration operations
|
|
40
|
-
* @returns The modified transaction with math string replacements
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* const editor = new Editor({ ... })
|
|
45
|
-
* const tr = editor.state.tr
|
|
46
|
-
* const updatedTr = createMathMigrateTransaction(editor, tr)
|
|
47
|
-
* editor.view.dispatch(updatedTr)
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export declare function createMathMigrateTransaction(editor: Editor, tr: Transaction): Transaction;
|
|
51
|
-
/**
|
|
52
|
-
* Migrates existing math strings in the editor document to math nodes.
|
|
53
|
-
* This function creates and dispatches a transaction that converts LaTeX math syntax
|
|
54
|
-
* into proper math nodes. It handles both inline math (single dollar signs) and
|
|
55
|
-
* block math (double dollar signs). The migration happens immediately and is not
|
|
56
|
-
* added to the editor's history.
|
|
57
|
-
*
|
|
58
|
-
* Note: Math formulas inside <pre> and <code> tags are ignored to preserve code formatting.
|
|
59
|
-
*
|
|
60
|
-
* @param editor - The editor instance to perform the migration on
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* ```typescript
|
|
64
|
-
* const editor = new Editor({
|
|
65
|
-
* extensions: [Mathematics],
|
|
66
|
-
* content: 'This is inline math: $x^2 + y^2 = z^2$ and block math: $$\frac{a}{b}$$'
|
|
67
|
-
* })
|
|
68
|
-
*
|
|
69
|
-
* // Math strings will be automatically migrated to math nodes
|
|
70
|
-
* migrateMathStrings(editor)
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
export declare function migrateMathStrings(editor: Editor): void;
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Regular expression to match LaTeX math strings wrapped in single dollar signs.
|
|
3
|
-
* This should not catch dollar signs which are not part of a math expression,
|
|
4
|
-
* like those used for currency or other purposes.
|
|
5
|
-
* It ensures that the dollar signs are not preceded or followed by digits,
|
|
6
|
-
* allowing for proper identification of inline math expressions.
|
|
7
|
-
*
|
|
8
|
-
* - `$x^2 + y^2 = z^2$` will match
|
|
9
|
-
* - `This is $inline math$ in text.` will match
|
|
10
|
-
* - `This is $100$ dollars.` will not match (as it is not a math expression)
|
|
11
|
-
* - `This is $x^2 + y^2 = z^2$ and $100$ dollars.` will match both math expressions
|
|
12
|
-
* - `$$\frac{a}{b}$$` will NOT match (block math should be handled separately)
|
|
13
|
-
*/
|
|
14
|
-
export var mathMigrationRegex = /\$(?!\d+\$)(?!\$)(.+?)(?<!\$)\$(?!\d)(?!\$)/g;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Regular expression to match LaTeX math strings wrapped in double dollar signs.
|
|
18
|
-
* This matches block-level math expressions that should be displayed on their own line.
|
|
19
|
-
*
|
|
20
|
-
* - `$$\frac{a}{b}$$` will match
|
|
21
|
-
* - `$$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$$` will match
|
|
22
|
-
* - `$$100$$ dollars` will not match (as it is not a math expression)
|
|
23
|
-
*/
|
|
24
|
-
export var blockMathMigrationRegex = /\$\$([^$]+)\$\$/g;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Alternative approach: First find all math patterns, then classify them
|
|
28
|
-
* This is more reliable than trying to use complex regex patterns
|
|
29
|
-
*/
|
|
30
|
-
export var allMathPatternsRegex = /\$\$?[^$]*\$\$?/g;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Creates a transaction that migrates existing math strings in the document to new math nodes.
|
|
34
|
-
* This function traverses the document and replaces LaTeX math syntax with proper math nodes,
|
|
35
|
-
* preserving the mathematical content. It handles both inline and block math expressions.
|
|
36
|
-
*
|
|
37
|
-
* Note: Math formulas inside <pre> and <code> tags are ignored to preserve code formatting.
|
|
38
|
-
*
|
|
39
|
-
* @param editor - The editor instance containing the schema and configuration
|
|
40
|
-
* @param tr - The transaction to modify with the migration operations
|
|
41
|
-
* @returns The modified transaction with math string replacements
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```typescript
|
|
45
|
-
* const editor = new Editor({ ... })
|
|
46
|
-
* const tr = editor.state.tr
|
|
47
|
-
* const updatedTr = createMathMigrateTransaction(editor, tr)
|
|
48
|
-
* editor.view.dispatch(updatedTr)
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
export function createMathMigrateTransaction(editor, tr) {
|
|
52
|
-
var inlineMathNode = editor.schema.nodes.inlineMath;
|
|
53
|
-
var blockMathNode = editor.schema.nodes.blockMath;
|
|
54
|
-
|
|
55
|
-
// 收集所有需要处理的数学公式,按位置排序
|
|
56
|
-
var allMathReplacements = [];
|
|
57
|
-
|
|
58
|
-
// 第一遍:收集所有数学公式信息
|
|
59
|
-
tr.doc.descendants(function (node, pos) {
|
|
60
|
-
// 跳过代码块和预格式化文本
|
|
61
|
-
if (node.marks.some(function (mark) {
|
|
62
|
-
return mark.type.name === 'codeBlock' || mark.type.name === 'code';
|
|
63
|
-
})) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
if (!node.isText || !node.text || !node.text.includes('$')) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
var text = node.text;
|
|
70
|
-
|
|
71
|
-
// 使用更可靠的方法:先找到所有可能的数学模式,然后分类
|
|
72
|
-
var allMatches = text.match(allMathPatternsRegex);
|
|
73
|
-
if (!allMatches) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// 按位置排序,从后往前处理,避免位置偏移问题
|
|
78
|
-
var sortedMatches = allMatches.map(function (match) {
|
|
79
|
-
var isBlock = match.startsWith('$$') && match.endsWith('$$');
|
|
80
|
-
var type = isBlock ? 'block' : 'inline';
|
|
81
|
-
return {
|
|
82
|
-
match: match,
|
|
83
|
-
start: text.indexOf(match),
|
|
84
|
-
end: text.indexOf(match) + match.length,
|
|
85
|
-
type: type
|
|
86
|
-
};
|
|
87
|
-
}).sort(function (a, b) {
|
|
88
|
-
return b.start - a.start;
|
|
89
|
-
}); // 从后往前排序
|
|
90
|
-
|
|
91
|
-
sortedMatches.forEach(function (_ref) {
|
|
92
|
-
var match = _ref.match,
|
|
93
|
-
start = _ref.start,
|
|
94
|
-
end = _ref.end,
|
|
95
|
-
type = _ref.type;
|
|
96
|
-
var from = tr.mapping.map(pos + start);
|
|
97
|
-
var to = tr.mapping.map(pos + end);
|
|
98
|
-
var $from = tr.doc.resolve(from);
|
|
99
|
-
var parent = $from.parent;
|
|
100
|
-
var index = $from.index();
|
|
101
|
-
var latex;
|
|
102
|
-
if (type === 'block') {
|
|
103
|
-
latex = match.slice(2, -2); // 移除 $$ 符号
|
|
104
|
-
} else {
|
|
105
|
-
latex = match.slice(1, -1); // 移除 $ 符号
|
|
106
|
-
}
|
|
107
|
-
allMathReplacements.push({
|
|
108
|
-
pos: from,
|
|
109
|
-
match: match,
|
|
110
|
-
type: type,
|
|
111
|
-
latex: latex,
|
|
112
|
-
parent: parent,
|
|
113
|
-
index: index
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
// 按位置从后往前排序,避免位置偏移问题
|
|
119
|
-
allMathReplacements.sort(function (a, b) {
|
|
120
|
-
return b.pos - a.pos;
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// 第二遍:执行替换
|
|
124
|
-
allMathReplacements.forEach(function (_ref2) {
|
|
125
|
-
var pos = _ref2.pos,
|
|
126
|
-
match = _ref2.match,
|
|
127
|
-
type = _ref2.type,
|
|
128
|
-
latex = _ref2.latex,
|
|
129
|
-
parent = _ref2.parent,
|
|
130
|
-
index = _ref2.index;
|
|
131
|
-
var mathNode;
|
|
132
|
-
if (type === 'block') {
|
|
133
|
-
// 块级数学公式
|
|
134
|
-
mathNode = blockMathNode;
|
|
135
|
-
} else {
|
|
136
|
-
// 行内数学公式
|
|
137
|
-
mathNode = inlineMathNode;
|
|
138
|
-
}
|
|
139
|
-
if (type === 'block') {
|
|
140
|
-
// 对于块级数学公式,需要特殊处理
|
|
141
|
-
// 1. 先删除原始文本
|
|
142
|
-
var endPos = pos + match.length;
|
|
143
|
-
tr.delete(pos, endPos);
|
|
144
|
-
|
|
145
|
-
// 2. 在删除位置插入块级数学节点
|
|
146
|
-
tr.insert(pos, mathNode.create({
|
|
147
|
-
latex: latex
|
|
148
|
-
}));
|
|
149
|
-
} else {
|
|
150
|
-
// 对于行内数学公式,在段落内部替换
|
|
151
|
-
if (!parent.canReplaceWith(index, index + 1, mathNode)) {
|
|
152
|
-
console.warn("\u65E0\u6CD5\u66FF\u6362\u884C\u5185\u6570\u5B66\u516C\u5F0F:", match);
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
var _endPos = pos + match.length;
|
|
156
|
-
tr.replaceWith(pos, _endPos, mathNode.create({
|
|
157
|
-
latex: latex
|
|
158
|
-
}));
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// don't add to history
|
|
163
|
-
tr.setMeta('addToHistory', false);
|
|
164
|
-
return tr;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Migrates existing math strings in the editor document to math nodes.
|
|
169
|
-
* This function creates and dispatches a transaction that converts LaTeX math syntax
|
|
170
|
-
* into proper math nodes. It handles both inline math (single dollar signs) and
|
|
171
|
-
* block math (double dollar signs). The migration happens immediately and is not
|
|
172
|
-
* added to the editor's history.
|
|
173
|
-
*
|
|
174
|
-
* Note: Math formulas inside <pre> and <code> tags are ignored to preserve code formatting.
|
|
175
|
-
*
|
|
176
|
-
* @param editor - The editor instance to perform the migration on
|
|
177
|
-
*
|
|
178
|
-
* @example
|
|
179
|
-
* ```typescript
|
|
180
|
-
* const editor = new Editor({
|
|
181
|
-
* extensions: [Mathematics],
|
|
182
|
-
* content: 'This is inline math: $x^2 + y^2 = z^2$ and block math: $$\frac{a}{b}$$'
|
|
183
|
-
* })
|
|
184
|
-
*
|
|
185
|
-
* // Math strings will be automatically migrated to math nodes
|
|
186
|
-
* migrateMathStrings(editor)
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
export function migrateMathStrings(editor) {
|
|
190
|
-
var tr = createMathMigrateTransaction(editor, editor.state.tr);
|
|
191
|
-
editor.view.dispatch(tr);
|
|
192
|
-
}
|