rte-builder 2.0.4 → 2.0.6

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/dist/index.mjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use client";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
9
7
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
@@ -14,9 +12,6 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
14
12
  var __esm = (fn, res) => function __init() {
15
13
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
16
14
  };
17
- var __commonJS = (cb, mod) => function __require2() {
18
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
19
- };
20
15
  var __export = (target, all) => {
21
16
  for (var name in all)
22
17
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -29,14 +24,6 @@ var __copyProps = (to, from, except, desc) => {
29
24
  }
30
25
  return to;
31
26
  };
32
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
33
- // If the importer is in node compatibility mode or this is not an ESM
34
- // file that has been converted to a CommonJS file using a Babel-
35
- // compatible transform (i.e. "__esModule" has not been set), then set
36
- // "default" to the CommonJS "module.exports" for node compatibility.
37
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
38
- mod
39
- ));
40
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
41
28
 
42
29
  // src/extensions/FontSize.ts
@@ -1595,6 +1582,7 @@ var init_Indent = __esm({
1595
1582
 
1596
1583
  // src/adapters/tiptap/TipTapToolbar.tsx
1597
1584
  import { useState, useRef, useEffect } from "react";
1585
+ import { CodeXml } from "lucide-react";
1598
1586
  import { jsx, jsxs } from "react/jsx-runtime";
1599
1587
  var FONT_FAMILIES, FONT_SIZES, LINE_HEIGHTS, EmojiPicker, TipTapToolbar;
1600
1588
  var init_TipTapToolbar = __esm({
@@ -1697,7 +1685,10 @@ var init_TipTapToolbar = __esm({
1697
1685
  {
1698
1686
  ref: buttonRef,
1699
1687
  type: "button",
1700
- onClick,
1688
+ onMouseDown: (e) => {
1689
+ e.preventDefault();
1690
+ onClick();
1691
+ },
1701
1692
  disabled,
1702
1693
  title,
1703
1694
  className: `rte-builder-toolbar-btn ${active ? "active" : ""} ${disabled ? "disabled" : ""}`,
@@ -1781,27 +1772,43 @@ var init_TipTapToolbar = __esm({
1781
1772
  return /* @__PURE__ */ jsx(
1782
1773
  ToolbarButton3,
1783
1774
  {
1784
- onClick: () => editor.chain().focus().toggleCodeBlock().run(),
1785
- active: editor.isActive("codeBlock"),
1786
- title: "Code Block",
1787
- children: /* @__PURE__ */ jsxs(
1788
- "svg",
1789
- {
1790
- width: "18",
1791
- height: "18",
1792
- viewBox: "0 0 24 24",
1793
- fill: "none",
1794
- stroke: "currentColor",
1795
- strokeWidth: "2",
1796
- strokeLinecap: "round",
1797
- strokeLinejoin: "round",
1798
- children: [
1799
- /* @__PURE__ */ jsx("rect", { x: "3", y: "3", width: "18", height: "18", rx: "2", ry: "2" }),
1800
- /* @__PURE__ */ jsx("polyline", { points: "9 8 5 12 9 16" }),
1801
- /* @__PURE__ */ jsx("polyline", { points: "15 8 19 12 15 16" })
1802
- ]
1775
+ onClick: () => {
1776
+ if (editor.isActive("codeBlock")) {
1777
+ let htmlSource = "";
1778
+ editor.state.doc.descendants((node) => {
1779
+ if (node.type.name === "codeBlock") {
1780
+ htmlSource = node.textContent;
1781
+ return false;
1782
+ }
1783
+ });
1784
+ htmlSource = htmlSource.trim();
1785
+ if (htmlSource) {
1786
+ editor.commands.setContent(htmlSource, true, {
1787
+ preserveWhitespace: false
1788
+ });
1789
+ } else {
1790
+ editor.commands.clearContent(true);
1791
+ }
1792
+ editor.commands.focus();
1793
+ } else {
1794
+ const currentHTML = editor.getHTML();
1795
+ const isEmptyContent = !currentHTML || currentHTML === "<p></p>" || currentHTML.trim() === "";
1796
+ const codeBlockContent = isEmptyContent ? { type: "doc", content: [{ type: "codeBlock" }] } : {
1797
+ type: "doc",
1798
+ content: [
1799
+ {
1800
+ type: "codeBlock",
1801
+ content: [{ type: "text", text: currentHTML }]
1802
+ }
1803
+ ]
1804
+ };
1805
+ editor.commands.setContent(codeBlockContent, false);
1806
+ editor.commands.focus();
1803
1807
  }
1804
- )
1808
+ },
1809
+ active: editor.isActive("codeBlock"),
1810
+ title: "Code Block (HTML Source)",
1811
+ children: /* @__PURE__ */ jsx(CodeXml, { size: 18 })
1805
1812
  },
1806
1813
  "codeBlock"
1807
1814
  );
@@ -2282,7 +2289,8 @@ var init_TipTapToolbar = __esm({
2282
2289
  return null;
2283
2290
  }
2284
2291
  };
2285
- return /* @__PURE__ */ jsx("div", { className: "rte-builder-toolbar", children: buttons.map(renderButton) });
2292
+ const isSourceMode = editor.isActive("codeBlock");
2293
+ return /* @__PURE__ */ jsx("div", { className: `rte-builder-toolbar${isSourceMode ? " rte-builder-toolbar-source-mode" : ""}`, children: buttons.map(renderButton) });
2286
2294
  };
2287
2295
  }
2288
2296
  });
@@ -2587,203 +2595,6 @@ var init_TipTapEditorComponent = __esm({
2587
2595
  }
2588
2596
  });
2589
2597
 
2590
- // node_modules/is-hotkey/lib/index.js
2591
- var require_lib = __commonJS({
2592
- "node_modules/is-hotkey/lib/index.js"(exports) {
2593
- "use strict";
2594
- Object.defineProperty(exports, "__esModule", {
2595
- value: true
2596
- });
2597
- var IS_MAC = typeof window != "undefined" && /Mac|iPod|iPhone|iPad/.test(window.navigator.platform);
2598
- var MODIFIERS = {
2599
- alt: "altKey",
2600
- control: "ctrlKey",
2601
- meta: "metaKey",
2602
- shift: "shiftKey"
2603
- };
2604
- var ALIASES = {
2605
- add: "+",
2606
- break: "pause",
2607
- cmd: "meta",
2608
- command: "meta",
2609
- ctl: "control",
2610
- ctrl: "control",
2611
- del: "delete",
2612
- down: "arrowdown",
2613
- esc: "escape",
2614
- ins: "insert",
2615
- left: "arrowleft",
2616
- mod: IS_MAC ? "meta" : "control",
2617
- opt: "alt",
2618
- option: "alt",
2619
- return: "enter",
2620
- right: "arrowright",
2621
- space: " ",
2622
- spacebar: " ",
2623
- up: "arrowup",
2624
- win: "meta",
2625
- windows: "meta"
2626
- };
2627
- var CODES = {
2628
- backspace: 8,
2629
- tab: 9,
2630
- enter: 13,
2631
- shift: 16,
2632
- control: 17,
2633
- alt: 18,
2634
- pause: 19,
2635
- capslock: 20,
2636
- escape: 27,
2637
- " ": 32,
2638
- pageup: 33,
2639
- pagedown: 34,
2640
- end: 35,
2641
- home: 36,
2642
- arrowleft: 37,
2643
- arrowup: 38,
2644
- arrowright: 39,
2645
- arrowdown: 40,
2646
- insert: 45,
2647
- delete: 46,
2648
- meta: 91,
2649
- numlock: 144,
2650
- scrolllock: 145,
2651
- ";": 186,
2652
- "=": 187,
2653
- ",": 188,
2654
- "-": 189,
2655
- ".": 190,
2656
- "/": 191,
2657
- "`": 192,
2658
- "[": 219,
2659
- "\\": 220,
2660
- "]": 221,
2661
- "'": 222
2662
- };
2663
- for (f = 1; f < 20; f++) {
2664
- CODES["f" + f] = 111 + f;
2665
- }
2666
- var f;
2667
- function isHotkey2(hotkey, options, event) {
2668
- if (options && !("byKey" in options)) {
2669
- event = options;
2670
- options = null;
2671
- }
2672
- if (!Array.isArray(hotkey)) {
2673
- hotkey = [hotkey];
2674
- }
2675
- var array = hotkey.map(function(string) {
2676
- return parseHotkey(string, options);
2677
- });
2678
- var check = function check2(e) {
2679
- return array.some(function(object) {
2680
- return compareHotkey(object, e);
2681
- });
2682
- };
2683
- var ret = event == null ? check : check(event);
2684
- return ret;
2685
- }
2686
- function isCodeHotkey(hotkey, event) {
2687
- return isHotkey2(hotkey, event);
2688
- }
2689
- function isKeyHotkey(hotkey, event) {
2690
- return isHotkey2(hotkey, { byKey: true }, event);
2691
- }
2692
- function parseHotkey(hotkey, options) {
2693
- var byKey = options && options.byKey;
2694
- var ret = {};
2695
- hotkey = hotkey.replace("++", "+add");
2696
- var values = hotkey.split("+");
2697
- var length = values.length;
2698
- for (var k in MODIFIERS) {
2699
- ret[MODIFIERS[k]] = false;
2700
- }
2701
- var _iteratorNormalCompletion = true;
2702
- var _didIteratorError = false;
2703
- var _iteratorError = void 0;
2704
- try {
2705
- for (var _iterator = values[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
2706
- var value = _step.value;
2707
- var optional = value.endsWith("?") && value.length > 1;
2708
- if (optional) {
2709
- value = value.slice(0, -1);
2710
- }
2711
- var name = toKeyName(value);
2712
- var modifier = MODIFIERS[name];
2713
- if (value.length > 1 && !modifier && !ALIASES[value] && !CODES[name]) {
2714
- throw new TypeError('Unknown modifier: "' + value + '"');
2715
- }
2716
- if (length === 1 || !modifier) {
2717
- if (byKey) {
2718
- ret.key = name;
2719
- } else {
2720
- ret.which = toKeyCode(value);
2721
- }
2722
- }
2723
- if (modifier) {
2724
- ret[modifier] = optional ? null : true;
2725
- }
2726
- }
2727
- } catch (err) {
2728
- _didIteratorError = true;
2729
- _iteratorError = err;
2730
- } finally {
2731
- try {
2732
- if (!_iteratorNormalCompletion && _iterator.return) {
2733
- _iterator.return();
2734
- }
2735
- } finally {
2736
- if (_didIteratorError) {
2737
- throw _iteratorError;
2738
- }
2739
- }
2740
- }
2741
- return ret;
2742
- }
2743
- function compareHotkey(object, event) {
2744
- for (var key in object) {
2745
- var expected = object[key];
2746
- var actual = void 0;
2747
- if (expected == null) {
2748
- continue;
2749
- }
2750
- if (key === "key" && event.key != null) {
2751
- actual = event.key.toLowerCase();
2752
- } else if (key === "which") {
2753
- actual = expected === 91 && event.which === 93 ? 91 : event.which;
2754
- } else {
2755
- actual = event[key];
2756
- }
2757
- if (actual == null && expected === false) {
2758
- continue;
2759
- }
2760
- if (actual !== expected) {
2761
- return false;
2762
- }
2763
- }
2764
- return true;
2765
- }
2766
- function toKeyCode(name) {
2767
- name = toKeyName(name);
2768
- var code = CODES[name] || name.toUpperCase().charCodeAt(0);
2769
- return code;
2770
- }
2771
- function toKeyName(name) {
2772
- name = name.toLowerCase();
2773
- name = ALIASES[name] || name;
2774
- return name;
2775
- }
2776
- exports.default = isHotkey2;
2777
- exports.isHotkey = isHotkey2;
2778
- exports.isCodeHotkey = isCodeHotkey;
2779
- exports.isKeyHotkey = isKeyHotkey;
2780
- exports.parseHotkey = parseHotkey;
2781
- exports.compareHotkey = compareHotkey;
2782
- exports.toKeyCode = toKeyCode;
2783
- exports.toKeyName = toKeyName;
2784
- }
2785
- });
2786
-
2787
2598
  // src/adapters/slate/SlateToolbar.tsx
2788
2599
  import { useCallback as useCallback2, useState as useState2, useRef as useRef2, useEffect as useEffect3 } from "react";
2789
2600
  import { Editor, Transforms, Element as SlateElement } from "slate";
@@ -3695,12 +3506,12 @@ import {
3695
3506
  import { createEditor, Editor as Editor2, Transforms as Transforms2, Text as Text2, Element as SlateElement2, Node as Node2 } from "slate";
3696
3507
  import { Slate, Editable, withReact, ReactEditor as ReactEditor2 } from "slate-react";
3697
3508
  import { withHistory, HistoryEditor } from "slate-history";
3509
+ import isHotkey from "is-hotkey";
3698
3510
  import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
3699
- var import_is_hotkey, HOTKEYS, LIST_TYPES, TEXT_ALIGN_TYPES, isMarkActive, isBlockActive, toggleMark, toggleBlock, serializeToHtml, serializeNode, escapeHtml, deserializeFromHtml, deserializeElement, renderElement, renderLeaf, withInlines, SlateEditorComponent, SlateEditorComponent_default;
3511
+ var HOTKEYS, LIST_TYPES, TEXT_ALIGN_TYPES, isMarkActive, isBlockActive, toggleMark, toggleBlock, serializeToHtml, serializeNode, escapeHtml, deserializeFromHtml, deserializeElement, renderElement, renderLeaf, withInlines, SlateEditorComponent, SlateEditorComponent_default;
3700
3512
  var init_SlateEditorComponent = __esm({
3701
3513
  "src/adapters/slate/SlateEditorComponent.tsx"() {
3702
3514
  "use strict";
3703
- import_is_hotkey = __toESM(require_lib());
3704
3515
  init_SlateToolbar();
3705
3516
  HOTKEYS = {
3706
3517
  "mod+b": "bold",
@@ -4040,7 +3851,7 @@ var init_SlateEditorComponent = __esm({
4040
3851
  const handleKeyDown = useCallback3(
4041
3852
  (event) => {
4042
3853
  for (const hotkey in HOTKEYS) {
4043
- if ((0, import_is_hotkey.default)(hotkey, event)) {
3854
+ if (isHotkey(hotkey, event)) {
4044
3855
  event.preventDefault();
4045
3856
  const mark = HOTKEYS[hotkey];
4046
3857
  toggleMark(editor, mark);
@@ -6190,7 +6001,6 @@ import {
6190
6001
  Underline as Underline2,
6191
6002
  Strikethrough,
6192
6003
  Code as Code2,
6193
- FileCode,
6194
6004
  Subscript as Subscript2,
6195
6005
  Superscript as Superscript2,
6196
6006
  RemoveFormatting,
@@ -6217,7 +6027,8 @@ import {
6217
6027
  Minimize,
6218
6028
  Printer,
6219
6029
  Undo,
6220
- Redo
6030
+ Redo,
6031
+ CodeXml as CodeXml2
6221
6032
  } from "lucide-react";
6222
6033
  import { jsx as jsx8, jsxs as jsxs8 } from "react/jsx-runtime";
6223
6034
  var FONT_FAMILIES2 = [
@@ -6316,7 +6127,10 @@ var Toolbar = ({
6316
6127
  {
6317
6128
  ref: buttonRef,
6318
6129
  type: "button",
6319
- onClick,
6130
+ onMouseDown: (e) => {
6131
+ e.preventDefault();
6132
+ onClick();
6133
+ },
6320
6134
  disabled,
6321
6135
  title,
6322
6136
  className: `rte-builder-toolbar-btn ${active ? "active" : ""} ${disabled ? "disabled" : ""}`,
@@ -6384,10 +6198,43 @@ var Toolbar = ({
6384
6198
  return /* @__PURE__ */ jsx8(
6385
6199
  ToolbarButton3,
6386
6200
  {
6387
- onClick: () => editor.chain().focus().toggleCodeBlock().run(),
6201
+ onClick: () => {
6202
+ if (editor.isActive("codeBlock")) {
6203
+ let htmlSource = "";
6204
+ editor.state.doc.descendants((node) => {
6205
+ if (node.type.name === "codeBlock") {
6206
+ htmlSource = node.textContent;
6207
+ return false;
6208
+ }
6209
+ });
6210
+ htmlSource = htmlSource.trim();
6211
+ if (htmlSource) {
6212
+ editor.commands.setContent(htmlSource, true, {
6213
+ preserveWhitespace: false
6214
+ });
6215
+ } else {
6216
+ editor.commands.clearContent(true);
6217
+ }
6218
+ editor.commands.focus();
6219
+ } else {
6220
+ const currentHTML = editor.getHTML();
6221
+ const isEmptyContent = !currentHTML || currentHTML === "<p></p>" || currentHTML.trim() === "";
6222
+ const codeBlockContent = isEmptyContent ? { type: "doc", content: [{ type: "codeBlock" }] } : {
6223
+ type: "doc",
6224
+ content: [
6225
+ {
6226
+ type: "codeBlock",
6227
+ content: [{ type: "text", text: currentHTML }]
6228
+ }
6229
+ ]
6230
+ };
6231
+ editor.commands.setContent(codeBlockContent, false);
6232
+ editor.commands.focus();
6233
+ }
6234
+ },
6388
6235
  active: editor.isActive("codeBlock"),
6389
- title: "Code Block",
6390
- children: /* @__PURE__ */ jsx8(FileCode, { size: 18 })
6236
+ title: "Code Block (HTML Source)",
6237
+ children: /* @__PURE__ */ jsx8(CodeXml2, { size: 18 })
6391
6238
  },
6392
6239
  "codeBlock"
6393
6240
  );
@@ -6780,7 +6627,8 @@ var Toolbar = ({
6780
6627
  return null;
6781
6628
  }
6782
6629
  };
6783
- return /* @__PURE__ */ jsx8("div", { className: "rte-builder-toolbar", children: buttons.map(renderButton) });
6630
+ const isSourceMode = editor.isActive("codeBlock");
6631
+ return /* @__PURE__ */ jsx8("div", { className: `rte-builder-toolbar${isSourceMode ? " rte-builder-toolbar-source-mode" : ""}`, children: buttons.map(renderButton) });
6784
6632
  };
6785
6633
 
6786
6634
  // src/components/RichTextEditor.tsx