@seafile/sdoc-editor 2.0.43 → 2.0.44

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.
@@ -26,6 +26,7 @@ var _basicSdk = require("../../basic-sdk");
26
26
  var _constants2 = require("../../constants");
27
27
  var _outlineModule = require("../../android/outline-module");
28
28
  var _jsBridge = _interopRequireDefault(require("../../android/js-bridge"));
29
+ var _helpers = require("../extension/plugins/ai/ai-module/helpers");
29
30
  const SdocEditor = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
30
31
  let {
31
32
  editor: propsEditor,
@@ -118,6 +119,16 @@ const SdocEditor = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
118
119
  (0, _react.useEffect)(() => {
119
120
  const eventBus = _basicSdk.EventBus.getInstance();
120
121
  eventBus.subscribe(_constants2.EXTERNAL_EVENT.REFRESH_DOCUMENT, onRefreshDocument);
122
+
123
+ // Remove aiMarks on special conditions like unexpected exit or refresh page using AI
124
+ const hasAIMark = !_slate.Editor.nodes(validEditor, {
125
+ at: [],
126
+ match: n => _slate.Text.isText(n) && n.sdoc_ai === true
127
+ }).next().done;
128
+ if (hasAIMark) {
129
+ (0, _helpers.removeMarks)(validEditor);
130
+ }
131
+ // eslint-disable-next-line react-hooks/exhaustive-deps
121
132
  }, [onRefreshDocument]);
122
133
 
123
134
  // The parent component can call the method of this component through ref
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.removeMarks = void 0;
7
+ var _slate = require("@seafile/slate");
8
+ const removeMarks = editor => {
9
+ const {
10
+ selection
11
+ } = editor;
12
+ _slate.Transforms.unsetNodes(editor, 'sdoc_ai', {
13
+ at: [],
14
+ match: n => _slate.Text.isText(n) && n.sdoc_ai === true
15
+ });
16
+ if (selection) {
17
+ _slate.Transforms.select(editor, selection);
18
+ } else {
19
+ _slate.Transforms.deselect(editor);
20
+ }
21
+ };
22
+ exports.removeMarks = removeMarks;
@@ -23,6 +23,7 @@ var _tipDialog = _interopRequireDefault(require("./tip-dialog"));
23
23
  var _core = require("../../../core");
24
24
  var _constants2 = require("../../../constants");
25
25
  var _aiIcon = _interopRequireDefault(require("../ai-icon"));
26
+ var _helpers = require("./helpers");
26
27
  require("./style.css");
27
28
  function AIModule(_ref) {
28
29
  let {
@@ -45,8 +46,9 @@ function AIModule(_ref) {
45
46
  const [searchResult, setSearchResult] = (0, _react.useState)(null);
46
47
  const [currentLang, setCurrentLang] = (0, _react.useState)('en');
47
48
  const [isShowTipDialog, setIsShowTipDialog] = (0, _react.useState)(false);
48
- const [selectDom, setSelectDom] = (0, _react.useState)(null);
49
49
  const toggleAskAI = (0, _react.useCallback)(() => {
50
+ // Add marks for selection
51
+ _slate.Editor.addMark(editor, 'sdoc_ai', true);
50
52
  scrollRef.current = document.querySelector('.sdoc-scroll-container');
51
53
  const {
52
54
  scrollTop,
@@ -59,7 +61,6 @@ function AIModule(_ref) {
59
61
  }
60
62
  const domSelection = window.getSelection();
61
63
  const domRange = domSelection.getRangeAt(0);
62
- setSelectDom(domRange);
63
64
  const rect = domRange.getBoundingClientRect();
64
65
  const needPaddingBottomHeight = scrollTop + rect.bottom + _constants.AI_MIN_HEIGHT - scrollHeight;
65
66
  if (needPaddingBottomHeight > 0) {
@@ -74,13 +75,17 @@ function AIModule(_ref) {
74
75
  });
75
76
  }
76
77
  setTimeout(() => {
77
- var _inputRef$current;
78
+ var _rect, _inputRef$current;
78
79
  const aboveNode = (0, _core.getAboveBlockNode)(editor);
79
80
  const slateDom = _slateReact.ReactEditor.toDOMNode(editor, aboveNode[0]);
80
81
  const slateRect = slateDom.getBoundingClientRect();
81
- const newRect = domRange.getBoundingClientRect();
82
+ const markedSpan = document.querySelectorAll('span[data-slate-leaf="true"].sdoc_ai');
83
+ let rect;
84
+ if (markedSpan.length) {
85
+ rect = markedSpan[markedSpan.length - 1].getBoundingClientRect();
86
+ }
82
87
  const el = aiRef.current;
83
- el.style.top = `${newRect.bottom + 8}px`; // top = Current top + Element height
88
+ el.style.top = `${((_rect = rect) === null || _rect === void 0 ? void 0 : _rect.bottom) + 8 - 3.23}px`; // top = Current top + Element height - Element padding bottom
84
89
  el.style.left = `${slateRect.left}px`;
85
90
  el.style.display = 'block';
86
91
  setIsShowAIPopover(true);
@@ -118,6 +123,7 @@ function AIModule(_ref) {
118
123
  }, 500);
119
124
  }, [editor, element]);
120
125
  const onCloseClick = (0, _react.useCallback)(() => {
126
+ (0, _helpers.removeMarks)(editor);
121
127
  const element = aiRef.current;
122
128
  element.style.display = 'none';
123
129
  const articleDom = document.querySelector('.sdoc-editor__article');
@@ -126,7 +132,7 @@ function AIModule(_ref) {
126
132
  setSearchResult('');
127
133
  setIsShowAIPopover(false);
128
134
  closeModule();
129
- }, [closeModule]);
135
+ }, [closeModule, editor]);
130
136
  const onDocumentClick = (0, _react.useCallback)(event => {
131
137
  // not in ai container
132
138
  if (aiRef.current && aiRef.current.contains(event.target) && aiRef.current !== event.target) return;
@@ -161,12 +167,17 @@ function AIModule(_ref) {
161
167
  }, [onDocumentClick]);
162
168
  const onScroll = (0, _react.useCallback)(event => {
163
169
  if (!element) {
164
- const newRect = selectDom.getBoundingClientRect();
170
+ var _rect2;
171
+ const markedSpan = document.querySelectorAll('span[data-slate-leaf="true"].sdoc_ai');
172
+ let rect;
173
+ if (markedSpan.length) {
174
+ rect = markedSpan[markedSpan.length - 1].getBoundingClientRect();
175
+ }
165
176
  const aboveNode = (0, _core.getAboveBlockNode)(editor);
166
177
  const slateDom = _slateReact.ReactEditor.toDOMNode(editor, aboveNode[0]);
167
178
  const slateRect = slateDom.getBoundingClientRect();
168
179
  const el = aiRef.current;
169
- el.style.top = `${newRect.bottom + 8}px`; // top = Current top + Element height
180
+ el.style.top = `${((_rect2 = rect) === null || _rect2 === void 0 ? void 0 : _rect2.bottom) + 8 - 3.23}px`; // top = Current top + Element height - Element padding bottom
170
181
  el.style.left = `${slateRect.left}px`;
171
182
  el.style.display = 'block';
172
183
  } else {
@@ -177,7 +188,7 @@ function AIModule(_ref) {
177
188
  el.style.left = `${slateRect.left}px`;
178
189
  el.style.display = 'block';
179
190
  }
180
- }, [editor, element, selectDom]);
191
+ }, [editor, element]);
181
192
  (0, _react.useEffect)(() => {
182
193
  let observerRefValue = null;
183
194
  if (isShowAIPopover) {
@@ -9,8 +9,7 @@ var _react = _interopRequireDefault(require("react"));
9
9
  var _caret = _interopRequireDefault(require("./caret"));
10
10
  var _constants = require("../../constants");
11
11
  var _helpers = require("../font/helpers");
12
- var _InlineBugFixWrapper = _interopRequireDefault(require("../../commons/Inline-bug-fix-wrapper"));
13
- const renderText = (props, editor) => {
12
+ const renderText = props => {
14
13
  const {
15
14
  attributes,
16
15
  children,
@@ -35,6 +34,12 @@ const renderText = (props, editor) => {
35
34
  style['display'] = 'inline-block';
36
35
  style['minWidth'] = '2px';
37
36
  }
37
+
38
+ // Add temporary marks for selection in AI
39
+ if (leaf.sdoc_ai && leaf.text.trim()) {
40
+ style['padding'] = '3.23px 0';
41
+ style['background'] = '#a9c9ed';
42
+ }
38
43
  if (leaf.computed_background_color) {
39
44
  style['backgroundColor'] = leaf.computed_background_color;
40
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "2.0.43",
3
+ "version": "2.0.44",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",