@manuscripts/body-editor 1.6.2 → 1.6.3

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.
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ /*!
3
+ * © 2019 Atypon Systems LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.CollabProvider = void 0;
19
+ class CollabProvider {
20
+ }
21
+ exports.CollabProvider = CollabProvider;
@@ -20,6 +20,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
21
  require("prosemirror-gapcursor/style/gapcursor.css");
22
22
  require("prosemirror-tables/style/tables.css");
23
+ const prosemirror_collab_1 = require("prosemirror-collab");
23
24
  const prosemirror_dropcursor_1 = require("prosemirror-dropcursor");
24
25
  const prosemirror_history_1 = require("prosemirror-history");
25
26
  const prosemirror_tables_1 = require("prosemirror-tables");
@@ -42,7 +43,7 @@ const tracking_mark_1 = __importDefault(require("../plugins/tracking-mark"));
42
43
  const rules_1 = __importDefault(require("../rules"));
43
44
  exports.default = (props) => {
44
45
  const plugins = props.plugins || [];
45
- return [
46
+ const allPlugins = [
46
47
  rules_1.default,
47
48
  ...keys_1.default,
48
49
  (0, prosemirror_dropcursor_1.dropCursor)(),
@@ -65,6 +66,10 @@ exports.default = (props) => {
65
66
  (0, tracking_mark_1.default)(),
66
67
  (0, footnotes_1.default)(),
67
68
  ];
69
+ if (props.collabProvider) {
70
+ allPlugins.push((0, prosemirror_collab_1.collab)({ version: props.collabProvider.currentVersion }));
71
+ }
72
+ return allPlugins;
68
73
  };
69
74
  document.execCommand('enableObjectResizing', false, 'false');
70
75
  document.execCommand('enableInlineTableEditing', false, 'false');
package/dist/cjs/index.js CHANGED
@@ -32,7 +32,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
32
32
  return (mod && mod.__esModule) ? mod : { "default": mod };
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.ManuscriptsEditor = exports.useEditor = exports.isHighlightComment = exports.isThereSelector = exports.commentScroll = exports.toolbar = exports.PopperManager = exports.getMenus = exports.ManuscriptToolbar = exports.OutlineManuscript = exports.OutlineItemIcon = exports.ManuscriptOutline = exports.DialogNames = exports.DialogController = void 0;
35
+ exports.ManuscriptsEditor = exports.useEditor = exports.isHighlightComment = exports.isThereSelector = exports.commentScroll = exports.toolbar = exports.PopperManager = exports.CollabProvider = exports.getMenus = exports.ManuscriptToolbar = exports.OutlineManuscript = exports.OutlineItemIcon = exports.ManuscriptOutline = exports.DialogNames = exports.DialogController = void 0;
36
36
  __exportStar(require("./citation-sources"), exports);
37
37
  __exportStar(require("./commands"), exports);
38
38
  __exportStar(require("./components/application-menu"), exports);
@@ -49,6 +49,8 @@ var ManuscriptToolbar_1 = require("./components/toolbar/ManuscriptToolbar");
49
49
  Object.defineProperty(exports, "ManuscriptToolbar", { enumerable: true, get: function () { return ManuscriptToolbar_1.ManuscriptToolbar; } });
50
50
  var menus_1 = require("./menus");
51
51
  Object.defineProperty(exports, "getMenus", { enumerable: true, get: function () { return __importDefault(menus_1).default; } });
52
+ var collabProvider_1 = require("./classes/collabProvider");
53
+ Object.defineProperty(exports, "CollabProvider", { enumerable: true, get: function () { return collabProvider_1.CollabProvider; } });
52
54
  var popper_1 = require("./lib/popper");
53
55
  Object.defineProperty(exports, "PopperManager", { enumerable: true, get: function () { return popper_1.PopperManager; } });
54
56
  var toolbar_1 = require("./toolbar");
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useDoWithDebounce = void 0;
4
+ const react_1 = require("react");
5
+ const useDoWithDebounce = () => {
6
+ const debounced = (0, react_1.useRef)(() => undefined);
7
+ const timeout = (0, react_1.useRef)();
8
+ const doWithDebounce = (fn, interval = 1000, flush = false) => {
9
+ debounced.current = fn;
10
+ if (flush) {
11
+ fn();
12
+ debounced.current = () => undefined;
13
+ window.clearTimeout(timeout.current);
14
+ }
15
+ window.clearTimeout(timeout.current);
16
+ timeout.current = window.setTimeout(() => {
17
+ debounced.current();
18
+ timeout.current = 0;
19
+ }, interval);
20
+ };
21
+ return doWithDebounce;
22
+ };
23
+ exports.useDoWithDebounce = useDoWithDebounce;
@@ -14,23 +14,62 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
17
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
+ const track_changes_plugin_1 = require("@manuscripts/track-changes-plugin");
28
+ const prosemirror_collab_1 = require("prosemirror-collab");
18
29
  const prosemirror_state_1 = require("prosemirror-state");
19
30
  const react_1 = require("react");
20
31
  const react_router_dom_1 = require("react-router-dom");
32
+ const use_do_with_debounce_1 = require("./lib/use-do-with-debounce");
21
33
  const bibliography_1 = require("./plugins/bibliography");
22
- const useEditor = (initialState, createView) => {
34
+ const useEditor = (initialState, createView, editorProps) => {
23
35
  const view = (0, react_1.useRef)();
24
36
  const [state, setState] = (0, react_1.useState)(initialState);
25
37
  const [viewElement, setViewElement] = (0, react_1.useState)(null);
26
38
  const history = (0, react_router_dom_1.useHistory)();
39
+ const { collabProvider } = editorProps;
40
+ if (collabProvider) {
41
+ collabProvider.onNewSteps((newVersion, steps, clientIDs) => __awaiter(void 0, void 0, void 0, function* () {
42
+ if (state && view.current) {
43
+ const localVersion = (0, prosemirror_collab_1.getVersion)(view.current.state);
44
+ const since = yield collabProvider.stepsSince(localVersion);
45
+ if ((since === null || since === void 0 ? void 0 : since.steps) && since.clientIDs) {
46
+ view.current.dispatch((0, prosemirror_collab_1.receiveTransaction)(view.current.state, since === null || since === void 0 ? void 0 : since.steps, since.clientIDs));
47
+ }
48
+ else {
49
+ console.warn('Inconsistent new steps event from the authority.');
50
+ }
51
+ }
52
+ }));
53
+ }
54
+ const debounce = (0, use_do_with_debounce_1.useDoWithDebounce)();
27
55
  const dispatch = (0, react_1.useCallback)((tr) => {
28
56
  if (!view.current) {
29
57
  return state;
30
58
  }
31
59
  const nextState = view.current.state.apply(tr);
32
60
  view.current.updateState(nextState);
33
- setState(nextState);
61
+ const trackState = track_changes_plugin_1.trackChangesPluginKey.getState(view.current.state);
62
+ if (collabProvider &&
63
+ trackState &&
64
+ trackState.status !== track_changes_plugin_1.TrackChangesStatus.viewSnapshots) {
65
+ const sendable = (0, prosemirror_collab_1.sendableSteps)(nextState);
66
+ if (sendable) {
67
+ collabProvider.sendSteps(sendable.version, sendable.steps, sendable.clientID, !tr.isGeneric);
68
+ }
69
+ }
70
+ debounce(() => {
71
+ setState(nextState);
72
+ }, 200, !tr.isGeneric);
34
73
  return state;
35
74
  }, []);
36
75
  const replaceView = (state, createView) => {
@@ -0,0 +1,17 @@
1
+ /*!
2
+ * © 2019 Atypon Systems LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export class CollabProvider {
17
+ }
@@ -15,6 +15,7 @@
15
15
  */
