@seafile/sdoc-editor 0.1.70 → 0.1.71

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.
@@ -111,8 +111,7 @@ var SDocEditor = function SDocEditor(_ref) {
111
111
  docUuid: config.docUuid
112
112
  }), /*#__PURE__*/React.createElement("div", {
113
113
  ref: scrollRef,
114
- className: "sdoc-editor-article-container",
115
- id: "sdoc-editor-article-container"
114
+ className: "sdoc-editor-article-container"
116
115
  }, /*#__PURE__*/React.createElement(ScrollContext.Provider, {
117
116
  value: {
118
117
  scrollRef: scrollRef
@@ -3,12 +3,15 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
3
  import React, { useCallback, useEffect, useState, useRef } from 'react';
4
4
  import { ReactEditor, useSlateStatic, useReadOnly } from '@seafile/slate-react';
5
5
  import { Transforms } from '@seafile/slate';
6
+ import { useScrollContext } from '../../../hooks/use-scroll-context';
6
7
  import CodeBlockHoverMenu from '../../../../components/code-block-hover-menu';
7
8
  import '../../../assets/css/code-block.css';
8
9
  var CodeBlock = function CodeBlock(_ref) {
9
10
  var codeBlockProps = _ref.codeBlockProps;
10
11
  var readOnly = useReadOnly();
12
+ var editor = useSlateStatic();
11
13
  var codeBlockRef = useRef();
14
+ var scrollRef = useScrollContext();
12
15
  var attributes = codeBlockProps.attributes,
13
16
  children = codeBlockProps.children,
14
17
  element = codeBlockProps.element;
@@ -17,7 +20,6 @@ var CodeBlock = function CodeBlock(_ref) {
17
20
  white_space: 'nowrap'
18
21
  } : _element$style;
19
22
  var white_space = style.white_space;
20
- var editor = useSlateStatic();
21
23
  var _useState = useState({
22
24
  top: '',
23
25
  left: ''
@@ -97,15 +99,12 @@ var CodeBlock = function CodeBlock(_ref) {
97
99
  // eslint-disable-next-line react-hooks/exhaustive-deps
98
100
  }, []);
99
101
  useEffect(function () {
102
+ if (readOnly) return;
100
103
  if (showHoverMenu) {
101
- document.getElementById('sdoc-editor-article-container').addEventListener('scroll', onScroll);
104
+ scrollRef.current && scrollRef.current.addEventListener('scroll', onScroll);
102
105
  } else {
103
- document.getElementById('sdoc-editor-article-container').removeEventListener('scroll', onScroll);
106
+ scrollRef.current && scrollRef.current.removeEventListener('scroll', onScroll);
104
107
  }
105
- return function () {
106
- var articleContainer = document.getElementById('sdoc-editor-article-container');
107
- articleContainer && articleContainer.removeEventListener('scroll', onScroll);
108
- };
109
108
  // eslint-disable-next-line react-hooks/exhaustive-deps
110
109
  }, [showHoverMenu]);
111
110
  return /*#__PURE__*/React.createElement("div", {
@@ -1,27 +1,54 @@
1
1
  import slugid from 'slugid';
2
2
  import { CODE_BLOCK, CODE_LINE } from '../../../constants';
3
+ import { genCodeLangs } from '../../../../utils/prismjs';
3
4
  var codeBlockRule = function codeBlockRule(element, parseChild) {
4
5
  var nodeName = element.nodeName,
5
6
  childNodes = element.childNodes;
6
7
  if (nodeName === 'PRE') {
8
+ var children = Array.from(childNodes).filter(function (item) {
9
+ return item.nodeName === 'CODE';
10
+ });
11
+ var codeChild = children[0];
12
+ var lang = codeChild.getAttribute('lang');
13
+ lang = genCodeLangs().find(function (item) {
14
+ return item.value === lang;
15
+ }) || 'plaintext';
7
16
  return {
8
17
  level: 'level1',
9
18
  id: slugid.nice(),
19
+ language: lang,
10
20
  type: CODE_BLOCK,
11
- children: parseChild(childNodes)
21
+ children: parseChild(children)
12
22
  };
13
23
  }
14
24
  if (nodeName === 'CODE' && element.parentElement.nodeName === 'PRE') {
15
- return {
16
- level: 'level2',
17
- id: slugid.nice(),
18
- type: CODE_LINE,
19
- children: [{
20
- level: 'level3',
25
+ var content = element.textContent;
26
+ var hasNewLine = content.indexOf('\n') > -1;
27
+ if (!hasNewLine) {
28
+ return {
29
+ level: 'level2',
21
30
  id: slugid.nice(),
22
- text: element.textContent
23
- }]
24
- };
31
+ type: CODE_LINE,
32
+ children: [{
33
+ level: 'level3',
34
+ id: slugid.nice(),
35
+ text: element.textContent
36
+ }]
37
+ };
38
+ }
39
+ var codes = content.split('\n');
40
+ return codes.map(function (item) {
41
+ return {
42
+ level: 'level2',
43
+ id: slugid.nice(),
44
+ type: CODE_LINE,
45
+ children: [{
46
+ level: 'level3',
47
+ id: slugid.nice(),
48
+ text: item
49
+ }]
50
+ };
51
+ });
25
52
  }
26
53
  return;
27
54
  };
@@ -34,6 +34,7 @@ var listRule = function listRule(element, parseChild) {
34
34
  id: slugid.nice(),
35
35
  type: LIST_ITEM,
36
36
  children: [{
37
+ id: slugid.nice(),
37
38
  level: 'level3',
38
39
  type: LIST_LIC,
39
40
  children: parseChild(childNodes)
@@ -1,30 +1,21 @@
1
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- import React, { useRef, useEffect, useState } from 'react';
1
+ import React, { useRef, useEffect } from 'react';
3
2
  import { Editable, Slate } from '@seafile/slate-react';
4
3
  import defaultEditor, { renderLeaf as _renderLeaf, renderElement as _renderElement } from '../extension';
5
- import { withSocketIO } from '../socket';
6
4
  import withNodeId from '../node-id';
5
+ import { ScrollContext } from '../hooks/use-scroll-context';
6
+ import { generateDefaultDocContent } from '../../utils';
7
7
  import '../assets/css/layout.css';
8
8
  import '../assets/css/sdoc-editor-plugins.css';
9
- import { generateDefaultDocContent } from '../../utils';
10
9
  var SDocViewer = function SDocViewer(_ref) {
11
- var isOpenSocket = _ref.isOpenSocket,
12
- document = _ref.document,
13
- config = _ref.config,
10
+ var document = _ref.document,
14
11
  customRenderLeaf = _ref.renderLeaf,
15
12
  customRenderElement = _ref.renderElement;
16
- var editor = !isOpenSocket ? withNodeId(defaultEditor) : withSocketIO(withNodeId(defaultEditor, {
17
- document: document,
18
- config: config
19
- }));
13
+ var editor = withNodeId(defaultEditor);
20
14
  var slateValue = (document || generateDefaultDocContent()).children;
21
15
  var articleRef = useRef(null);
22
- var _useState = useState(false),
23
- _useState2 = _slicedToArray(_useState, 2),
24
- setUpdate = _useState2[1];
16
+ var scrollRef = useRef(null);
25
17
  useEffect(function () {
26
18
  editor.width = articleRef.current.children[0].clientWidth;
27
- setUpdate(true);
28
19
  // eslint-disable-next-line react-hooks/exhaustive-deps
29
20
  }, []);
30
21
  return /*#__PURE__*/React.createElement("div", {
@@ -32,12 +23,15 @@ var SDocViewer = function SDocViewer(_ref) {
32
23
  }, /*#__PURE__*/React.createElement("div", {
33
24
  className: "sdoc-editor-content"
34
25
  }, /*#__PURE__*/React.createElement("div", {
35
- className: "flex-fill o-auto sdoc-editor-article-container",
36
- id: "sdoc-editor-article-container"
26
+ ref: scrollRef,
27
+ className: "sdoc-editor-article-container"
28
+ }, /*#__PURE__*/React.createElement(ScrollContext.Provider, {
29
+ value: {
30
+ scrollRef: scrollRef
31
+ }
37
32
  }, /*#__PURE__*/React.createElement(Slate, {
38
33
  editor: editor,
39
- value: slateValue,
40
- onChange: function onChange() {}
34
+ value: slateValue
41
35
  }, /*#__PURE__*/React.createElement("div", {
42
36
  className: "article mx-auto",
43
37
  ref: articleRef
@@ -51,6 +45,6 @@ var SDocViewer = function SDocViewer(_ref) {
51
45
  return (customRenderLeaf || _renderLeaf)(props, editor);
52
46
  },
53
47
  onDOMBeforeInput: function onDOMBeforeInput(event) {}
54
- }))))));
48
+ })))))));
55
49
  };
56
50
  export default SDocViewer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.1.70",
3
+ "version": "0.1.71",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",