@malaya_jeeva/rich-text-editor 1.0.9 → 1.0.10

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.js CHANGED
@@ -1244,14 +1244,26 @@ function RichTextEditor({ value, onChange, toolbar }) {
1244
1244
  setWords(txt.trim() ? txt.trim().split(/\s+/).length : 0);
1245
1245
  onChange == null ? void 0 : onChange(sanitizeScriptTags((_d = (_c = editorRef.current) == null ? void 0 : _c.innerHTML) != null ? _d : ""));
1246
1246
  }, [onChange, calcFloat, linkBar, sanitizeScriptTags]);
1247
+ const fixListInParagraph = (0, import_react5.useCallback)(() => {
1248
+ var _a2;
1249
+ (_a2 = editorRef.current) == null ? void 0 : _a2.querySelectorAll("p > ol, p > ul").forEach((list) => {
1250
+ var _a3;
1251
+ const p = list.parentElement;
1252
+ p.insertAdjacentElement("afterend", list);
1253
+ if (!((_a3 = p.textContent) == null ? void 0 : _a3.trim()) && !p.querySelector("img, br")) p.remove();
1254
+ });
1255
+ }, []);
1247
1256
  const exec = (0, import_react5.useCallback)((cmd, val = null) => {
1248
1257
  var _a2;
1249
1258
  (_a2 = editorRef.current) == null ? void 0 : _a2.focus();
1250
1259
  document.execCommand(cmd, false, val != null ? val : void 0);
1251
1260
  fixFontTags();
1252
1261
  fixListNesting();
1262
+ if (cmd === "insertUnorderedList" || cmd === "insertOrderedList") {
1263
+ fixListInParagraph();
1264
+ }
1253
1265
  refresh();
1254
- }, [fixFontTags, fixListNesting, refresh]);
1266
+ }, [fixFontTags, fixListNesting, fixListInParagraph, refresh]);
1255
1267
  const insertImage = (0, import_react5.useCallback)((src) => {
1256
1268
  var _a2;
1257
1269
  (_a2 = editorRef.current) == null ? void 0 : _a2.focus();
@@ -1298,6 +1310,16 @@ function RichTextEditor({ value, onChange, toolbar }) {
1298
1310
  var _a2, _b;
1299
1311
  clearSelection();
1300
1312
  clearImageSel();
1313
+ if (!e.ctrlKey && !e.metaKey && !e.altKey && e.key.length === 1) {
1314
+ const sel = window.getSelection();
1315
+ if (sel == null ? void 0 : sel.rangeCount) {
1316
+ const anchor = sel.anchorNode;
1317
+ const parent = (anchor == null ? void 0 : anchor.nodeType) === Node.TEXT_NODE ? anchor.parentElement : anchor;
1318
+ if (parent === editorRef.current) {
1319
+ document.execCommand("formatBlock", false, fmt.block || "p");
1320
+ }
1321
+ }
1322
+ }
1301
1323
  if (e.key === "Tab") {
1302
1324
  e.preventDefault();
1303
1325
  const cell = getCurrentCell();
@@ -1329,7 +1351,7 @@ function RichTextEditor({ value, onChange, toolbar }) {
1329
1351
  exec("underline");
1330
1352
  }
1331
1353
  }
1332
- }, [exec, clearSelection, clearImageSel]);
1354
+ }, [exec, clearSelection, clearImageSel, fmt.block]);
1333
1355
  const insertTable = (rows, cols) => {
1334
1356
  var _a2;
1335
1357
  (_a2 = editorRef.current) == null ? void 0 : _a2.focus();
@@ -1495,6 +1517,7 @@ function RichTextEditor({ value, onChange, toolbar }) {
1495
1517
  };
1496
1518
  (0, import_react5.useEffect)(() => {
1497
1519
  if (editorRef.current) {
1520
+ document.execCommand("defaultParagraphSeparator", false, "p");
1498
1521
  editorRef.current.innerHTML = sanitizeScriptTags(value != null ? value : "<p>Start writing here...</p>");
1499
1522
  refresh();
1500
1523
  }
package/dist/index.mjs CHANGED
@@ -1223,14 +1223,26 @@ function RichTextEditor({ value, onChange, toolbar }) {
1223
1223
  setWords(txt.trim() ? txt.trim().split(/\s+/).length : 0);
1224
1224
  onChange == null ? void 0 : onChange(sanitizeScriptTags((_d = (_c = editorRef.current) == null ? void 0 : _c.innerHTML) != null ? _d : ""));
1225
1225
  }, [onChange, calcFloat, linkBar, sanitizeScriptTags]);
1226
+ const fixListInParagraph = useCallback3(() => {
1227
+ var _a2;
1228
+ (_a2 = editorRef.current) == null ? void 0 : _a2.querySelectorAll("p > ol, p > ul").forEach((list) => {
1229
+ var _a3;
1230
+ const p = list.parentElement;
1231
+ p.insertAdjacentElement("afterend", list);
1232
+ if (!((_a3 = p.textContent) == null ? void 0 : _a3.trim()) && !p.querySelector("img, br")) p.remove();
1233
+ });
1234
+ }, []);
1226
1235
  const exec = useCallback3((cmd, val = null) => {
1227
1236
  var _a2;
1228
1237
  (_a2 = editorRef.current) == null ? void 0 : _a2.focus();
1229
1238
  document.execCommand(cmd, false, val != null ? val : void 0);
1230
1239
  fixFontTags();
1231
1240
  fixListNesting();
1241
+ if (cmd === "insertUnorderedList" || cmd === "insertOrderedList") {
1242
+ fixListInParagraph();
1243
+ }
1232
1244
  refresh();
1233
- }, [fixFontTags, fixListNesting, refresh]);
1245
+ }, [fixFontTags, fixListNesting, fixListInParagraph, refresh]);
1234
1246
  const insertImage = useCallback3((src) => {
1235
1247
  var _a2;
1236
1248
  (_a2 = editorRef.current) == null ? void 0 : _a2.focus();
@@ -1277,6 +1289,16 @@ function RichTextEditor({ value, onChange, toolbar }) {
1277
1289
  var _a2, _b;
1278
1290
  clearSelection();
1279
1291
  clearImageSel();
1292
+ if (!e.ctrlKey && !e.metaKey && !e.altKey && e.key.length === 1) {
1293
+ const sel = window.getSelection();
1294
+ if (sel == null ? void 0 : sel.rangeCount) {
1295
+ const anchor = sel.anchorNode;
1296
+ const parent = (anchor == null ? void 0 : anchor.nodeType) === Node.TEXT_NODE ? anchor.parentElement : anchor;
1297
+ if (parent === editorRef.current) {
1298
+ document.execCommand("formatBlock", false, fmt.block || "p");
1299
+ }
1300
+ }
1301
+ }
1280
1302
  if (e.key === "Tab") {
1281
1303
  e.preventDefault();
1282
1304
  const cell = getCurrentCell();
@@ -1308,7 +1330,7 @@ function RichTextEditor({ value, onChange, toolbar }) {
1308
1330
  exec("underline");
1309
1331
  }
1310
1332
  }
1311
- }, [exec, clearSelection, clearImageSel]);
1333
+ }, [exec, clearSelection, clearImageSel, fmt.block]);
1312
1334
  const insertTable = (rows, cols) => {
1313
1335
  var _a2;
1314
1336
  (_a2 = editorRef.current) == null ? void 0 : _a2.focus();
@@ -1474,6 +1496,7 @@ function RichTextEditor({ value, onChange, toolbar }) {
1474
1496
  };
1475
1497
  useEffect3(() => {
1476
1498
  if (editorRef.current) {
1499
+ document.execCommand("defaultParagraphSeparator", false, "p");
1477
1500
  editorRef.current.innerHTML = sanitizeScriptTags(value != null ? value : "<p>Start writing here...</p>");
1478
1501
  refresh();
1479
1502
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malaya_jeeva/rich-text-editor",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "Custom React Rich Text Editor",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",