16
16
  import 'prosemirror-gapcursor/style/gapcursor.css';
17
17
  import 'prosemirror-tables/style/tables.css';
18
+ import { collab } from 'prosemirror-collab';
18
19
  import { dropCursor } from 'prosemirror-dropcursor';
19
20
  import { history } from 'prosemirror-history';
20
21
  import { tableEditing } from 'prosemirror-tables';
@@ -37,7 +38,7 @@ import tracking_mark from '../plugins/tracking-mark';
37
38
  import rules from '../rules';
38
39
  export default (props) => {
39
40
  const plugins = props.plugins || [];
40
- return [
41
+ const allPlugins = [
41
42
  rules,
42
43
  ...keys,
43
44
  dropCursor(),
@@ -60,6 +61,10 @@ export default (props) => {
60
61
  tracking_mark(),
61
62
  footnotes(),
62
63
  ];
64
+ if (props.collabProvider) {
65
+ allPlugins.push(collab({ version: props.collabProvider.currentVersion }));
66
+ }
67
+ return allPlugins;
63
68
  };
64
69
  document.execCommand('enableObjectResizing', false, 'false');
65
70
  document.execCommand('enableInlineTableEditing', false, 'false');
package/dist/es/index.js CHANGED
@@ -22,6 +22,7 @@ export { OutlineItemIcon } from './components/outline/Outline';
22
22
  export { OutlineManuscript } from './components/outline/OutlineManuscript';
23
23
  export { ManuscriptToolbar, } from './components/toolbar/ManuscriptToolbar';
24
24
  export { default as getMenus } from './menus';
25
+ export { CollabProvider } from './classes/collabProvider';
25
26
  export { PopperManager } from './lib/popper';
26
27
  export { toolbar } from './toolbar';
27
28
  export * from './plugins/highlight';
@@ -0,0 +1,19 @@
1
+ import { useRef } from 'react';
2
+ export const useDoWithDebounce = () => {
3
+ const debounced = useRef(() => undefined);
4
+ const timeout = useRef();
5
+ const doWithDebounce = (fn, interval = 1000, flush = false) => {
6
+ debounced.current = fn;
7
+ if (flush) {
8
+ fn();
9
+ debounced.current = () => undefined;
10
+ window.clearTimeout(timeout.current);
11
+ }
12
+ window.clearTimeout(timeout.current);
13
+ timeout.current = window.setTimeout(() => {
14
+ debounced.current();
15
+ timeout.current = 0;
16
+ }, interval);
17
+ };
18
+ return doWithDebounce;
19
+ };
@@ -13,22 +13,61 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ import { trackChangesPluginKey, TrackChangesStatus, } from '@manuscripts/track-changes-plugin';
26
+ import { getVersion, receiveTransaction, sendableSteps, } from 'prosemirror-collab';
16
27
  import { NodeSelection, } from 'prosemirror-state';
17
28
  import { useCallback, useEffect, useRef, useState } from 'react';
18
29
  import { useHistory } from 'react-router-dom';
30
+ import { useDoWithDebounce } from './lib/use-do-with-debounce';
19
31
  import { bibliographyKey } from './plugins/bibliography';
20
- const useEditor = (initialState, createView) => {
32
+ const useEditor = (initialState, createView, editorProps) => {
21
33
  const view = useRef();
22
34
  const [state, setState] = useState(initialState);
23
35
  const [viewElement, setViewElement] = useState(null);
24
36
  const history = useHistory();
37
+ const { collabProvider } = editorProps;
38
+ if (collabProvider) {
39
+ collabProvider.onNewSteps((newVersion, steps, clientIDs) => __awaiter(void 0, void 0, void 0, function* () {
40
+ if (state && view.current) {
41
+ const localVersion = getVersion(view.current.state);
42
+ const since = yield collabProvider.stepsSince(localVersion);
43
+ if ((since === null || since === void 0 ? void 0 : since.steps) && since.clientIDs) {
44
+ view.current.dispatch(receiveTransaction(view.current.state, since === null || since === void 0 ? void 0 : since.steps, since.clientIDs));
45
+ }
46
+ else {
47
+ console.warn('Inconsistent new steps event from the authority.');
48
+ }
49
+ }
50
+ }));
51
+ }
52
+ const debounce = useDoWithDebounce();
25
53
  const dispatch = useCallback((tr) => {
26
54
  if (!view.current) {
27
55
  return state;
28
56
  }
29
57
  const nextState = view.current.state.apply(tr);
30
58
  view.current.updateState(nextState);
31
- setState(nextState);
59
+ const trackState = trackChangesPluginKey.getState(view.current.state);
60
+ if (collabProvider &&
61
+ trackState &&
62
+ trackState.status !== TrackChangesStatus.viewSnapshots) {
63
+ const sendable = sendableSteps(nextState);
64
+ if (sendable) {
65
+ collabProvider.sendSteps(sendable.version, sendable.steps, sendable.clientID, !tr.isGeneric);
66
+ }
67
+ }
68
+ debounce(() => {
69
+ setState(nextState);
70
+ }, 200, !tr.isGeneric);
32
71
  return state;
33
72
  }, []);
34
73
  const replaceView = (state, createView) => {
@@ -0,0 +1,27 @@
1
+ /*!
2
+ * © 2019 Atypon Systems LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { Step } from 'prosemirror-transform';
17
+ export declare abstract class CollabProvider {
18
+ currentVersion: number;
19
+ protected newStepsListener: (version: number, steps: Step[], clientIDs: number[]) => void;
20
+ abstract sendSteps(version: number, steps: readonly Step[], clientID: string | number, flush?: boolean): Promise<void>;
21
+ abstract onNewSteps(listener: CollabProvider['newStepsListener']): void;
22
+ abstract stepsSince(version: number): Promise<{
23
+ steps: Step[];
24
+ clientIDs: number[];
25
+ version: number;
26
+ } | undefined>;
27
+ }
@@ -21,6 +21,7 @@ import { History } from 'history';
21
21
  import { EditorState, Plugin } from 'prosemirror-state';
22
22
  import React from 'react';
23
23
  import { DefaultTheme } from 'styled-components';
24
+ import { CollabProvider } from '../classes/collabProvider';
24
25
  import { PopperManager } from '../lib/popper';
25
26
  import { CreateView } from '../useEditor';
26
27
  export type CSLProps = {
@@ -55,6 +56,7 @@ export interface EditorProps {
55
56
  setEditorSelectedSuggestion: (id?: string) => void;
56
57
  retrySync: (componentIDs: string[]) => Promise<void>;
57
58
  environment?: string;
59
+ collabProvider?: CollabProvider;
58
60
  }
59
61
  declare const _default: {
60
62
  createState: (props: EditorProps) => EditorState;
@@ -19,6 +19,7 @@ import { CommentAnnotation, Manuscript, Model } from '@manuscripts/json-schema';
19
19
  import { Capabilities } from '@manuscripts/style-guide';
20
20
  import { Build, ManuscriptSchema } from '@manuscripts/transform';
21
21
  import { Plugin } from 'prosemirror-state';
22
+ import { CollabProvider } from '../classes/collabProvider';
22
23
  import { PopperManager } from '../lib/popper';
23
24
  import { CSLProps } from './ManuscriptsEditor';
24
25
  interface PluginProps {
@@ -33,6 +34,7 @@ interface PluginProps {
33
34
  plugins?: Plugin<ManuscriptSchema>[];
34
35
  cslProps: CSLProps;
35
36
  popper: PopperManager;
37
+ collabProvider?: CollabProvider;
36
38
  }
37
39
  declare const _default: (props: PluginProps) => any[];
38
40
  export default _default;
@@ -23,6 +23,7 @@ export { OutlineManuscript } from './components/outline/OutlineManuscript';
23
23
  export { ManuscriptToolbar, ToolbarButtonConfig, } from './components/toolbar/ManuscriptToolbar';
24
24
  export { default as getMenus } from './menus';
25
25
  export { ChangeReceiver } from './types';
26
+ export { CollabProvider } from './classes/collabProvider';
26
27
  export { PopperManager } from './lib/popper';
27
28
  export { toolbar } from './toolbar';
28
29
  export * from './plugins/highlight';
@@ -0,0 +1 @@
1
+ export declare const useDoWithDebounce: () => (fn: () => void, interval?: number, flush?: boolean) => void;
@@ -15,8 +15,9 @@
15
15
  */
16
16
  import { Command, EditorState, Transaction } from 'prosemirror-state';
17
17
  import { EditorView } from 'prosemirror-view';
18
+ import { EditorProps } from './configs/ManuscriptsEditor';
18
19
  export type CreateView = (element: HTMLDivElement, state: EditorState, dispatch: (tr: Transaction) => EditorState) => EditorView;
19
- declare const useEditor: (initialState: EditorState, createView: CreateView) => {
20
+ declare const useEditor: (initialState: EditorState, createView: CreateView, editorProps: EditorProps) => {
20
21
  state: EditorState;
21
22
  onRender: (el: HTMLDivElement | null) => void;
22
23
  isCommandValid: (command: Command) => boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@manuscripts/body-editor",
3
3
  "description": "Prosemirror components for editing and viewing manuscripts",
4
- "version": "1.6.2",
4
+ "version": "1.6.3",
5
5
  "repository": "github:Atypon-OpenSource/manuscripts-body-editor",
6
6
  "license": "Apache-2.0",
7
7
  "main": "dist/cjs",
@@ -20,6 +20,7 @@
20
20
  "lint:fix": "eslint src --fix --ext .ts,.tsx --max-warnings=0",
21
21
  "prebuild": "rimraf dist",
22
22
  "prettier": "prettier --write 'src/**/*.{ts,tsx}'",
23
+ "prepare": "husky install",
23
24
  "preversion": "concurrently 'yarn:typecheck' 'yarn:lint' 'yarn:test'",
24
25
  "test": "jest --runInBand",
25
26
  "typecheck": "tsc --noEmit",
@@ -43,6 +44,7 @@
43
44
  "lodash-es": "^4.17.21",
44
45
  "popper.js": "^1.16.1",
45
46
  "pretty-bytes": "^6.0.0",
47
+ "prosemirror-collab": "^1.3.1",
46
48
  "prosemirror-commands": "^1.5.0",
47
49
  "prosemirror-dropcursor": "^1.6.1",
48
50
  "prosemirror-history": "^1.3.0",
@@ -69,8 +71,8 @@
69
71
  "@babel/preset-env": "^7.20.2",
70
72
  "@babel/preset-react": "^7.18.6",
71
73
  "@babel/preset-typescript": "^7.18.6",
72
- "@manuscripts/eslint-config": "^0.5.1",
73
74
  "@inline-svg-unique-id/react": "^1.2.3",
75
+ "@manuscripts/eslint-config": "^0.5.1",
74
76
  "@types/codemirror": "^5.60.5",
75
77
  "@types/dompurify": "^2.4.0",
76
78
  "@types/enzyme": "^3.10.12",