@datalayer/jupyter-lexical 0.0.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.
- package/LICENSE +21 -0
- package/README.md +7 -0
- package/lib/components/InsertEquationDialog.d.ts +6 -0
- package/lib/components/InsertEquationDialog.js +17 -0
- package/lib/components/InsertEquationDialog.js.map +1 -0
- package/lib/components/InsertImageDialog.d.ts +13 -0
- package/lib/components/InsertImageDialog.js +48 -0
- package/lib/components/InsertImageDialog.js.map +1 -0
- package/lib/components/JupyterCellComponent.d.ts +2 -0
- package/lib/components/JupyterCellComponent.js +12 -0
- package/lib/components/JupyterCellComponent.js.map +1 -0
- package/lib/components/JupyterOutputComponent.d.ts +13 -0
- package/lib/components/JupyterOutputComponent.js +8 -0
- package/lib/components/JupyterOutputComponent.js.map +1 -0
- package/lib/convert/LexicalToNbFormat.d.ts +4 -0
- package/lib/convert/LexicalToNbFormat.js +100 -0
- package/lib/convert/LexicalToNbFormat.js.map +1 -0
- package/lib/convert/NbFormatToLexical.d.ts +4 -0
- package/lib/convert/NbFormatToLexical.js +31 -0
- package/lib/convert/NbFormatToLexical.js.map +1 -0
- package/lib/convert/markdown/MarkdownExport.d.ts +11 -0
- package/lib/convert/markdown/MarkdownExport.js +129 -0
- package/lib/convert/markdown/MarkdownExport.js.map +1 -0
- package/lib/convert/markdown/MarkdownImport.d.ts +2 -0
- package/lib/convert/markdown/MarkdownImport.js +246 -0
- package/lib/convert/markdown/MarkdownImport.js.map +1 -0
- package/lib/convert/markdown/MarkdownShortcuts.d.ts +10 -0
- package/lib/convert/markdown/MarkdownShortcuts.js +254 -0
- package/lib/convert/markdown/MarkdownShortcuts.js.map +1 -0
- package/lib/convert/markdown/MarkdownTransformers.d.ts +39 -0
- package/lib/convert/markdown/MarkdownTransformers.js +242 -0
- package/lib/convert/markdown/MarkdownTransformers.js.map +1 -0
- package/lib/convert/markdown/convertToMarkdown.d.ts +1 -0
- package/lib/convert/markdown/convertToMarkdown.js +126 -0
- package/lib/convert/markdown/convertToMarkdown.js.map +1 -0
- package/lib/convert/markdown/index.d.ts +17 -0
- package/lib/convert/markdown/index.js +46 -0
- package/lib/convert/markdown/index.js.map +1 -0
- package/lib/convert/markdown/utils.d.ts +24 -0
- package/lib/convert/markdown/utils.js +303 -0
- package/lib/convert/markdown/utils.js.map +1 -0
- package/lib/convert/transformers/MarkdownTransformers.d.ts +13 -0
- package/lib/convert/transformers/MarkdownTransformers.js +178 -0
- package/lib/convert/transformers/MarkdownTransformers.js.map +1 -0
- package/lib/examples/App.d.ts +1 -0
- package/lib/examples/App.js +22 -0
- package/lib/examples/App.js.map +1 -0
- package/lib/examples/Editor.d.ts +9 -0
- package/lib/examples/Editor.js +81 -0
- package/lib/examples/Editor.js.map +1 -0
- package/lib/examples/Example.d.ts +1 -0
- package/lib/examples/Example.js +11 -0
- package/lib/examples/Example.js.map +1 -0
- package/lib/examples/content/Example.ipynb.json +98 -0
- package/lib/examples/content/Example.lexical.json +259 -0
- package/lib/examples/context/LexicalContext.d.ts +13 -0
- package/lib/examples/context/LexicalContext.js +22 -0
- package/lib/examples/context/LexicalContext.js.map +1 -0
- package/lib/examples/plugins/ToolbarPlugin.d.ts +2 -0
- package/lib/examples/plugins/ToolbarPlugin.js +448 -0
- package/lib/examples/plugins/ToolbarPlugin.js.map +1 -0
- package/lib/examples/plugins/TreeViewPlugin.d.ts +2 -0
- package/lib/examples/plugins/TreeViewPlugin.js +13 -0
- package/lib/examples/plugins/TreeViewPlugin.js.map +1 -0
- package/lib/examples/themes/Theme.d.ts +10 -0
- package/lib/examples/themes/Theme.js +94 -0
- package/lib/examples/themes/Theme.js.map +1 -0
- package/lib/hooks/useModal.d.ts +2 -0
- package/lib/hooks/useModal.js +39 -0
- package/lib/hooks/useModal.js.map +1 -0
- package/lib/images/yellow-flower-small.jpg +0 -0
- package/lib/index.d.ts +40 -0
- package/lib/index.js +55 -0
- package/lib/index.js.map +1 -0
- package/lib/jupyter/index.d.ts +0 -0
- package/lib/jupyter/index.js +6 -0
- package/lib/jupyter/index.js.map +1 -0
- package/lib/jupyter/lab/Example.d.ts +1 -0
- package/lib/jupyter/lab/Example.js +11 -0
- package/lib/jupyter/lab/Example.js.map +1 -0
- package/lib/jupyter/lab/__tests__/datalayer.spec.d.ts +0 -0
- package/lib/jupyter/lab/__tests__/datalayer.spec.js +11 -0
- package/lib/jupyter/lab/__tests__/datalayer.spec.js.map +1 -0
- package/lib/jupyter/lab/component/MockComponent.d.ts +2 -0
- package/lib/jupyter/lab/component/MockComponent.js +17 -0
- package/lib/jupyter/lab/component/MockComponent.js.map +1 -0
- package/lib/jupyter/lab/component/MockTab1.d.ts +2 -0
- package/lib/jupyter/lab/component/MockTab1.js +14 -0
- package/lib/jupyter/lab/component/MockTab1.js.map +1 -0
- package/lib/jupyter/lab/handler.d.ts +8 -0
- package/lib/jupyter/lab/handler.js +41 -0
- package/lib/jupyter/lab/handler.js.map +1 -0
- package/lib/jupyter/lab/index.d.ts +7 -0
- package/lib/jupyter/lab/index.js +73 -0
- package/lib/jupyter/lab/index.js.map +1 -0
- package/lib/jupyter/lab/widget.d.ts +6 -0
- package/lib/jupyter/lab/widget.js +18 -0
- package/lib/jupyter/lab/widget.js.map +1 -0
- package/lib/nodes/EquationNode.d.ts +31 -0
- package/lib/nodes/EquationNode.js +118 -0
- package/lib/nodes/EquationNode.js.map +1 -0
- package/lib/nodes/ImageNode.css +50 -0
- package/lib/nodes/ImageNode.d.ts +58 -0
- package/lib/nodes/ImageNode.js +275 -0
- package/lib/nodes/ImageNode.js.map +1 -0
- package/lib/nodes/JupyterCodeHighlightNode.d.ts +49 -0
- package/lib/nodes/JupyterCodeHighlightNode.js +165 -0
- package/lib/nodes/JupyterCodeHighlightNode.js.map +1 -0
- package/lib/nodes/JupyterCodeHighlighter.d.ts +28 -0
- package/lib/nodes/JupyterCodeHighlighter.js +522 -0
- package/lib/nodes/JupyterCodeHighlighter.js.map +1 -0
- package/lib/nodes/JupyterCodeNode.d.ts +50 -0
- package/lib/nodes/JupyterCodeNode.js +277 -0
- package/lib/nodes/JupyterCodeNode.js.map +1 -0
- package/lib/nodes/JupyterOutputNode.d.ts +58 -0
- package/lib/nodes/JupyterOutputNode.js +144 -0
- package/lib/nodes/JupyterOutputNode.js.map +1 -0
- package/lib/nodes/YouTubeNode.d.ts +28 -0
- package/lib/nodes/YouTubeNode.js +56 -0
- package/lib/nodes/YouTubeNode.js.map +1 -0
- package/lib/plugins/AutoEmbedPlugin.d.ts +17 -0
- package/lib/plugins/AutoEmbedPlugin.js +110 -0
- package/lib/plugins/AutoEmbedPlugin.js.map +1 -0
- package/lib/plugins/AutoLinkPlugin.d.ts +2 -0
- package/lib/plugins/AutoLinkPlugin.js +34 -0
- package/lib/plugins/AutoLinkPlugin.js.map +1 -0
- package/lib/plugins/CodeActionMenuPlugin.css +52 -0
- package/lib/plugins/CodeActionMenuPlugin.d.ts +12 -0
- package/lib/plugins/CodeActionMenuPlugin.js +108 -0
- package/lib/plugins/CodeActionMenuPlugin.js.map +1 -0
- package/lib/plugins/ComponentPickerMenuPlugin.d.ts +2 -0
- package/lib/plugins/ComponentPickerMenuPlugin.js +244 -0
- package/lib/plugins/ComponentPickerMenuPlugin.js.map +1 -0
- package/lib/plugins/EquationsPlugin.d.ts +21 -0
- package/lib/plugins/EquationsPlugin.js +48 -0
- package/lib/plugins/EquationsPlugin.js.map +1 -0
- package/lib/plugins/HorizontalRulePlugin.d.ts +2 -0
- package/lib/plugins/HorizontalRulePlugin.js +40 -0
- package/lib/plugins/HorizontalRulePlugin.js.map +1 -0
- package/lib/plugins/ImagesPlugin.d.ts +25 -0
- package/lib/plugins/ImagesPlugin.js +212 -0
- package/lib/plugins/ImagesPlugin.js.map +1 -0
- package/lib/plugins/JupyterPlugin.css +13 -0
- package/lib/plugins/JupyterPlugin.d.ts +16 -0
- package/lib/plugins/JupyterPlugin.js +169 -0
- package/lib/plugins/JupyterPlugin.js.map +1 -0
- package/lib/plugins/ListMaxIndentLevelPlugin.d.ts +4 -0
- package/lib/plugins/ListMaxIndentLevelPlugin.js +49 -0
- package/lib/plugins/ListMaxIndentLevelPlugin.js.map +1 -0
- package/lib/plugins/MardownPlugin.d.ts +2 -0
- package/lib/plugins/MardownPlugin.js +20 -0
- package/lib/plugins/MardownPlugin.js.map +1 -0
- package/lib/plugins/NbformatContentPlugin.d.ts +6 -0
- package/lib/plugins/NbformatContentPlugin.js +20 -0
- package/lib/plugins/NbformatContentPlugin.js.map +1 -0
- package/lib/plugins/TableOfContentsPlugin.css +87 -0
- package/lib/plugins/TableOfContentsPlugin.d.ts +3 -0
- package/lib/plugins/TableOfContentsPlugin.js +115 -0
- package/lib/plugins/TableOfContentsPlugin.js.map +1 -0
- package/lib/plugins/YouTubePlugin.d.ts +4 -0
- package/lib/plugins/YouTubePlugin.js +34 -0
- package/lib/plugins/YouTubePlugin.js.map +1 -0
- package/lib/ui/Button.css +40 -0
- package/lib/ui/Button.d.ts +18 -0
- package/lib/ui/Button.js +8 -0
- package/lib/ui/Button.js.map +1 -0
- package/lib/ui/ContentEditable.css +24 -0
- package/lib/ui/ContentEditable.d.ts +5 -0
- package/lib/ui/ContentEditable.js +20 -0
- package/lib/ui/ContentEditable.js.map +1 -0
- package/lib/ui/CopyButton.d.ts +7 -0
- package/lib/ui/CopyButton.js +47 -0
- package/lib/ui/CopyButton.js.map +1 -0
- package/lib/ui/Dialog.css +23 -0
- package/lib/ui/Dialog.d.ts +16 -0
- package/lib/ui/Dialog.js +21 -0
- package/lib/ui/Dialog.js.map +1 -0
- package/lib/ui/DropDown.d.ts +23 -0
- package/lib/ui/DropDown.js +119 -0
- package/lib/ui/DropDown.js.map +1 -0
- package/lib/ui/EquationEditor.css +42 -0
- package/lib/ui/EquationEditor.d.ts +18 -0
- package/lib/ui/EquationEditor.js +31 -0
- package/lib/ui/EquationEditor.js.map +1 -0
- package/lib/ui/FileInput.d.ts +15 -0
- package/lib/ui/FileInput.js +18 -0
- package/lib/ui/FileInput.js.map +1 -0
- package/lib/ui/ImageResizer.d.ts +16 -0
- package/lib/ui/ImageResizer.js +166 -0
- package/lib/ui/ImageResizer.js.map +1 -0
- package/lib/ui/Input.css +37 -0
- package/lib/ui/KatexEquationAlterer.css +45 -0
- package/lib/ui/KatexEquationAlterer.d.ts +7 -0
- package/lib/ui/KatexEquationAlterer.js +33 -0
- package/lib/ui/KatexEquationAlterer.js.map +1 -0
- package/lib/ui/KatexRenderer.d.ts +6 -0
- package/lib/ui/KatexRenderer.js +36 -0
- package/lib/ui/KatexRenderer.js.map +1 -0
- package/lib/ui/Modal.css +67 -0
- package/lib/ui/Modal.d.ts +15 -0
- package/lib/ui/Modal.js +58 -0
- package/lib/ui/Modal.js.map +1 -0
- package/lib/ui/Placeholder.css +29 -0
- package/lib/ui/Placeholder.d.ts +14 -0
- package/lib/ui/Placeholder.js +7 -0
- package/lib/ui/Placeholder.js.map +1 -0
- package/lib/ui/PrettierButton.css +20 -0
- package/lib/ui/PrettierButton.tsx_tmp +133 -0
- package/lib/ui/TextInput.d.ts +16 -0
- package/lib/ui/TextInput.js +20 -0
- package/lib/ui/TextInput.js.map +1 -0
- package/lib/utils/canUseDOM.d.ts +8 -0
- package/lib/utils/canUseDOM.js +16 -0
- package/lib/utils/canUseDOM.js.map +1 -0
- package/lib/utils/debouncer.d.ts +1 -0
- package/lib/utils/debouncer.js +24 -0
- package/lib/utils/debouncer.js.map +1 -0
- package/lib/utils/invariant.d.ts +8 -0
- package/lib/utils/invariant.js +23 -0
- package/lib/utils/invariant.js.map +1 -0
- package/lib/utils/join.d.ts +8 -0
- package/lib/utils/join.js +16 -0
- package/lib/utils/join.js.map +1 -0
- package/lib/utils/selection.d.ts +8 -0
- package/lib/utils/selection.js +14 -0
- package/lib/utils/selection.js.map +1 -0
- package/lib/utils/useLayoutEffect.d.ts +10 -0
- package/lib/utils/useLayoutEffect.js +19 -0
- package/lib/utils/useLayoutEffect.js.map +1 -0
- package/package.json +166 -0
- package/style/base.css +16 -0
- package/style/index.css +7 -0
- package/style/index.js +7 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2021-2023 Datalayer, Inc.
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
9
|
+
*
|
|
10
|
+
* This source code is licensed under the MIT license found in the
|
|
11
|
+
* LICENSE file in the root directory of this source tree.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
import 'katex/dist/katex.css';
|
|
15
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
16
|
+
import { $wrapNodeInElement } from '@lexical/utils';
|
|
17
|
+
import { $createParagraphNode, $insertNodes, $isRootOrShadowRoot, COMMAND_PRIORITY_EDITOR, createCommand, } from 'lexical';
|
|
18
|
+
import { useCallback, useEffect } from 'react';
|
|
19
|
+
import { $createEquationNode, EquationNode } from '../nodes/EquationNode';
|
|
20
|
+
import KatexEquationAlterer from '../ui/KatexEquationAlterer';
|
|
21
|
+
export const INSERT_EQUATION_COMMAND = createCommand('INSERT_EQUATION_COMMAND');
|
|
22
|
+
export function InsertEquationDialog({ activeEditor, onClose, }) {
|
|
23
|
+
const onEquationConfirm = useCallback((equation, inline) => {
|
|
24
|
+
activeEditor.dispatchCommand(INSERT_EQUATION_COMMAND, { equation, inline });
|
|
25
|
+
onClose();
|
|
26
|
+
}, [activeEditor, onClose]);
|
|
27
|
+
return _jsx(KatexEquationAlterer, { onConfirm: onEquationConfirm });
|
|
28
|
+
}
|
|
29
|
+
export const EquationsPlugin = () => {
|
|
30
|
+
const [editor] = useLexicalComposerContext();
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (!editor.hasNodes([EquationNode])) {
|
|
33
|
+
throw new Error('EquationsPlugins: EquationsNode not registered on editor');
|
|
34
|
+
}
|
|
35
|
+
return editor.registerCommand(INSERT_EQUATION_COMMAND, (payload) => {
|
|
36
|
+
const { equation, inline } = payload;
|
|
37
|
+
const equationNode = $createEquationNode(equation, inline);
|
|
38
|
+
$insertNodes([equationNode]);
|
|
39
|
+
if ($isRootOrShadowRoot(equationNode.getParentOrThrow())) {
|
|
40
|
+
$wrapNodeInElement(equationNode, $createParagraphNode).selectEnd();
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}, COMMAND_PRIORITY_EDITOR);
|
|
44
|
+
}, [editor]);
|
|
45
|
+
return null;
|
|
46
|
+
};
|
|
47
|
+
export default EquationsPlugin;
|
|
48
|
+
//# sourceMappingURL=EquationsPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EquationsPlugin.js","sourceRoot":"","sources":["../../src/plugins/EquationsPlugin.tsx"],"names":[],"mappings":";AAAA;;;;GAIG;AAEH;;;;;;GAMG;AAEH,OAAO,sBAAsB,CAAC;AAE9B,OAAO,EAAC,yBAAyB,EAAC,MAAM,uCAAuC,CAAC;AAChF,OAAO,EAAC,kBAAkB,EAAC,MAAM,gBAAgB,CAAC;AAClD,OAAO,EACL,oBAAoB,EACpB,YAAY,EACZ,mBAAmB,EACnB,uBAAuB,EACvB,aAAa,GAGd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAC,WAAW,EAAE,SAAS,EAAC,MAAM,OAAO,CAAC;AAE7C,OAAO,EAAC,mBAAmB,EAAE,YAAY,EAAC,MAAM,uBAAuB,CAAC;AACxE,OAAO,oBAAoB,MAAM,4BAA4B,CAAC;AAO9D,MAAM,CAAC,MAAM,uBAAuB,GAClC,aAAa,CAAC,yBAAyB,CAAC,CAAC;AAE3C,MAAM,UAAU,oBAAoB,CAAC,EACnC,YAAY,EACZ,OAAO,GAIR;IACC,MAAM,iBAAiB,GAAG,WAAW,CACnC,CAAC,QAAgB,EAAE,MAAe,EAAE,EAAE;QACpC,YAAY,CAAC,eAAe,CAAC,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forms, Inc. and affiliates.
|
|
8
|
+
*
|
|
9
|
+
* This source code is licensed under the MIT license found in the
|
|
10
|
+
* LICENSE file in the root directory of this source tree.
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
14
|
+
import { $createHorizontalRuleNode, INSERT_HORIZONTAL_RULE_COMMAND, } from '@lexical/react/LexicalHorizontalRuleNode';
|
|
15
|
+
import { $getSelection, $isRangeSelection, COMMAND_PRIORITY_EDITOR, } from 'lexical';
|
|
16
|
+
import { useEffect } from 'react';
|
|
17
|
+
export const HorizontalRulePlugin = () => {
|
|
18
|
+
const [editor] = useLexicalComposerContext();
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
return editor.registerCommand(INSERT_HORIZONTAL_RULE_COMMAND, (type) => {
|
|
21
|
+
const selection = $getSelection();
|
|
22
|
+
if (!$isRangeSelection(selection)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const focusNode = selection.focus.getNode();
|
|
26
|
+
if (focusNode !== null) {
|
|
27
|
+
const horizontalRuleNode = $createHorizontalRuleNode();
|
|
28
|
+
selection.insertParagraph();
|
|
29
|
+
selection.focus
|
|
30
|
+
.getNode()
|
|
31
|
+
.getTopLevelElementOrThrow()
|
|
32
|
+
.insertBefore(horizontalRuleNode);
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}, COMMAND_PRIORITY_EDITOR);
|
|
36
|
+
}, [editor]);
|
|
37
|
+
return null;
|
|
38
|
+
};
|
|
39
|
+
export default HorizontalRulePlugin;
|
|
40
|
+
//# sourceMappingURL=HorizontalRulePlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HorizontalRulePlugin.js","sourceRoot":"","sources":["../../src/plugins/HorizontalRulePlugin.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;GAMG;AAEH,OAAO,EAAC,yBAAyB,EAAC,MAAM,uCAAuC,CAAC;AAChF,OAAO,EACL,yBAAyB,EACzB,8BAA8B,GAC/B,MAAM,0CAA0C,CAAC;AAClD,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAC,SAAS,EAAC,MAAM,OAAO,CAAC;AAEhC,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAS,EAAE;IAC7C,MAAM,CAAC,MAAM,CAAC,GAAG,yBAAyB,EAAE,CAAC;IAE7C,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,MAAM,CAAC,eAAe,CAC3B,8BAA8B,EAC9B,CAAC,IAAI,EAAE,EAAE;YACP,MAAM,SAAS,GAAG,aAAa,EAAE,CAAC;YAClC,IAAI,CAAC,iBAAiB,CAAC,SAAS,CAAC,EAAE;gBACjC,OAAO,KAAK,CAAC;aACd;YACD,MAAM,SAAS,GAAG,SAAS,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;YAC5C,IAAI,SAAS,KAAK,IAAI,EAAE;gBACtB,MAAM,kBAAkB,GAAG,yBAAyB,EAAE,CAAC;gBACvD,SAAS,CAAC,eAAe,EAAE,CAAC;gBAC5B,SAAS,CAAC,KAAK;qBACZ,OAAO,EAAE;qBACT,yBAAyB,EAAE;qBAC3B,YAAY,CAAC,kBAAkB,CAAC,CAAC;aACrC;YACD,OAAO,IAAI,CAAC;QACd,CAAC,EACD,uBAAuB,CACxB,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IACb,OAAO,IAAI,CAAC;AACd,CAAC,CAAA;AAED,eAAe,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { LexicalCommand, LexicalEditor } from 'lexical';
|
|
3
|
+
import { ImagePayload } from '../nodes/ImageNode';
|
|
4
|
+
export type InsertImagePayload = Readonly<ImagePayload>;
|
|
5
|
+
export declare const INSERT_IMAGE_COMMAND: LexicalCommand<InsertImagePayload>;
|
|
6
|
+
export declare function InsertImageUriDialogBody({ onClick, }: {
|
|
7
|
+
onClick: (payload: InsertImagePayload) => void;
|
|
8
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
export declare function InsertImageUploadedDialogBody({ onClick, }: {
|
|
10
|
+
onClick: (payload: InsertImagePayload) => void;
|
|
11
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
12
|
+
export declare function InsertImageDialog({ activeEditor, onClose, }: {
|
|
13
|
+
activeEditor: LexicalEditor;
|
|
14
|
+
onClose: () => void;
|
|
15
|
+
}): JSX.Element;
|
|
16
|
+
export declare const ImagesPlugin: ({ captionsEnabled, }: {
|
|
17
|
+
captionsEnabled?: boolean | undefined;
|
|
18
|
+
}) => JSX.Element | null;
|
|
19
|
+
declare global {
|
|
20
|
+
interface DragEvent {
|
|
21
|
+
rangeOffset?: number;
|
|
22
|
+
rangeParent?: Node;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export default ImagesPlugin;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2021-2023 Datalayer, Inc.
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
9
|
+
*
|
|
10
|
+
* This source code is licensed under the MIT license found in the
|
|
11
|
+
* LICENSE file in the root directory of this source tree.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
15
|
+
import { $wrapNodeInElement, mergeRegister } from '@lexical/utils';
|
|
16
|
+
import { $createParagraphNode, $createRangeSelection, $getSelection, $insertNodes, $isNodeSelection, $isRootOrShadowRoot, $setSelection, COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_HIGH, COMMAND_PRIORITY_LOW, createCommand, DRAGOVER_COMMAND, DRAGSTART_COMMAND, DROP_COMMAND, } from 'lexical';
|
|
17
|
+
import { useEffect, useRef, useState } from 'react';
|
|
18
|
+
import { CAN_USE_DOM } from './../utils/canUseDOM';
|
|
19
|
+
import yellowFlowerImage from '../images/yellow-flower-small.jpg';
|
|
20
|
+
import { $createImageNode, $isImageNode, ImageNode, } from '../nodes/ImageNode';
|
|
21
|
+
import Button from '../ui/Button';
|
|
22
|
+
import { DialogActions, DialogButtonsList } from '../ui/Dialog';
|
|
23
|
+
import FileInput from '../ui/FileInput';
|
|
24
|
+
import TextInput from '../ui/TextInput';
|
|
25
|
+
const getDOMSelection = (targetWindow) => CAN_USE_DOM ? (targetWindow || window).getSelection() : null;
|
|
26
|
+
export const INSERT_IMAGE_COMMAND = createCommand('INSERT_IMAGE_COMMAND');
|
|
27
|
+
export function InsertImageUriDialogBody({ onClick, }) {
|
|
28
|
+
const [src, setSrc] = useState('');
|
|
29
|
+
const [altText, setAltText] = useState('');
|
|
30
|
+
const isDisabled = src === '';
|
|
31
|
+
return (_jsxs(_Fragment, { children: [_jsx(TextInput, { label: "Image URL", placeholder: "i.e. https://source.unsplash.com/random", onChange: setSrc, value: src, "data-test-id": "image-modal-url-input" }), _jsx(TextInput, { label: "Alt Text", placeholder: "Random unsplash image", onChange: setAltText, value: altText, "data-test-id": "image-modal-alt-text-input" }), _jsx(DialogActions, { children: _jsx(Button, { "data-test-id": "image-modal-confirm-btn", disabled: isDisabled, onClick: () => onClick({ altText, src }), children: "Confirm" }) })] }));
|
|
32
|
+
}
|
|
33
|
+
export function InsertImageUploadedDialogBody({ onClick, }) {
|
|
34
|
+
const [src, setSrc] = useState('');
|
|
35
|
+
const [altText, setAltText] = useState('');
|
|
36
|
+
const isDisabled = src === '';
|
|
37
|
+
const loadImage = (files) => {
|
|
38
|
+
const reader = new FileReader();
|
|
39
|
+
reader.onload = function () {
|
|
40
|
+
if (typeof reader.result === 'string') {
|
|
41
|
+
setSrc(reader.result);
|
|
42
|
+
}
|
|
43
|
+
return '';
|
|
44
|
+
};
|
|
45
|
+
if (files !== null) {
|
|
46
|
+
reader.readAsDataURL(files[0]);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
return (_jsxs(_Fragment, { children: [_jsx(FileInput, { label: "Image Upload", onChange: loadImage, accept: "image/*", "data-test-id": "image-modal-file-upload" }), _jsx(TextInput, { label: "Alt Text", placeholder: "Descriptive alternative text", onChange: setAltText, value: altText, "data-test-id": "image-modal-alt-text-input" }), _jsx(DialogActions, { children: _jsx(Button, { "data-test-id": "image-modal-file-upload-btn", disabled: isDisabled, onClick: () => onClick({ altText, src }), children: "Confirm" }) })] }));
|
|
50
|
+
}
|
|
51
|
+
export function InsertImageDialog({ activeEditor, onClose, }) {
|
|
52
|
+
const [mode, setMode] = useState(null);
|
|
53
|
+
const hasModifier = useRef(false);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
hasModifier.current = false;
|
|
56
|
+
const handler = (e) => {
|
|
57
|
+
hasModifier.current = e.altKey;
|
|
58
|
+
};
|
|
59
|
+
document.addEventListener('keydown', handler);
|
|
60
|
+
return () => {
|
|
61
|
+
document.removeEventListener('keydown', handler);
|
|
62
|
+
};
|
|
63
|
+
}, [activeEditor]);
|
|
64
|
+
const onClick = (payload) => {
|
|
65
|
+
activeEditor.dispatchCommand(INSERT_IMAGE_COMMAND, payload);
|
|
66
|
+
onClose();
|
|
67
|
+
};
|
|
68
|
+
return (_jsxs(_Fragment, { children: [!mode && (_jsxs(DialogButtonsList, { children: [_jsx(Button, { "data-test-id": "image-modal-option-sample", onClick: () => onClick(hasModifier.current
|
|
69
|
+
? {
|
|
70
|
+
altText: 'Daylight fir trees forest glacier green high ice landscape',
|
|
71
|
+
src: yellowFlowerImage,
|
|
72
|
+
}
|
|
73
|
+
: {
|
|
74
|
+
altText: 'Yellow flower in tilt shift lens',
|
|
75
|
+
src: yellowFlowerImage,
|
|
76
|
+
}), children: "Sample" }), _jsx(Button, { "data-test-id": "image-modal-option-url", onClick: () => setMode('url'), children: "URL" }), _jsx(Button, { "data-test-id": "image-modal-option-file", onClick: () => setMode('file'), children: "File" })] })), mode === 'url' && _jsx(InsertImageUriDialogBody, { onClick: onClick }), mode === 'file' && _jsx(InsertImageUploadedDialogBody, { onClick: onClick })] }));
|
|
77
|
+
}
|
|
78
|
+
export const ImagesPlugin = ({ captionsEnabled, }) => {
|
|
79
|
+
const [editor] = useLexicalComposerContext();
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
if (!editor.hasNodes([ImageNode])) {
|
|
82
|
+
throw new Error('ImagesPlugin: ImageNode not registered on editor');
|
|
83
|
+
}
|
|
84
|
+
return mergeRegister(editor.registerCommand(INSERT_IMAGE_COMMAND, (payload) => {
|
|
85
|
+
const imageNode = $createImageNode(payload);
|
|
86
|
+
$insertNodes([imageNode]);
|
|
87
|
+
if ($isRootOrShadowRoot(imageNode.getParentOrThrow())) {
|
|
88
|
+
$wrapNodeInElement(imageNode, $createParagraphNode).selectEnd();
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}, COMMAND_PRIORITY_EDITOR), editor.registerCommand(DRAGSTART_COMMAND, (event) => {
|
|
92
|
+
return onDragStart(event);
|
|
93
|
+
}, COMMAND_PRIORITY_HIGH), editor.registerCommand(DRAGOVER_COMMAND, (event) => {
|
|
94
|
+
return onDragover(event);
|
|
95
|
+
}, COMMAND_PRIORITY_LOW), editor.registerCommand(DROP_COMMAND, (event) => {
|
|
96
|
+
return onDrop(event, editor);
|
|
97
|
+
}, COMMAND_PRIORITY_HIGH));
|
|
98
|
+
}, [captionsEnabled, editor]);
|
|
99
|
+
return null;
|
|
100
|
+
};
|
|
101
|
+
const TRANSPARENT_IMAGE = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
|
|
102
|
+
const img = document.createElement('img');
|
|
103
|
+
img.src = TRANSPARENT_IMAGE;
|
|
104
|
+
function onDragStart(event) {
|
|
105
|
+
const node = getImageNodeInSelection();
|
|
106
|
+
if (!node) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
const dataTransfer = event.dataTransfer;
|
|
110
|
+
if (!dataTransfer) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
dataTransfer.setData('text/plain', '_');
|
|
114
|
+
dataTransfer.setDragImage(img, 0, 0);
|
|
115
|
+
dataTransfer.setData('application/x-lexical-drag', JSON.stringify({
|
|
116
|
+
data: {
|
|
117
|
+
altText: node.__altText,
|
|
118
|
+
caption: node.__caption,
|
|
119
|
+
height: node.__height,
|
|
120
|
+
key: node.getKey(),
|
|
121
|
+
maxWidth: node.__maxWidth,
|
|
122
|
+
showCaption: node.__showCaption,
|
|
123
|
+
src: node.__src,
|
|
124
|
+
width: node.__width,
|
|
125
|
+
},
|
|
126
|
+
type: 'image',
|
|
127
|
+
}));
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
function onDragover(event) {
|
|
131
|
+
const node = getImageNodeInSelection();
|
|
132
|
+
if (!node) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
if (!canDropImage(event)) {
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
function onDrop(event, editor) {
|
|
141
|
+
const node = getImageNodeInSelection();
|
|
142
|
+
if (!node) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
const data = getDragImageData(event);
|
|
146
|
+
if (!data) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
event.preventDefault();
|
|
150
|
+
if (canDropImage(event)) {
|
|
151
|
+
const range = getDragSelection(event);
|
|
152
|
+
node.remove();
|
|
153
|
+
const rangeSelection = $createRangeSelection();
|
|
154
|
+
if (range !== null && range !== undefined) {
|
|
155
|
+
rangeSelection.applyDOMRange(range);
|
|
156
|
+
}
|
|
157
|
+
$setSelection(rangeSelection);
|
|
158
|
+
editor.dispatchCommand(INSERT_IMAGE_COMMAND, data);
|
|
159
|
+
}
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
function getImageNodeInSelection() {
|
|
163
|
+
const selection = $getSelection();
|
|
164
|
+
if (!$isNodeSelection(selection)) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
const nodes = selection.getNodes();
|
|
168
|
+
const node = nodes[0];
|
|
169
|
+
return $isImageNode(node) ? node : null;
|
|
170
|
+
}
|
|
171
|
+
function getDragImageData(event) {
|
|
172
|
+
const dragData = event.dataTransfer?.getData('application/x-lexical-drag');
|
|
173
|
+
if (!dragData) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
const { type, data } = JSON.parse(dragData);
|
|
177
|
+
if (type !== 'image') {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
return data;
|
|
181
|
+
}
|
|
182
|
+
function canDropImage(event) {
|
|
183
|
+
const target = event.target;
|
|
184
|
+
return !!(target &&
|
|
185
|
+
target instanceof HTMLElement &&
|
|
186
|
+
!target.closest('code, span.editor-image') &&
|
|
187
|
+
target.parentElement &&
|
|
188
|
+
target.parentElement.closest('div.ContentEditable__root'));
|
|
189
|
+
}
|
|
190
|
+
function getDragSelection(event) {
|
|
191
|
+
let range;
|
|
192
|
+
const target = event.target;
|
|
193
|
+
const targetWindow = target == null
|
|
194
|
+
? null
|
|
195
|
+
: target.nodeType === 9
|
|
196
|
+
? target.defaultView
|
|
197
|
+
: target.ownerDocument.defaultView;
|
|
198
|
+
const domSelection = getDOMSelection(targetWindow);
|
|
199
|
+
if (document.caretRangeFromPoint) {
|
|
200
|
+
range = document.caretRangeFromPoint(event.clientX, event.clientY);
|
|
201
|
+
}
|
|
202
|
+
else if (event.rangeParent && domSelection !== null) {
|
|
203
|
+
domSelection.collapse(event.rangeParent, event.rangeOffset || 0);
|
|
204
|
+
range = domSelection.getRangeAt(0);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
throw Error(`Cannot get the selection when dragging`);
|
|
208
|
+
}
|
|
209
|
+
return range;
|
|
210
|
+
}
|
|
211
|
+
export default ImagesPlugin;
|
|
212
|
+
//# sourceMappingURL=ImagesPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ImagesPlugin.js","sourceRoot":"","sources":["../../src/plugins/ImagesPlugin.tsx"],"names":[],"mappings":";AAAA;;;;GAIG;AAEH;;;;;;GAMG;AACH,OAAO,EAAC,yBAAyB,EAAC,MAAM,uCAAuC,CAAC;AAChF,OAAO,EAAC,kBAAkB,EAAE,aAAa,EAAC,MAAM,gBAAgB,CAAC;AACjE,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,aAAa,EACb,YAAY,EACZ,gBAAgB,EAChB,mBAAmB,EACnB,aAAa,EACb,uBAAuB,EACvB,qBAAqB,EACrB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,YAAY,GAGb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAC;AAClD,OAAO,EAAC,WAAW,EAAC,MAAM,sBAAsB,CAAC;AAEjD,OAAO,iBAAiB,MAAM,mCAAmC,CAAC;AAClE,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,SAAS,GAEV,MAAM,oBAAoB,CAAC;AAC5B,OAAO,MAAM,MAAM,cAAc,CAAC;AAClC,OAAO,EAAC,aAAa,EAAE,iBAAiB,EAAC,MAAM,cAAc,CAAC;AAC9D,OAAO,SAAS,MAAM,iBAAiB,CAAC;AACxC,OAAO,SAAS,MAAM,iBAAiB,CAAC;AAIxC,MAAM,eAAe,GAAG,CAAC,YAA2B,EAAoB,EAAE,CACxE,WAAW,CAAC,CAAC,CAAC,CAAC,YAAY,IAAI,MAAM,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC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
|
+
export declare const CODE_UUID_TO_OUTPUT_KEY: Map<string, string | undefined>;
|
|
4
|
+
export declare const CODE_UUID_TO_CODE_KEY: Map<string, string | undefined>;
|
|
5
|
+
export declare const CODE_UUID_TO_OUTPUT_UUID: Map<string, string | undefined>;
|
|
6
|
+
export declare const OUTPUT_UUID_TO_CODE_UUID: Map<string, string | undefined>;
|
|
7
|
+
export declare const OUTPUT_UUID_TO_OUTPUT_KEY: Map<string, string | undefined>;
|
|
8
|
+
export declare const DEFAULT_INITIAL_OUTPUTS: IOutput[];
|
|
9
|
+
export type JupyterOutputProps = {
|
|
10
|
+
code: string;
|
|
11
|
+
outputs?: IOutput[];
|
|
12
|
+
loading?: string;
|
|
13
|
+
};
|
|
14
|
+
export declare const INSERT_JUPYTER_CELL_COMMAND: import("lexical").LexicalCommand<JupyterOutputProps>;
|
|
15
|
+
export declare const JupyterPlugin: () => null;
|
|
16
|
+
export default JupyterPlugin;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021-2023 Datalayer, Inc.
|
|
3
|
+
*
|
|
4
|
+
* MIT License
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect } from "react";
|
|
7
|
+
import { $getSelection, $isRangeSelection, createCommand, COMMAND_PRIORITY_EDITOR, INSERT_LINE_BREAK_COMMAND, COMMAND_PRIORITY_HIGH, $isLineBreakNode, $isElementNode, $insertNodes, $createParagraphNode } from "lexical";
|
|
8
|
+
import { $getNodeByKey, $createNodeSelection, $setSelection } from "lexical";
|
|
9
|
+
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
10
|
+
import { $setBlocksType } from "@lexical/selection";
|
|
11
|
+
import { $insertNodeToNearestRoot } from '@lexical/utils';
|
|
12
|
+
import { OutputAdapter } from '@datalayer/jupyter-react';
|
|
13
|
+
import { UUID } from '@lumino/coreutils';
|
|
14
|
+
import { $createJupyterCodeNode } from "../nodes/JupyterCodeNode";
|
|
15
|
+
import { $isJupyterCodeNode } from "../nodes/JupyterCodeNode";
|
|
16
|
+
import { registerCodeHighlighting } from "../nodes/JupyterCodeHighlighter";
|
|
17
|
+
import { JupyterOutputNode, $createJupyterOutputNode } from "../nodes/JupyterOutputNode";
|
|
18
|
+
import { JupyterCodeNode } from "../nodes/JupyterCodeNode";
|
|
19
|
+
import "./JupyterPlugin.css";
|
|
20
|
+
export const CODE_UUID_TO_OUTPUT_KEY = new Map();
|
|
21
|
+
export const CODE_UUID_TO_CODE_KEY = new Map();
|
|
22
|
+
export const CODE_UUID_TO_OUTPUT_UUID = new Map();
|
|
23
|
+
export const OUTPUT_UUID_TO_CODE_UUID = new Map();
|
|
24
|
+
export const OUTPUT_UUID_TO_OUTPUT_KEY = new Map();
|
|
25
|
+
export const DEFAULT_INITIAL_OUTPUTS = [
|
|
26
|
+
{
|
|
27
|
+
"output_type": "execute_result",
|
|
28
|
+
"data": {
|
|
29
|
+
"text/html": [
|
|
30
|
+
"<p>Type code in the cell and Shift+Enter to execute.</p>"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"execution_count": 0,
|
|
34
|
+
"metadata": {},
|
|
35
|
+
}
|
|
36
|
+
];
|
|
37
|
+
export const INSERT_JUPYTER_CELL_COMMAND = createCommand();
|
|
38
|
+
export const JupyterPlugin = () => {
|
|
39
|
+
const [editor] = useLexicalComposerContext();
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
return registerCodeHighlighting(editor);
|
|
42
|
+
}, [editor]);
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (!editor.hasNodes([JupyterOutputNode])) {
|
|
45
|
+
throw new Error("JupyterPlugin: JupyterOutputNode not registered on editor");
|
|
46
|
+
}
|
|
47
|
+
}, [editor]);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
return editor.registerCommand(INSERT_LINE_BREAK_COMMAND, (event) => {
|
|
50
|
+
const selection = $getSelection();
|
|
51
|
+
const node = selection?.getNodes()[0];
|
|
52
|
+
if (node?.__parent) {
|
|
53
|
+
const parentNode = $getNodeByKey(node?.__parent);
|
|
54
|
+
if ($isJupyterCodeNode(parentNode)) {
|
|
55
|
+
const code = parentNode.getTextContent();
|
|
56
|
+
const codeNodeUuid = parentNode.getCodeNodeUuid();
|
|
57
|
+
const jupyterOutputNodeKey = CODE_UUID_TO_OUTPUT_KEY.get(codeNodeUuid);
|
|
58
|
+
if (jupyterOutputNodeKey) {
|
|
59
|
+
const jupyterOutputNode = $getNodeByKey(jupyterOutputNodeKey);
|
|
60
|
+
if (jupyterOutputNode) {
|
|
61
|
+
jupyterOutputNode.executeCode(code);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const jupyterOutputNode = $createJupyterOutputNode(code, new OutputAdapter(undefined, []), [], true, codeNodeUuid, UUID.uuid4());
|
|
66
|
+
$insertNodeToNearestRoot(jupyterOutputNode);
|
|
67
|
+
const nodeSelection = $createNodeSelection();
|
|
68
|
+
nodeSelection.add(parentNode.__key);
|
|
69
|
+
$setSelection(nodeSelection);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}, COMMAND_PRIORITY_HIGH);
|
|
75
|
+
}, [editor]);
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
return editor.registerMutationListener(JupyterCodeNode, (mutatedNodes) => {
|
|
78
|
+
for (let [nodeKey, mutation] of mutatedNodes) {
|
|
79
|
+
if (mutation === "destroyed") {
|
|
80
|
+
editor.update(() => {
|
|
81
|
+
// let codeNodeUuid: string | undefined;
|
|
82
|
+
let outputNodeUuid;
|
|
83
|
+
// TODO Do not use forEach...
|
|
84
|
+
CODE_UUID_TO_CODE_KEY.forEach((codeKey, codeUuid) => {
|
|
85
|
+
if (codeKey === nodeKey) {
|
|
86
|
+
// codeNodeUuid = codeUuid;
|
|
87
|
+
outputNodeUuid = CODE_UUID_TO_OUTPUT_UUID.get(codeUuid);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
if (outputNodeUuid) {
|
|
91
|
+
const outputNodeKey = OUTPUT_UUID_TO_OUTPUT_KEY.get(outputNodeUuid);
|
|
92
|
+
if (outputNodeKey) {
|
|
93
|
+
const outputNode = $getNodeByKey(outputNodeKey);
|
|
94
|
+
if (outputNode) {
|
|
95
|
+
outputNode.markDirty();
|
|
96
|
+
outputNode.removeForce();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// CODE_UUID_TO_OUTPUT_UUID.delete(codeNodeUuid);
|
|
100
|
+
// CODE_UUID_TO_OUTPUT_KEY.delete(codeNodeUuid);
|
|
101
|
+
// CODE_UUID_TO_CODE_KEY.delete(codeNodeUuid);
|
|
102
|
+
}
|
|
103
|
+
if (outputNodeUuid) {
|
|
104
|
+
// OUTPUT_UUID_TO_CODE_UUID.delete(outputNodeUuid);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}, [editor]);
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
return editor.registerCommand(INSERT_JUPYTER_CELL_COMMAND, (props) => {
|
|
113
|
+
const { code, outputs, loading } = props;
|
|
114
|
+
const selection = $getSelection();
|
|
115
|
+
if ($isRangeSelection(selection)) {
|
|
116
|
+
selection.removeText();
|
|
117
|
+
const jupyterCodeNode = $createJupyterCodeNode("python");
|
|
118
|
+
/*
|
|
119
|
+
if (selection.isCollapsed()) {
|
|
120
|
+
const paragraphNode = $createParagraphNode();
|
|
121
|
+
const textNode = $createTextNode(code || "");
|
|
122
|
+
paragraphNode.append(textNode);
|
|
123
|
+
selection.insertNodes([paragraphNode]);
|
|
124
|
+
$wrapNodes(selection, () => codeNode);
|
|
125
|
+
} else {
|
|
126
|
+
const textContent = selection.getTextContent();
|
|
127
|
+
selection.insertNodes([codeNode]);
|
|
128
|
+
selection.insertRawText(textContent);
|
|
129
|
+
}
|
|
130
|
+
*/
|
|
131
|
+
if (selection.isCollapsed()) {
|
|
132
|
+
const anchorNode = selection.anchor.getNode();
|
|
133
|
+
if (anchorNode && $isElementNode(anchorNode)) {
|
|
134
|
+
const nodes = anchorNode.getChildren();
|
|
135
|
+
nodes.map((node) => {
|
|
136
|
+
if ($isLineBreakNode(node)) {
|
|
137
|
+
node.remove();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
selection.insertRawText(code);
|
|
142
|
+
$setBlocksType(selection, () => jupyterCodeNode);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
selection.insertNodes([jupyterCodeNode]);
|
|
146
|
+
}
|
|
147
|
+
const outputAdapter = new OutputAdapter(undefined, outputs);
|
|
148
|
+
const jupyterOutputNode = $createJupyterOutputNode(code, outputAdapter, outputs || [], false, jupyterCodeNode.getCodeNodeUuid(), UUID.uuid4());
|
|
149
|
+
outputAdapter.outputArea.model.changed.connect((outputModel, args) => {
|
|
150
|
+
editor.update(() => {
|
|
151
|
+
jupyterOutputNode.setOutputs(outputModel.toJSON());
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
const tmpParagraph = $createParagraphNode();
|
|
155
|
+
$insertNodes([tmpParagraph]);
|
|
156
|
+
$insertNodeToNearestRoot(jupyterOutputNode);
|
|
157
|
+
tmpParagraph.remove();
|
|
158
|
+
if (!loading) {
|
|
159
|
+
jupyterCodeNode.selectEnd();
|
|
160
|
+
// selection.insertRawText(code);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}, COMMAND_PRIORITY_EDITOR);
|
|
165
|
+
}, [editor]);
|
|
166
|
+
return null;
|
|
167
|
+
};
|
|
168
|
+
export default JupyterPlugin;
|
|
169
|
+
//# sourceMappingURL=JupyterPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JupyterPlugin.js","sourceRoot":"","sources":["../../src/plugins/JupyterPlugin.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS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 2021-2023 Datalayer, Inc.
|
|
3
|
+
*
|
|
4
|
+
* MIT License
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect } from "react";
|
|
7
|
+
import { $getSelection, $isElementNode, $isRangeSelection, INDENT_CONTENT_COMMAND, COMMAND_PRIORITY_HIGH } from "lexical";
|
|
8
|
+
import { $getListDepth, $isListItemNode, $isListNode } from "@lexical/list";
|
|
9
|
+
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
10
|
+
function getElementNodesInSelection(selection) {
|
|
11
|
+
const nodesInSelection = selection.getNodes();
|
|
12
|
+
if (nodesInSelection.length === 0) {
|
|
13
|
+
return new Set([
|
|
14
|
+
selection.anchor.getNode().getParentOrThrow(),
|
|
15
|
+
selection.focus.getNode().getParentOrThrow()
|
|
16
|
+
]);
|
|
17
|
+
}
|
|
18
|
+
return new Set(nodesInSelection.map((n) => ($isElementNode(n) ? n : n.getParentOrThrow())));
|
|
19
|
+
}
|
|
20
|
+
function isIndentPermitted(maxDepth) {
|
|
21
|
+
const selection = $getSelection();
|
|
22
|
+
if (!$isRangeSelection(selection)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const elementNodesInSelection = getElementNodesInSelection(selection);
|
|
26
|
+
let totalDepth = 0;
|
|
27
|
+
for (const elementNode of elementNodesInSelection) {
|
|
28
|
+
if ($isListNode(elementNode)) {
|
|
29
|
+
totalDepth = Math.max($getListDepth(elementNode) + 1, totalDepth);
|
|
30
|
+
}
|
|
31
|
+
else if ($isListItemNode(elementNode)) {
|
|
32
|
+
const parent = elementNode.getParent();
|
|
33
|
+
if (!$isListNode(parent)) {
|
|
34
|
+
throw new Error("ListMaxIndentLevelPlugin: A ListItemNode must have a ListNode for a parent.");
|
|
35
|
+
}
|
|
36
|
+
totalDepth = Math.max($getListDepth(parent) + 1, totalDepth);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return totalDepth <= maxDepth;
|
|
40
|
+
}
|
|
41
|
+
export const ListMaxIndentLevelPlugin = ({ maxDepth }) => {
|
|
42
|
+
const [editor] = useLexicalComposerContext();
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
return editor.registerCommand(INDENT_CONTENT_COMMAND, () => !isIndentPermitted(maxDepth ?? 7), COMMAND_PRIORITY_HIGH);
|
|
45
|
+
}, [editor, maxDepth]);
|
|
46
|
+
return null;
|
|
47
|
+
};
|
|
48
|
+
export default ListMaxIndentLevelPlugin;
|
|
49
|
+
//# sourceMappingURL=ListMaxIndentLevelPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ListMaxIndentLevelPlugin.js","sourceRoot":"","sources":["../../src/plugins/ListMaxIndentLevelPlugin.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,qBAAqB,EAAkB,MAAM,SAAS,CAAC;AAC1I,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,EAAE,yBAAyB,EAAE,MAAM,uCAAuC,CAAC;AAElF,SAAS,0BAA0B,CAAC,SAAyB;IAC3D,MAAM,gBAAgB,GAAG,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC9C,IAAI,gBAAgB,CAAC,MAAM,KAAK,CAAC,EAAE;QACjC,OAAO,IAAI,GAAG,CAAC;YACb,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,gBAAgB,EAAE;YAC7C,SAAS,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,gBAAgB,EAAE;SAC7C,CAAC,CAAC;KACJ;IACD,OAAO,IAAI,GAAG,CACZ,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CA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