@haklex/rich-editor 0.0.75 → 0.0.76

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.
@@ -16,6 +16,7 @@ import { CHECK_LIST, TRANSFORMERS, QUOTE, CODE, $convertFromMarkdownString } fro
16
16
  import { $ as $createAlertQuoteEditNode } from "./AlertQuoteEditNode-CHdvQnOr.js";
17
17
  import { s as AlertQuoteNode, F as FootnoteNode, w as $createFootnoteNode, k as KaTeXInlineNode, K as KaTeXBlockNode, t as $createKaTeXInlineNode, v as $createKaTeXBlockNode, M as MentionNode, a as $createMentionNode, o as extractTextContent, S as SpoilerNode, x as $createSpoilerNode, $ as $createImageNode, O as OPEN_IMAGE_UPLOAD_DIALOG_COMMAND } from "./theme-CYsyGmCL.js";
18
18
  import { r as CodeBlockNode, n as BannerNode, D as DetailsNode, s as $createDetailsNode, F as FootnoteSectionNode, d as $isFootnoteSectionNode, $ as $createFootnoteSectionNode, e as $isGridContainerNode, R as RubyNode, c as $createRubyNode } from "./config-DcR_yktp.js";
19
+ import { $createTableNode, $createTableRowNode, $createTableCellNode, TableCellHeaderStates } from "@lexical/table";
19
20
  import { MarkdownShortcutPlugin } from "@lexical/react/LexicalMarkdownShortcutPlugin";
20
21
  import { nanoid } from "nanoid";
21
22
  import { Dialog, DialogPopup, DialogTitle, SegmentedControl, ActionButton, ActionBar } from "@haklex/rich-editor-ui";
