@mdxeditor/editor 0.4.1 → 0.5.0

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,84 @@
1
+ import { $getNodeByKey, $createParagraphNode } from "lexical";
2
+ import React__default from "react";
3
+ import { u as useEmitterValues, a as usePublisher } from "./index-11db5fbf.js";
4
+ function useCodeMirrorRef(nodeKey, editorType, language) {
5
+ const [activeEditor] = useEmitterValues("activeEditor");
6
+ const setActiveEditorType = usePublisher("activeEditorType");
7
+ const codeMirrorRef = React__default.useRef(null);
8
+ const atBottom = React__default.useRef(false);
9
+ const atTop = React__default.useRef(false);
10
+ const onFocusHandler = React__default.useCallback(() => {
11
+ setActiveEditorType({ type: editorType, nodeKey });
12
+ }, [nodeKey, setActiveEditorType, editorType]);
13
+ const onKeyDownHandler = React__default.useCallback(
14
+ (e) => {
15
+ var _a, _b, _c, _d;
16
+ if (e.key === "ArrowDown") {
17
+ const state = (_b = (_a = codeMirrorRef == null ? void 0 : codeMirrorRef.current) == null ? void 0 : _a.getCodemirror()) == null ? void 0 : _b.state;
18
+ if (state) {
19
+ const docLength = state.doc.length;
20
+ const selectionEnd = state.selection.ranges[0].to;
21
+ if (docLength === selectionEnd) {
22
+ if (!atBottom.current) {
23
+ atBottom.current = true;
24
+ } else {
25
+ activeEditor == null ? void 0 : activeEditor.update(() => {
26
+ var _a2, _b2;
27
+ const node = $getNodeByKey(nodeKey);
28
+ const nextSibling = node.getNextSibling();
29
+ if (nextSibling) {
30
+ (_b2 = (_a2 = codeMirrorRef == null ? void 0 : codeMirrorRef.current) == null ? void 0 : _a2.getCodemirror()) == null ? void 0 : _b2.contentDOM.blur();
31
+ node.selectNext();
32
+ } else {
33
+ node.insertAfter($createParagraphNode());
34
+ }
35
+ });
36
+ atBottom.current = false;
37
+ }
38
+ }
39
+ }
40
+ } else if (e.key === "ArrowUp") {
41
+ const state = (_d = (_c = codeMirrorRef == null ? void 0 : codeMirrorRef.current) == null ? void 0 : _c.getCodemirror()) == null ? void 0 : _d.state;
42
+ if (state) {
43
+ const selectionStart = state.selection.ranges[0].from;
44
+ if (selectionStart === 0) {
45
+ if (!atTop.current) {
46
+ atTop.current = true;
47
+ } else {
48
+ activeEditor == null ? void 0 : activeEditor.update(() => {
49
+ var _a2, _b2;
50
+ const node = $getNodeByKey(nodeKey);
51
+ const previousSibling = node.getPreviousSibling();
52
+ if (previousSibling) {
53
+ (_b2 = (_a2 = codeMirrorRef == null ? void 0 : codeMirrorRef.current) == null ? void 0 : _a2.getCodemirror()) == null ? void 0 : _b2.contentDOM.blur();
54
+ node.selectPrevious();
55
+ }
56
+ });
57
+ atTop.current = false;
58
+ }
59
+ }
60
+ }
61
+ } else if (e.key === "Enter") {
62
+ e.stopPropagation();
63
+ }
64
+ },
65
+ [activeEditor, nodeKey]
66
+ );
67
+ React__default.useEffect(() => {
68
+ const codeMirror = codeMirrorRef.current;
69
+ setTimeout(() => {
70
+ var _a, _b, _c, _d;
71
+ (_b = (_a = codeMirror == null ? void 0 : codeMirror.getCodemirror()) == null ? void 0 : _a.contentDOM) == null ? void 0 : _b.addEventListener("focus", onFocusHandler);
72
+ (_d = (_c = codeMirror == null ? void 0 : codeMirror.getCodemirror()) == null ? void 0 : _c.contentDOM) == null ? void 0 : _d.addEventListener("keydown", onKeyDownHandler);
73
+ }, 100);
74
+ return () => {
75
+ var _a, _b;
76
+ (_a = codeMirror == null ? void 0 : codeMirror.getCodemirror()) == null ? void 0 : _a.contentDOM.removeEventListener("focus", onFocusHandler);
77
+ (_b = codeMirror == null ? void 0 : codeMirror.getCodemirror()) == null ? void 0 : _b.contentDOM.removeEventListener("keydown", onKeyDownHandler);
78
+ };
79
+ }, [codeMirrorRef, onFocusHandler, onKeyDownHandler, language]);
80
+ return codeMirrorRef;
81
+ }
82
+ export {
83
+ useCodeMirrorRef as u
84
+ };
@@ -0,0 +1 @@
1
+ "use strict";const e=require("lexical"),r=require("react"),t=require("./index-731c8e58.cjs");exports.useCodeMirrorRef=function(n,o,l){const[u]=t.useEmitterValues("activeEditor"),i=t.usePublisher("activeEditorType"),s=r.useRef(null),c=r.useRef(!1),d=r.useRef(!1),v=r.useCallback((()=>{i({type:o,nodeKey:n})}),[n,i,o]),a=r.useCallback((r=>{var t,o,l,i;if("ArrowDown"===r.key){const r=null==(o=null==(t=null==s?void 0:s.current)?void 0:t.getCodemirror())?void 0:o.state;if(r){r.doc.length===r.selection.ranges[0].to&&(c.current?(null==u||u.update((()=>{var r,t;const o=e.$getNodeByKey(n);o.getNextSibling()?(null==(t=null==(r=null==s?void 0:s.current)?void 0:r.getCodemirror())||t.contentDOM.blur(),o.selectNext()):o.insertAfter(e.$createParagraphNode())})),c.current=!1):c.current=!0)}}else if("ArrowUp"===r.key){const r=null==(i=null==(l=null==s?void 0:s.current)?void 0:l.getCodemirror())?void 0:i.state;if(r){0===r.selection.ranges[0].from&&(d.current?(null==u||u.update((()=>{var r,t;const o=e.$getNodeByKey(n);o.getPreviousSibling()&&(null==(t=null==(r=null==s?void 0:s.current)?void 0:r.getCodemirror())||t.contentDOM.blur(),o.selectPrevious())})),d.current=!1):d.current=!0)}}else"Enter"===r.key&&r.stopPropagation()}),[u,n]);return r.useEffect((()=>{const e=s.current;return setTimeout((()=>{var r,t,n,o;null==(t=null==(r=null==e?void 0:e.getCodemirror())?void 0:r.contentDOM)||t.addEventListener("focus",v),null==(o=null==(n=null==e?void 0:e.getCodemirror())?void 0:n.contentDOM)||o.addEventListener("keydown",a)}),100),()=>{var r,t;null==(r=null==e?void 0:e.getCodemirror())||r.contentDOM.removeEventListener("focus",v),null==(t=null==e?void 0:e.getCodemirror())||t.contentDOM.removeEventListener("keydown",a)}}),[s,v,a,l]),s};
package/package.json CHANGED
@@ -21,7 +21,7 @@
21
21
  "main": "dist/index.cjs",
22
22
  "module": "dist/index.js",
23
23
  "types": "dist/index.d.ts",
24
- "version": "0.4.1",
24
+ "version": "0.5.0",
25
25
  "description": "React component for rich text markdown editing",
26
26
  "scripts": {
27
27
  "build": "vite build",