@seafile/sdoc-editor 0.1.104 → 0.1.105

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.
@@ -1,5 +1,5 @@
1
1
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+ import React, { useCallback, useEffect, useMemo, useRef, useState, forwardRef, useImperativeHandle } from 'react';
3
3
  import { Editable, ReactEditor, Slate } from '@seafile/slate-react';
4
4
  import defaultEditor, { renderLeaf, renderElement, Toolbar, ContextToolbar } from './extension';
5
5
  import { focusEditor, getAboveBlockNode, getNextNode, getPrevNode, isSelectionAtBlockEnd, isSelectionAtBlockStart } from './extension/core';
@@ -20,7 +20,7 @@ import { ColorProvider } from './hooks/use-color-context';
20
20
  import './assets/css/layout.css';
21
21
  import './assets/css/sdoc-editor-plugins.css';
22
22
  import './assets/css/dropdown-menu.css';
23
- var SDocEditor = function SDocEditor(_ref) {
23
+ var SDocEditor = forwardRef(function (_ref, ref) {
24
24
  var document = _ref.document,
25
25
  config = _ref.config;
26
26
  // init editor
@@ -51,6 +51,16 @@ var SDocEditor = function SDocEditor(_ref) {
51
51
  return new EventProxy(editor);
52
52
  }, []);
53
53
 
54
+ // The parent component can call the method of this component through ref
55
+ useImperativeHandle(ref, function () {
56
+ return {
57
+ // get latest value
58
+ getValue: function getValue() {
59
+ return slateValue;
60
+ }
61
+ };
62
+ }, [slateValue]);
63
+
54
64
  // useMount: init socket connection
55
65
  useEffect(function () {
56
66
  editor.openConnection();
@@ -246,5 +256,5 @@ var SDocEditor = function SDocEditor(_ref) {
246
256
  decorate: decorate,
247
257
  onCut: eventProxy.onCut
248
258
  })), /*#__PURE__*/React.createElement(CommentWrapper, null)))))))))));
249
- };
259
+ });
250
260
  export default SDocEditor;
@@ -89,6 +89,7 @@ var CodeBlock = function CodeBlock(_ref) {
89
89
  }, []);