@@ -426,7 +427,7 @@ const CODE_BLOCK_MULTILINE_TRANSFORMER = {
426
427
  regExp: /[\t ]*```$/
427
428
  },
428
429
  regExpStart: /^[\t ]*```([\w-]+)?/,
429
- replace: (rootNode, _children, startMatch, _endMatch, linesInBetween) => {
430
+ replace: (rootNode, _children, startMatch, _endMatch, linesInBetween, isImport) => {
430
431
  const lang = startMatch[1] || "";
431
432
  let code = "";
432
433
  if (linesInBetween) {
@@ -437,7 +438,11 @@ const CODE_BLOCK_MULTILINE_TRANSFORMER = {
437
438
  }
438
439
  const Klass = findCodeBlockKlass(getResolvedEditNodes());
439
440
  const node = new Klass(code, lang);
440
- rootNode.replace(node);
441
+ if (isImport || $isRootNode(rootNode)) {
442
+ rootNode.append(node);
443
+ } else {
444
+ rootNode.replace(node);
445
+ }
441
446
  const selection = $createNodeSelection();
442
447
  selection.add(node.getKey());
443
448
  $setSelection(selection);
@@ -640,6 +645,62 @@ const SPOILER_TRANSFORMER = {
640
645
  textNode.replace(spoilerNode);
641
646
  }
642
647
  };
648
+ const TABLE_ROW_REG_EXP = /^\|(.+)\|\s*$/;
649
+ const TABLE_DIVIDER_REG_EXP = /^\|(?:\s*:?-+:?\s*\|)+\s*$/;
650
+ function parseCells(row) {
651
+ const match = row.match(TABLE_ROW_REG_EXP);
652
+ if (!match) return [];
653
+ return match[1].split("|").map((c) => c.trim());
654
+ }
655
+ const TABLE_IMPORT_TRANSFORMER = {
656
+ dependencies: [],
657
+ export: () => null,
658
+ handleImportAfterStartMatch({ lines, rootNode, startLineIndex }) {
659
+ if (startLineIndex + 1 >= lines.length) return null;
660
+ const dividerLine = lines[startLineIndex + 1];
661
+ if (!TABLE_DIVIDER_REG_EXP.test(dividerLine)) return null;
662
+ const headerCells = parseCells(lines[startLineIndex]);
663
+ if (headerCells.length === 0) return null;
664
+ let endLineIndex = startLineIndex + 1;
665
+ const dataRows = [];
666
+ for (let i = startLineIndex + 2; i < lines.length; i++) {
667
+ if (!TABLE_ROW_REG_EXP.test(lines[i])) break;
668
+ dataRows.push(parseCells(lines[i]));
669
+ endLineIndex = i;
670
+ }
671
+ const tableNode = $createTableNode();
672
+ const headerRow = $createTableRowNode();
673
+ for (const cell of headerCells) {
674
+ const cellNode = $createTableCellNode(TableCellHeaderStates.ROW);
675
+ const p = $createParagraphNode();
676
+ p.append($createTextNode(cell));
677
+ cellNode.append(p);
678
+ headerRow.append(cellNode);
679
+ }
680
+ tableNode.append(headerRow);
681
+ for (const row of dataRows) {
682
+ const rowNode = $createTableRowNode();
683
+ for (let i = 0; i < headerCells.length; i++) {
684
+ const cellNode = $createTableCellNode(TableCellHeaderStates.NO_STATUS);
685
+ const p = $createParagraphNode();
686
+ p.append($createTextNode(row[i] ?? ""));
687
+ cellNode.append(p);
688
+ rowNode.append(cellNode);
689
+ }
690
+ tableNode.append(rowNode);
691
+ }
692
+ rootNode.append(tableNode);
693
+ return [true, endLineIndex];
694
+ },
695
+ regExpEnd: {
696
+ optional: true,
697
+ regExp: TABLE_ROW_REG_EXP
698
+ },
699
+ regExpStart: TABLE_ROW_REG_EXP,
700
+ replace: () => {
701
+ },
702
+ type: "multiline-element"
703
+ };
643
704
  const ALL_TRANSFORMERS = [
644
705
  // Inline transformers
645
706
  SPOILER_TRANSFORMER,
@@ -664,6 +725,7 @@ const ALL_TRANSFORMERS = [
664
725
  MERMAID_BLOCK_TRANSFORMER,
665
726
  GRID_CONTAINER_BLOCK_TRANSFORMER,
666
727
  HORIZONTAL_RULE_BLOCK_TRANSFORMER,
728
+ TABLE_IMPORT_TRANSFORMER,
667
729
  TABLE_BLOCK_TRANSFORMER,
668
730
  QUOTE_TRANSFORMER,
669
731
  ...TRANSFORMERS.filter((t) => t !== QUOTE && t !== CODE)
@@ -711,11 +773,12 @@ function detectMarkdown(text) {
711
773
  return score >= 5;
712
774
  }
713
775
  function convertAndInsert(markdown) {
776
+ let conversionError = null;
714
777
  const tempEditor = createEditor({
715
778
  namespace: "markdown-paste-temp",
716
779
  nodes: getResolvedEditNodes(),
717
780
  onError: (error) => {
718
- console.error("MarkdownPastePlugin: convertAndInsert error", error);
781
+ conversionError = error;
719
782
  }
720
783
  });
721
784
  tempEditor.update(
@@ -724,9 +787,15 @@ function convertAndInsert(markdown) {
724
787
  },
725
788
  { discrete: true }
726
789
  );
790
+ if (conversionError) {
791
+ console.error("MarkdownPastePlugin: convertAndInsert error", conversionError);
792
+ return false;
793
+ }
727
794
  const serializedChildren = tempEditor.getEditorState().toJSON().root.children;
795
+ if (!serializedChildren.length) return false;
728
796
  const nodes = serializedChildren.map((s) => $parseSerializedNode(s));
729
797
  $insertNodes(nodes);
798
+ return true;
730
799
  }
731
800
  function MarkdownPastePlugin() {
732
801
  const [editor] = useLexicalComposerContext();
@@ -757,11 +826,13 @@ function MarkdownPastePlugin() {
757
826
  const text = clipboardData.getData("text/plain");
758
827
  if (!text || !detectMarkdown(text)) return false;
759
828
  try {
760
- console.info("MarkdownPastePlugin: convertAndInsert", text);
829
+ if (!convertAndInsert(text)) {
830
+ return false;
831
+ }
761
832
  event.preventDefault();
762
- convertAndInsert(text);
763
833
  return true;
764
- } catch {
834
+ } catch (error) {
835
+ console.error("MarkdownPastePlugin: paste error", error);
765
836
  return false;
766
837
  }
767
838
  },
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { F as FootnotePlugin, O as OnChangePlugin, S as SubmitShortcutPlugin, E as EditorRefPlugin, a as AutoFocusPlugin, d as ImageUploadProvider, g as defaultImageUpload, C as CorePlugins, I as ImageUploadPlugin, L as LinkFaviconPlugin, c as BlockIdPlugin, f as blockIdState } from "./SubmitShortcutPlugin-CgXXuTDw.js";
1
+ import { F as FootnotePlugin, O as OnChangePlugin, S as SubmitShortcutPlugin, E as EditorRefPlugin, a as AutoFocusPlugin, d as ImageUploadProvider, g as defaultImageUpload, C as CorePlugins, I as ImageUploadPlugin, L as LinkFaviconPlugin, c as BlockIdPlugin, f as blockIdState } from "./SubmitShortcutPlugin-BFBAwTb1.js";
2
2
  import { L, P, u } from "./PresentDialogContext-DmwCjSHw.js";
3
3
  import { jsx, jsxs } from "react/jsx-runtime";
4
4
  import { s as setResolvedEditNodes, a as allEditNodes } from "./node-registry-B2U2_LrI.js";
@@ -1 +1 @@
1
- {"version":3,"file":"MarkdownPastePlugin.d.ts","sourceRoot":"","sources":["../../src/plugins/MarkdownPastePlugin.tsx"],"names":[],"mappings":"AA+HA,wBAAgB,mBAAmB,SAuDlC"}
1
+ {"version":3,"file":"MarkdownPastePlugin.d.ts","sourceRoot":"","sources":["../../src/plugins/MarkdownPastePlugin.tsx"],"names":[],"mappings":"AAuIA,wBAAgB,mBAAmB,SAyDlC"}
@@ -1,4 +1,4 @@
1
- import { A, a, b, B, c, C, E, F, H, I, d, L, M, e, O, S, f, g, u } from "./SubmitShortcutPlugin-CgXXuTDw.js";
1
+ import { A, a, b, B, c, C, E, F, H, I, d, L, M, e, O, S, f, g, u } from "./SubmitShortcutPlugin-BFBAwTb1.js";
2
2
  import { A as A2, I as I2, K, M as M2 } from "./MermaidPlugin-DjLUVZvB.js";
3
3
  export {
4
4
  A as ALL_TRANSFORMERS,
@@ -1 +1 @@
1
- {"version":3,"file":"code-block.d.ts","sourceRoot":"","sources":["../../src/transformers/code-block.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AAWrE,eAAO,MAAM,gCAAgC,EAAE,2BA2B9C,CAAC"}
1
+ {"version":3,"file":"code-block.d.ts","sourceRoot":"","sources":["../../src/transformers/code-block.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AAWrE,eAAO,MAAM,gCAAgC,EAAE,2BAgC9C,CAAC"}
@@ -9,5 +9,6 @@ export { MENTION_TRANSFORMER } from './mention';
9
9
  export { CODE_BLOCK_NODE_TRANSFORMER, GRID_CONTAINER_BLOCK_TRANSFORMER, HORIZONTAL_RULE_BLOCK_TRANSFORMER, IMAGE_BLOCK_TRANSFORMER, LINK_CARD_BLOCK_TRANSFORMER, MERMAID_BLOCK_TRANSFORMER, TABLE_BLOCK_TRANSFORMER, VIDEO_BLOCK_TRANSFORMER, } from './rich-blocks';
10
10
  export { RUBY_TRANSFORMER } from './ruby';
11
11
  export { SPOILER_TRANSFORMER } from './spoiler';
12
- export { SUBSCRIPT_TRANSFORMER, SUPERSCRIPT_TRANSFORMER, } from './superscript-subscript';
12
+ export { SUBSCRIPT_TRANSFORMER, SUPERSCRIPT_TRANSFORMER } from './superscript-subscript';
13
+ export { TABLE_IMPORT_TRANSFORMER } from './table';
13
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/transformers/index.ts"],"names":[],"mappings":"AA2BA,eAAO,MAAM,gBAAgB,2CA2B5B,CAAA;AAED,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAC/C,OAAO,EAAE,gCAAgC,EAAE,MAAM,cAAc,CAAA;AAC/D,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AACnD,OAAO,EAAE,4BAA4B,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAC7C,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAA;AAC3E,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAA;AAC/C,OAAO,EACL,2BAA2B,EAC3B,gCAAgC,EAChC,iCAAiC,EACjC,uBAAuB,EACvB,2BAA2B,EAC3B,yBAAyB,EACzB,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAA;AACzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAA;AAC/C,OAAO,EACL,qBAAqB,EACrB,uBAAuB,GACxB,MAAM,yBAAyB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/transformers/index.ts"],"names":[],"mappings":"AAyBA,eAAO,MAAM,gBAAgB,2CA4B5B,CAAC;AAEF,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,EAAE,gCAAgC,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,4BAA4B,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAChF,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AAC5E,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EACL,2BAA2B,EAC3B,gCAAgC,EAChC,iCAAiC,EACjC,uBAAuB,EACvB,2BAA2B,EAC3B,yBAAyB,EACzB,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AACzF,OAAO,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { MultilineElementTransformer } from '@lexical/markdown';
2
+ export declare const TABLE_IMPORT_TRANSFORMER: MultilineElementTransformer;
3
+ //# sourceMappingURL=table.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/transformers/table.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AAkBrE,eAAO,MAAM,wBAAwB,EAAE,2BA8DtC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haklex/rich-editor",
3
- "version": "0.0.75",
3
+ "version": "0.0.76",
4
4
  "description": "Core rich text editor based on Lexical",
5
5
  "repository": {
6
6
  "type": "git",
@@ -49,9 +49,9 @@
49
49
  "@lexical/code": "npm:lexical-code-no-prism@0.41.0",
50
50
  "nanoid": "^5.1.6",
51
51
  "thumbhash": "^0.1.1",
52
- "@haklex/rich-style-token": "0.0.75",
53
- "@haklex/rich-editor-ui": "0.0.75",
54
- "@haklex/rich-headless": "0.0.75"
52
+ "@haklex/rich-editor-ui": "0.0.76",
53
+ "@haklex/rich-style-token": "0.0.76",
54
+ "@haklex/rich-headless": "0.0.76"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@lexical/extension": "^0.41.0",