@ctzhian/tiptap 2.4.4 → 2.5.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.
@@ -3,6 +3,7 @@ import 'core-js/actual/array/find-last';
3
3
  interface EditorDiffProps {
4
4
  oldHtml: string;
5
5
  newHtml: string;
6
+ baseUrl?: string;
6
7
  }
7
- declare const EditorDiff: ({ oldHtml, newHtml }: EditorDiffProps) => React.JSX.Element;
8
+ declare const EditorDiff: ({ oldHtml, newHtml, baseUrl }: EditorDiffProps) => React.JSX.Element;
8
9
  export default EditorDiff;
@@ -8,10 +8,12 @@ import useTiptap from "../hook";
8
8
 
9
9
  var EditorDiff = function EditorDiff(_ref) {
10
10
  var oldHtml = _ref.oldHtml,
11
- newHtml = _ref.newHtml;
11
+ newHtml = _ref.newHtml,
12
+ baseUrl = _ref.baseUrl;
12
13
  var editorRef = useTiptap({
13
14
  editable: false,
14
15
  content: newHtml,
16
+ baseUrl: baseUrl,
15
17
  exclude: ['youtube', 'mention']
16
18
  });
17
19
  useEffect(function () {
@@ -3,11 +3,35 @@ import { hasMarksInSelection } from "../../util";
3
3
  import { Divider, Paper, Stack } from '@mui/material';
4
4
  import { useEditorState } from '@tiptap/react';
5
5
  import { BubbleMenu } from '@tiptap/react/menus';
6
- import React from 'react';
6
+ import React, { useEffect, useRef } from 'react';
7
7
  import { ToolbarItem } from "../Toolbar";
8
8
  var CustomBubbleMenu = function CustomBubbleMenu(_ref) {
9
9
  var editor = _ref.editor,
10
10
  more = _ref.more;
11
+ // 跟踪编辑器是否已经完全初始化
12
+ var isInitializedRef = useRef(false);
13
+ var initTimeoutRef = useRef(null);
14
+
15
+ // 当编辑器挂载后,延迟一段时间标记为已初始化
16
+ useEffect(function () {
17
+ if (editor && editor.view) {
18
+ // 清除之前的定时器
19
+ if (initTimeoutRef.current) {
20
+ clearTimeout(initTimeoutRef.current);
21
+ }
22
+
23
+ // 延迟标记为已初始化,确保编辑器状态稳定
24
+ initTimeoutRef.current = setTimeout(function () {
25
+ isInitializedRef.current = true;
26
+ }, 100);
27
+ }
28
+ return function () {
29
+ if (initTimeoutRef.current) {
30
+ clearTimeout(initTimeoutRef.current);
31
+ }
32
+ };
33
+ }, [editor]);
34
+
11
35
  // const theme = useTheme()
12
36
 
13
37
  // const THEME_TEXT_COLOR = [
@@ -73,7 +97,38 @@ var CustomBubbleMenu = function CustomBubbleMenu(_ref) {
73
97
  offset: 8,
74
98
  flip: true
75
99
  },
76
- shouldShow: function shouldShow() {
100
+ shouldShow: function shouldShow(_ref2) {
101
+ var editor = _ref2.editor;
102
+ // 确保编辑器已初始化
103
+ if (!editor || !editor.state || !editor.view) {
104
+ return false;
105
+ }
106
+
107
+ // 如果编辑器还没有完全初始化,不显示
108
+ if (!isInitializedRef.current) {
109
+ return false;
110
+ }
111
+ var _editor$state = editor.state,
112
+ selection = _editor$state.selection,
113
+ doc = _editor$state.doc;
114
+
115
+ // 如果没有选中文本(选择为空或起始和结束位置相同),不显示
116
+ if (selection.empty || selection.from === selection.to) {
117
+ return false;
118
+ }
119
+
120
+ // 检查选中的文本内容是否真的有意义(不是空白字符)
121
+ // 使用 textBetween 获取选中范围内的文本,排除空白字符
122
+ var selectedText = doc.textBetween(selection.from, selection.to, ' ', ' ');
123
+ if (!selectedText || selectedText.trim().length === 0) {
124
+ return false;
125
+ }
126
+
127
+ // 如果文档为空(只有默认段落且无内容),不显示
128
+ if (editor.isEmpty) {
129
+ return false;
130
+ }
131
+
77
132
  // 表格多选单元格时禁止弹出气泡菜单
78
133
  // if (editor.state.selection.constructor.name === '_CellSelection') {
79
134
  // const cellSelection = editor.state.selection as any;
@@ -84,7 +139,9 @@ var CustomBubbleMenu = function CustomBubbleMenu(_ref) {
84
139
  // return cellSelection.$anchorCell.pos !== cellSelection.$headCell.pos;
85
140
  // }
86
141
  // }
87
- if (editor.state.selection.empty || editor.isActive('image') || editor.isActive('video') || editor.isActive('audio') || editor.isActive('emoji') || editor.isActive('codeBlock') || editor.isActive('blockMath') || editor.isActive('inlineMath') || editor.isActive('blockLink') || editor.isActive('inlineLink') || editor.isActive('blockAttachment') || editor.isActive('inlineAttachment') || editor.isActive('horizontalRule') || editor.isActive('iframe') || editor.isActive('yamlFormat') || editor.isActive('flow') || editor.isActive('table')) {
142
+
143
+ // 在某些特定节点类型时不显示
144
+ if (editor.isActive('image') || editor.isActive('video') || editor.isActive('audio') || editor.isActive('emoji') || editor.isActive('codeBlock') || editor.isActive('blockMath') || editor.isActive('inlineMath') || editor.isActive('blockLink') || editor.isActive('inlineLink') || editor.isActive('blockAttachment') || editor.isActive('inlineAttachment') || editor.isActive('horizontalRule') || editor.isActive('iframe') || editor.isActive('yamlFormat') || editor.isActive('flow') || editor.isActive('table')) {
88
145
  return false;
89
146
  }
90
147
  return true;
@@ -1,2 +1,2 @@
1
1
  import { GetExtensionsProps } from '../type';
2
- export declare const getExtensions: ({ limit, exclude, extensions: extensionsProps, editable, mentionItems, onMentionFilter, onUpload, onError, onTocUpdate, onAiWritingGetSuggestion, onValidateUrl, placeholder, youtubeOptions, tableOfContentsOptions, }: GetExtensionsProps) => any;
2
+ export declare const getExtensions: ({ limit, exclude, extensions: extensionsProps, editable, mentionItems, baseUrl, onMentionFilter, onUpload, onError, onTocUpdate, onAiWritingGetSuggestion, onValidateUrl, placeholder, youtubeOptions, tableOfContentsOptions, }: GetExtensionsProps) => any;
@@ -21,6 +21,8 @@ export var getExtensions = function getExtensions(_ref) {
21
21
  extensionsProps = _ref.extensions,
22
22
  editable = _ref.editable,
23
23
  mentionItems = _ref.mentionItems,
24
+ _ref$baseUrl = _ref.baseUrl,
25
+ baseUrl = _ref$baseUrl === void 0 ? '' : _ref$baseUrl,
24
26
  onMentionFilter = _ref.onMentionFilter,
25
27
  onUpload = _ref.onUpload,
26
28
  onError = _ref.onError,
@@ -57,21 +59,26 @@ export var getExtensions = function getExtensions(_ref) {
57
59
  onError: onError,
58
60
  onValidateUrl: onValidateUrl
59
61
  }), VideoExtension({
62
+ baseUrl: baseUrl,
60
63
  onUpload: onUpload,
61
64
  onError: onError,
62
65
  onValidateUrl: onValidateUrl
63
66
  }), AudioExtension({
67
+ baseUrl: baseUrl,
64
68
  onUpload: onUpload,
65
69
  onError: onError,
66
70
  onValidateUrl: onValidateUrl
67
71
  }), ImageExtension({
72
+ baseUrl: baseUrl,
68
73
  onUpload: onUpload,
69
74
  onError: onError,
70
75
  onValidateUrl: onValidateUrl
71
76
  }), InlineAttachmentExtension({
77
+ baseUrl: baseUrl,
72
78
  onUpload: onUpload,
73
79
  onError: onError
74
80
  }), BlockAttachmentExtension({
81
+ baseUrl: baseUrl,
75
82
  onUpload: onUpload,
76
83
  onError: onError
77
84
  }), Highlight.configure({
@@ -24,5 +24,8 @@ declare module '@tiptap/core' {
24
24
  };
25
25
  }
26
26
  }
27
- export declare const InlineAttachmentExtension: (props: EditorFnProps) => Node<any, any>;
28
- export declare const BlockAttachmentExtension: (props: EditorFnProps) => Node<any, any>;
27
+ export type AttachmentExtensionProps = EditorFnProps & {
28
+ baseUrl: string;
29
+ };
30
+ export declare const InlineAttachmentExtension: (props: AttachmentExtensionProps) => Node<any, any>;
31
+ export declare const BlockAttachmentExtension: (props: AttachmentExtensionProps) => Node<any, any>;
@@ -4,6 +4,7 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
4
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
5
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
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 { removeBaseUrl, withBaseUrl } from "../../util";
7
8
  import { mergeAttributes, Node } from "@tiptap/core";
8
9
  import { ReactNodeViewRenderer } from "@tiptap/react";
9
10
  import AttachmentViewWrapper from "../component/Attachment";
@@ -16,30 +17,21 @@ export var InlineAttachmentExtension = function InlineAttachmentExtension(props)
16
17
  atom: true,
17
18
  draggable: true,
18
19
  selectable: true,
19
- addOptions: function addOptions() {
20
- return {
21
- HTMLAttributes: {
22
- url: '',
23
- title: '',
24
- size: '0'
25
- }
26
- };
27
- },
28
20
  addAttributes: function addAttributes() {
29
21
  return {
30
22
  url: {
31
- default: this.options.HTMLAttributes.url,
23
+ default: '',
32
24
  parseHTML: function parseHTML(element) {
33
- return element.getAttribute('data-url');
25
+ return withBaseUrl(element.getAttribute('data-url') || '', props.baseUrl);
34
26
  },
35
27
  renderHTML: function renderHTML(attributes) {
36
28
  return {
37
- 'data-url': attributes.url
29
+ 'data-url': removeBaseUrl(attributes.url, props.baseUrl)
38
30
  };
39
31
  }
40
32
  },
41
33
  title: {
42
- default: this.options.HTMLAttributes.title,
34
+ default: '',
43
35
  parseHTML: function parseHTML(element) {
44
36
  return element.getAttribute('data-title');
45
37
  },
@@ -50,7 +42,7 @@ export var InlineAttachmentExtension = function InlineAttachmentExtension(props)
50
42
  }
51
43
  },
52
44
  size: {
53
- default: this.options.HTMLAttributes.size,
45
+ default: '0',
54
46
  parseHTML: function parseHTML(element) {
55
47
  return element.getAttribute('data-size') || '0';
56
48
  },
@@ -157,30 +149,22 @@ export var BlockAttachmentExtension = function BlockAttachmentExtension(props) {
157
149
  atom: true,
158
150
  draggable: true,
159
151
  selectable: true,
160
- addOptions: function addOptions() {
161
- return {
162
- HTMLAttributes: {
163
- url: '',
164
- title: '',
165
- size: '0'
166
- }
167
- };
168
- },
169
152
  addAttributes: function addAttributes() {
170
153
  return {
171
154
  url: {
172
- default: this.options.HTMLAttributes.url,
155
+ default: '',
173
156
  parseHTML: function parseHTML(element) {
174
- return element.getAttribute('data-url');
157
+ return withBaseUrl(element.getAttribute('data-url') || '', props.baseUrl);
175
158
  },
176
159
  renderHTML: function renderHTML(attributes) {
160
+ if (!attributes.url) return {};
177
161
  return {
178
- 'data-url': attributes.url
162
+ 'data-url': removeBaseUrl(attributes.url, props.baseUrl)
179
163
  };
180
164
  }
181
165
  },
182
166
  title: {
183
- default: this.options.HTMLAttributes.title,
167
+ default: '',
184
168
  parseHTML: function parseHTML(element) {
185
169
  return element.getAttribute('data-title');
186
170
  },
@@ -191,7 +175,7 @@ export var BlockAttachmentExtension = function BlockAttachmentExtension(props) {
191
175
  }
192
176
  },
193
177
  size: {
194
- default: this.options.HTMLAttributes.size,
178
+ default: '0',
195
179
  parseHTML: function parseHTML(element) {
196
180
  return element.getAttribute('data-size') || '0';
197
181
  },
@@ -18,6 +18,8 @@ declare module '@tiptap/core' {
18
18
  };
19
19
  }
20
20
  }
21
- export type AudioExtensionProps = EditorFnProps;
21
+ export type AudioExtensionProps = EditorFnProps & {
22
+ baseUrl: string;
23
+ };
22
24
  export declare const AudioExtension: (props: AudioExtensionProps) => Node<any, any>;
23
25
  export default AudioExtension;
@@ -4,6 +4,7 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
4
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
5
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
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 { removeBaseUrl, withBaseUrl } from "../../util";
7
8
  import { InputRule, mergeAttributes, Node } from '@tiptap/core';
8
9
  import { ReactNodeViewRenderer } from '@tiptap/react';
9
10
  import AudioViewWrapper from "../component/Audio";
@@ -31,12 +32,12 @@ export var AudioExtension = function AudioExtension(props) {
31
32
  src: {
32
33
  default: null,
33
34
  parseHTML: function parseHTML(element) {
34
- return element.getAttribute('src');
35
+ return withBaseUrl(element.getAttribute('src') || '', props.baseUrl);
35
36
  },
36
37
  renderHTML: function renderHTML(attributes) {
37
38
  if (!attributes.src) return {};
38
39
  return {
39
- src: attributes.src
40
+ src: removeBaseUrl(attributes.src, props.baseUrl)
40
41
  };
41
42
  }
42
43
  },
@@ -1,3 +1,5 @@
1
1
  import { EditorFnProps } from "../../type";
2
- export type ImageExtensionProps = EditorFnProps;
2
+ export type ImageExtensionProps = EditorFnProps & {
3
+ baseUrl: string;
4
+ };
3
5
  export declare const ImageExtension: (props: ImageExtensionProps) => import("@tiptap/core").Node<import("@tiptap/extension-image").ImageOptions, any>;
@@ -7,13 +7,30 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
7
7
  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; }
8
8
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
9
9
  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); }
10
- import { getFileType } from "../../util";
10
+ import { getFileType, removeBaseUrl, withBaseUrl } from "../../util";
11
11
  import Image from "@tiptap/extension-image";
12
12
  import { Plugin, PluginKey } from "@tiptap/pm/state";
13
13
  import { ReactNodeViewRenderer } from "@tiptap/react";
14
14
  import ImageViewWrapper, { getImageDimensionsFromFile } from "../component/Image";
15
15
  var customImage = function customImage(props) {
16
16
  return Image.extend({
17
+ addAttributes: function addAttributes() {
18
+ var _this$parent;
19
+ return _objectSpread(_objectSpread({}, (_this$parent = this.parent) === null || _this$parent === void 0 ? void 0 : _this$parent.call(this)), {}, {
20
+ src: {
21
+ default: null,
22
+ parseHTML: function parseHTML(element) {
23
+ return withBaseUrl(element.getAttribute('src') || '', props.baseUrl);
24
+ },
25
+ renderHTML: function renderHTML(attributes) {
26
+ if (!attributes.src) return {};
27
+ return {
28
+ src: removeBaseUrl(attributes.src, props.baseUrl)
29
+ };
30
+ }
31
+ }
32
+ });
33
+ },
17
34
  addKeyboardShortcuts: function addKeyboardShortcuts() {
18
35
  var _this = this;
19
36
  return {
@@ -1,5 +1,5 @@
1
1
  import { Extension } from '@tiptap/core';
2
2
  export declare const TableExtension: ({ editable }: {
3
3
  editable: boolean;
4
- }) => (Extension<any, any> | import("@tiptap/core").Node<import("@tiptap/extension-table").TableOptions, any> | import("@tiptap/core").Node<import("@tiptap/extension-table").TableHeaderOptions, any>)[];
4
+ }) => (import("@tiptap/core").Node<import("@tiptap/extension-table").TableOptions, any> | import("@tiptap/core").Node<import("@tiptap/extension-table").TableHeaderOptions, any> | Extension<any, any>)[];
5
5
  export default TableExtension;
@@ -19,6 +19,8 @@ declare module '@tiptap/core' {
19
19
  };
20
20
  }
21
21
  }
22
- export type VideoExtensionProps = EditorFnProps;
22
+ export type VideoExtensionProps = EditorFnProps & {
23
+ baseUrl: string;
24
+ };
23
25
  export declare const VideoExtension: (props: VideoExtensionProps) => Node<any, any>;
24
26
  export default VideoExtension;
@@ -7,7 +7,7 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
7
7
  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; }
8
8
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
9
9
  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); }
10
- import { getFileType } from "../../util";
10
+ import { getFileType, removeBaseUrl, withBaseUrl } from "../../util";
11
11
  import { InputRule, mergeAttributes, Node } from '@tiptap/core';
12
12
  import { Plugin, PluginKey } from '@tiptap/pm/state';
13
13
  import { ReactNodeViewRenderer } from '@tiptap/react';
@@ -37,12 +37,12 @@ export var VideoExtension = function VideoExtension(props) {
37
37
  src: {
38
38
  default: null,
39
39
  parseHTML: function parseHTML(element) {
40
- return element.getAttribute('src');
40
+ return withBaseUrl(element.getAttribute('src') || '', props.baseUrl);
41
41
  },
42
42
  renderHTML: function renderHTML(attributes) {
43
43
  if (!attributes.src) return {};
44
44
  return {
45
- src: attributes.src
45
+ src: removeBaseUrl(attributes.src, props.baseUrl)
46
46
  };
47
47
  }
48
48
  },
@@ -1,4 +1,4 @@
1
1
  import { UseTiptapProps, UseTiptapReturn } from "../type";
2
2
  import { UseEditorOptions } from '@tiptap/react';
3
- declare const useTiptap: ({ editable, contentType, onSave, onError, ...options }: UseTiptapProps & UseEditorOptions) => UseTiptapReturn;
3
+ declare const useTiptap: ({ editable, contentType, onSave, onError, onUpload, baseUrl, ...options }: UseTiptapProps & UseEditorOptions) => UseTiptapReturn;
4
4
  export default useTiptap;
@@ -1,5 +1,5 @@
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
- var _excluded = ["editable", "contentType", "onSave", "onError"];
2
+ var _excluded = ["editable", "contentType", "onSave", "onError", "onUpload", "baseUrl"];
3
3
  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; }
4
4
  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; }
5
5
  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; }
@@ -8,6 +8,7 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
8
8
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
9
9
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
10
10
  import { getExtensions } from "../extension";
11
+ import { withBaseUrl } from "../util";
11
12
  import { migrateMathStrings } from '@tiptap/extension-mathematics';
12
13
  import { useEditor } from '@tiptap/react';
13
14
  var useTiptap = function useTiptap(_ref) {
@@ -17,10 +18,20 @@ var useTiptap = function useTiptap(_ref) {
17
18
  contentType = _ref$contentType === void 0 ? 'html' : _ref$contentType,
18
19
  onSave = _ref.onSave,
19
20
  onError = _ref.onError,
21
+ onUpload = _ref.onUpload,
22
+ _ref$baseUrl = _ref.baseUrl,
23
+ baseUrl = _ref$baseUrl === void 0 ? '' : _ref$baseUrl,
20
24
  options = _objectWithoutProperties(_ref, _excluded);
25
+ var handleUpload = onUpload ? function (file, onProgress, abortSignal) {
26
+ return onUpload(file, onProgress, abortSignal).then(function (url) {
27
+ return withBaseUrl(url, baseUrl);
28
+ });
29
+ } : undefined;
21
30
  var extensions = getExtensions(_objectSpread({
22
31
  editable: editable,
23
- onError: onError
32
+ onError: onError,
33
+ baseUrl: baseUrl,
34
+ onUpload: handleUpload
24
35
  }, options));
25
36
  var editor = useEditor(_objectSpread(_objectSpread(_objectSpread({
26
37
  editable: editable,
@@ -140,6 +140,10 @@ export type BaseExtensionOptions = {
140
140
  * 占位符
141
141
  */
142
142
  placeholder?: string;
143
+ /**
144
+ * 静态资源基础路径
145
+ */
146
+ baseUrl?: string;
143
147
  };
144
148
  export type ExtensionRelativeProps = MentionExtensionProps & NodeOrMetaOrSuggestionOrExtensionOptions & EditorFnProps & BaseExtensionOptions;
145
149
  export type UseTiptapProps = {
@@ -7,6 +7,8 @@ export * from './shortcutKey';
7
7
  import { Node } from '@tiptap/pm/model';
8
8
  import { EditorState } from '@tiptap/pm/state';
9
9
  import { Editor } from '@tiptap/react';
10
+ export declare const withBaseUrl: (url: string, baseUrl: string) => string;
11
+ export declare const removeBaseUrl: (url: string, baseUrl: string) => string;
10
12
  export declare const formatFileSize: (bytes: number) => string;
11
13
  export declare const insertNodeAfterPosition: (editor: Editor, pos: number, nodeContent: any) => void;
12
14
  export declare const hasMarksInBlock: (node: Node | null | undefined) => boolean;
@@ -4,6 +4,44 @@ export * from "./floating";
4
4
  export * from "./linewiseConvert";
5
5
  export * from "./resourceExtractor";
6
6
  export * from "./shortcutKey";
7
+ export var withBaseUrl = function withBaseUrl(url, baseUrl) {
8
+ var _url$trim, _baseUrl$trim;
9
+ var trimmedUrl = (_url$trim = url === null || url === void 0 ? void 0 : url.trim()) !== null && _url$trim !== void 0 ? _url$trim : '';
10
+ if (!trimmedUrl) return trimmedUrl;
11
+ var isAbsolute = /^([a-z][a-z\d+\-.]*:)?\/\//i.test(trimmedUrl) || trimmedUrl.startsWith('data:');
12
+ if (isAbsolute) {
13
+ return trimmedUrl;
14
+ }
15
+ var trimmedBase = (_baseUrl$trim = baseUrl === null || baseUrl === void 0 ? void 0 : baseUrl.trim()) !== null && _baseUrl$trim !== void 0 ? _baseUrl$trim : '';
16
+ if (!trimmedBase) {
17
+ return trimmedUrl;
18
+ }
19
+ if (trimmedUrl.startsWith(trimmedBase)) {
20
+ return trimmedUrl;
21
+ }
22
+ var baseEndsWithSlash = trimmedBase.endsWith('/');
23
+ var urlStartsWithSlash = trimmedUrl.startsWith('/');
24
+ if (baseEndsWithSlash && urlStartsWithSlash) {
25
+ return "".concat(trimmedBase.slice(0, -1)).concat(trimmedUrl);
26
+ } else if (!baseEndsWithSlash && !urlStartsWithSlash) {
27
+ return "".concat(trimmedBase, "/").concat(trimmedUrl);
28
+ } else {
29
+ return "".concat(trimmedBase).concat(trimmedUrl);
30
+ }
31
+ };
32
+ export var removeBaseUrl = function removeBaseUrl(url, baseUrl) {
33
+ var _url$trim2, _baseUrl$trim2;
34
+ var trimmedUrl = (_url$trim2 = url === null || url === void 0 ? void 0 : url.trim()) !== null && _url$trim2 !== void 0 ? _url$trim2 : '';
35
+ if (!trimmedUrl) return trimmedUrl;
36
+ var trimmedBase = (_baseUrl$trim2 = baseUrl === null || baseUrl === void 0 ? void 0 : baseUrl.trim()) !== null && _baseUrl$trim2 !== void 0 ? _baseUrl$trim2 : '';
37
+ if (!trimmedBase) {
38
+ return trimmedUrl;
39
+ }
40
+ if (trimmedUrl.startsWith(trimmedBase)) {
41
+ return trimmedUrl.slice(trimmedBase.length);
42
+ }
43
+ return trimmedUrl;
44
+ };
7
45
  export var formatFileSize = function formatFileSize(bytes) {
8
46
  if (bytes === 0) return '0 B';
9
47
  var k = 1024;
package/package.json CHANGED
@@ -1,10 +1,24 @@
1
1
  {
2
2
  "name": "@ctzhian/tiptap",
3
- "version": "2.4.4",
3
+ "version": "2.5.1",
4
4
  "description": "基于 Tiptap 二次开发的编辑器组件",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "start": "npm run dev",
10
+ "dev": "dumi dev",
11
+ "build": "rm -rf dist && father build",
12
+ "build:watch": "father dev",
13
+ "docs:build": "dumi build",
14
+ "docs:preview": "dumi preview",
15
+ "prepare": "husky install && dumi setup",
16
+ "doctor": "father doctor",
17
+ "lint": "npm run lint:es && npm run lint:css",
18
+ "lint:css": "stylelint \"{src,test}/**/*.{css,less}\"",
19
+ "lint:es": "eslint \"{src,test}/**/*.{js,jsx,ts,tsx}\"",
20
+ "pre": "pnpm build && pnpm link --global"
21
+ },
8
22
  "authors": [
9
23
  "ky.kyy@qq.com"
10
24
  ],
@@ -128,18 +142,5 @@
128
142
  "react-image-crop": "^11.0.10",
129
143
  "react-photo-view": "^1.2.7",
130
144
  "uuid": "^11.1.0"
131
- },
132
- "scripts": {
133
- "start": "npm run dev",
134
- "dev": "dumi dev",
135
- "build": "rm -rf dist && father build",
136
- "build:watch": "father dev",
137
- "docs:build": "dumi build",
138
- "docs:preview": "dumi preview",
139
- "doctor": "father doctor",
140
- "lint": "npm run lint:es && npm run lint:css",
141
- "lint:css": "stylelint \"{src,test}/**/*.{css,less}\"",
142
- "lint:es": "eslint \"{src,test}/**/*.{js,jsx,ts,tsx}\"",
143
- "pre": "pnpm build && pnpm link --global"
144
145
  }
145
- }
146
+ }