90
90
  var onScroll = useCallback(function (e) {
91
91
  if (readOnly) return;
92
+ if (!showHoverMenu) return;
92
93
  if (e.currentTarget.scrollTop) {
93
94
  var _codeBlockRef$current2 = codeBlockRef.current.getBoundingClientRect(),
94
95
  top = _codeBlockRef$current2.top,
@@ -101,17 +102,18 @@ var CodeBlock = function CodeBlock(_ref) {
101
102
 
102
103
  setMenuPosition(newMenuPosition);
103
104
  }
104
- // eslint-disable-next-line react-hooks/exhaustive-deps
105
- }, []);
105
+ }, [readOnly, showHoverMenu]);
106
106
  useEffect(function () {
107
107
  if (readOnly) return;
108
- if (showHoverMenu) {
109
- scrollRef.current && scrollRef.current.addEventListener('scroll', onScroll);
110
- } else {
111
- scrollRef.current && scrollRef.current.removeEventListener('scroll', onScroll);
108
+ var observerRefValue = null;
109
+ if (scrollRef.current) {
110
+ scrollRef.current.addEventListener('scroll', onScroll);
111
+ observerRefValue = scrollRef.current;
112
112
  }
113
- // eslint-disable-next-line react-hooks/exhaustive-deps
114
- }, [showHoverMenu]);
113
+ return function () {
114
+ observerRefValue.removeEventListener('scroll', onScroll);
115
+ };
116
+ }, [onScroll, readOnly, scrollRef]);
115
117
  var isEntering = useCallback(function (e) {
116
118
  return !editor.operations.every(function (operation) {
117
119
  return operation.type === 'set_selection';
@@ -1,6 +1,8 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
1
2
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- import React, { useCallback, useEffect, useState } from 'react';
3
+ import React, { useCallback, useEffect, useRef, useState } from 'react';
3
4
  import { withTranslation } from 'react-i18next';
5
+ import deepCopy from 'deep-copy';
4
6
  import { SDocEditor } from '../basic-sdk';
5
7
  import Loading from '../components/loading';
6
8
  import DocInfo from '../components/doc-info';
@@ -15,6 +17,7 @@ var SimpleEditor = function SimpleEditor(_ref) {
15
17
  var isStarred = _ref.isStarred,
16
18
  isDraft = _ref.isDraft,
17
19
  t = _ref.t;
20
+ var editorRef = useRef(null);
18
21
  var _useState = useState(true),
19
22
  _useState2 = _slicedToArray(_useState, 2),
20
23
  isFirstLoad = _useState2[0],
@@ -77,35 +80,28 @@ var SimpleEditor = function SimpleEditor(_ref) {
77
80
  var initChangesData = useCallback(function () {
78
81
  setShowChanges(true);
79
82
  context.getSeadocRevisionDownloadLinks().then(function (res) {
80
- var _res$data = res.data,
81
- fileDownloadLink = _res$data.file_download_link,
82
- originFileDownloadLink = _res$data.origin_file_download_link;
83
- fetch(fileDownloadLink).then(function (res) {
84
- return res.json();
85
- }).then(function (revisionContent) {
86
- fetch(originFileDownloadLink).then(function (res) {
87
- return res.json();
88
- }).then(function (originContent) {
89
- setCurrentContent(revisionContent);
90
- setLastContent(originContent);
91
- setErrorMessage(null);
92
- setContextInit(true);
93
- }).catch(function (error) {
94
- setErrorMessage('Load_doc_content_error');
95
- setContextInit(true);
96
- });
97
- }).catch(function (error) {
98
- setErrorMessage('Load_doc_content_error');
99
- setContextInit(true);
100
- });
83
+ var originFileDownloadLink = res.data.origin_file_download_link;
84
+ return fetch(originFileDownloadLink);
85
+ }).then(function (res) {
86
+ return res.json();
87
+ }).then(function (originContent) {
88
+ setLastContent(originContent);
89
+ setErrorMessage(null);
90
+ setContextInit(true);
101
91
  }).catch(function (error) {
102
92
  setErrorMessage('Load_doc_content_error');
103
93
  setContextInit(true);
104
94
  });
105
95
 
106
96
  // eslint-disable-next-line react-hooks/exhaustive-deps
107
- }, []);
97
+ }, [document]);
108
98
  var toggleViewChanges = useCallback(function (isShowChanges) {
99
+ if (isShowChanges) {
100
+ var newestValue = editorRef.current.getValue();
101
+ setCurrentContent(deepCopy(_objectSpread(_objectSpread({}, document), {}, {
102
+ children: newestValue
103
+ })));
104
+ }
109
105
  setContextInit(false);
110
106
  if (isShowChanges) {
111
107
  initChangesData();
@@ -114,7 +110,7 @@ var SimpleEditor = function SimpleEditor(_ref) {
114
110
  initDocumentData(false);
115
111
 
116
112
  // eslint-disable-next-line react-hooks/exhaustive-deps
117
- }, []);
113
+ }, [document]);
118
114
  if (isFirstLoad && !isContextInit) {
119
115
  return /*#__PURE__*/React.createElement(Loading, null);
120
116
  }
@@ -140,6 +136,7 @@ var SimpleEditor = function SimpleEditor(_ref) {
140
136
  }) : /*#__PURE__*/React.createElement(SDocEditor, {
141
137
  config: context.getEditorConfig(),
142
138
  document: document,
139
+ ref: editorRef,
143
140
  isOpenSocket: context.getSetting('isOpenSocket')
144
141
  })))));
145
142
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.1.104",
3
+ "version": "0.1.105",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",