@ctzhian/tiptap 2.3.2-beta.3 → 2.3.2-beta.5

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.
@@ -18,10 +18,86 @@ export var TableOfContentsExtension = function TableOfContentsExtension(_ref) {
18
18
  var onTocUpdate = _ref.onTocUpdate,
19
19
  tableOfContentsOptions = _ref.tableOfContentsOptions;
20
20
  return TableOfContents.extend({
21
+ onCreate: function onCreate() {
22
+ var _lastChild$content$fi,
23
+ _this = this;
24
+ // 检查文档结构,避免在特定结构下触发错误
25
+ var doc = this.editor.state.doc;
26
+ var lastChild = doc.lastChild;
27
+ var hasProblematicStructure = lastChild && lastChild.type.name === 'paragraph' && lastChild.content.childCount === 1 && ((_lastChild$content$fi = lastChild.content.firstChild) === null || _lastChild$content$fi === void 0 ? void 0 : _lastChild$content$fi.type.name) === 'image';
28
+
29
+ // 如果有问题的结构,跳过父类的 onCreate,避免触发 contentMatchAt 错误
30
+ if (hasProblematicStructure) {
31
+ console.warn('TableOfContents: 检测到文档结构问题(图片单独成段),跳过 onCreate 以避免 contentMatchAt 错误');
32
+ // 仍然需要初始化 storage 和 scroll handler
33
+ this.storage.scrollHandler = function () {
34
+ if (!_this.options.scrollParent) return;
35
+ var scrollParent = typeof _this.options.scrollParent === 'function' ? _this.options.scrollParent() : _this.options.scrollParent;
36
+ var scrollPosition = scrollParent instanceof HTMLElement ? scrollParent.scrollTop : scrollParent.scrollY;
37
+ _this.storage.scrollPosition = scrollPosition || 0;
38
+ };
39
+ if (this.options.scrollParent) {
40
+ var scrollParent = typeof this.options.scrollParent === 'function' ? this.options.scrollParent() : this.options.scrollParent;
41
+ if (scrollParent) {
42
+ scrollParent.addEventListener('scroll', this.storage.scrollHandler);
43
+ }
44
+ }
45
+ return;
46
+ }
47
+
48
+ // 正常结构,调用父类的 onCreate,使用 try-catch 包装
49
+ try {
50
+ if (this.parent) {
51
+ this.parent();
52
+ }
53
+ } catch (error) {
54
+ // 捕获 onCreate 中的错误,特别是 contentMatchAt 错误
55
+ if (error instanceof Error && error.message.includes('contentMatchAt')) {
56
+ console.warn('TableOfContents: onCreate 中检测到 contentMatchAt 错误,已跳过', error);
57
+ // 即使出错,也初始化必要的部分
58
+ this.storage.scrollHandler = function () {
59
+ if (!_this.options.scrollParent) return;
60
+ var scrollParent = typeof _this.options.scrollParent === 'function' ? _this.options.scrollParent() : _this.options.scrollParent;
61
+ var scrollPosition = scrollParent instanceof HTMLElement ? scrollParent.scrollTop : scrollParent.scrollY;
62
+ _this.storage.scrollPosition = scrollPosition || 0;
63
+ };
64
+ return;
65
+ }
66
+ // 其他错误继续抛出
67
+ throw error;
68
+ }
69
+ },
21
70
  addProseMirrorPlugins: function addProseMirrorPlugins() {
22
71
  var _this$parent;
23
72
  var imeCompositionPluginKey = new PluginKey('imeComposition');
24
- return [].concat(_toConsumableArray((_this$parent = this.parent) === null || _this$parent === void 0 ? void 0 : _this$parent.call(this)), [new Plugin({
73
+ var parentPlugins = ((_this$parent = this.parent) === null || _this$parent === void 0 ? void 0 : _this$parent.call(this)) || [];
74
+
75
+ // 添加一个保护性插件,包装所有 appendTransaction,捕获可能的错误
76
+ var protectionPlugin = new Plugin({
77
+ key: new PluginKey('tableOfContentsProtection'),
78
+ appendTransaction: function appendTransaction(transactions, oldState, newState) {
79
+ try {
80
+ var _lastChild$content$fi2;
81
+ // 检查文档结构
82
+ var doc = newState.doc;
83
+ var lastChild = doc.lastChild;
84
+ var hasProblematicStructure = lastChild && lastChild.type.name === 'paragraph' && lastChild.content.childCount === 1 && ((_lastChild$content$fi2 = lastChild.content.firstChild) === null || _lastChild$content$fi2 === void 0 ? void 0 : _lastChild$content$fi2.type.name) === 'image';
85
+
86
+ // 如果有问题的结构,不执行任何操作
87
+ if (hasProblematicStructure) {
88
+ return null;
89
+ }
90
+ } catch (error) {
91
+ // 捕获任何错误,避免崩溃
92
+ if (error instanceof Error && error.message.includes('contentMatchAt')) {
93
+ console.warn('TableOfContents: 保护插件中检测到 contentMatchAt 错误', error);
94
+ return null;
95
+ }
96
+ }
97
+ return null;
98
+ }
99
+ });
100
+ return [protectionPlugin].concat(_toConsumableArray(parentPlugins), [new Plugin({
25
101
  key: new PluginKey('tableOfContentImeFix'),
26
102
  appendTransaction: function appendTransaction(transactions, _oldState, newState) {
27
103
  if (transactions.some(function (tr) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ctzhian/tiptap",
3
- "version": "2.3.2-beta.3",
3
+ "version": "2.3.2-beta.5",
4
4
  "description": "基于 Tiptap 二次开发的编辑器组件",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",