File size: 3,111 Bytes
8a8c5bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function getSelectionRange(root: HTMLElement | null): Range | null {
  if (!root || typeof window === "undefined") {
    return null;
  }
  const selection = window.getSelection();
  if (!selection || selection.rangeCount === 0) {
    return null;
  }
  const range = selection.getRangeAt(0);
  const anchorNode = selection.anchorNode;
  if (!anchorNode || !root.contains(anchorNode)) {
    return null;
  }
  return range;
}

function collapseRangeAtEnd(selection: Selection, node: Node): void {
  const next = document.createRange();
  next.selectNodeContents(node);
  next.collapse(false);
  selection.removeAllRanges();
  selection.addRange(next);
}

function insertList(root: HTMLElement | null, ordered: boolean): boolean {
  const range = getSelectionRange(root);
  if (!range || typeof window === "undefined") {
    return false;
  }
  const selection = window.getSelection();
  if (!selection) {
    return false;
  }
  const text = range.toString().trim();
  const list = document.createElement(ordered ? "ol" : "ul");
  const item = document.createElement("li");
  item.innerHTML = text.length > 0 ? text : "<br>";
  list.appendChild(item);
  range.deleteContents();
  range.insertNode(list);
  collapseRangeAtEnd(selection, item);
  return true;
}

function insertParagraph(root: HTMLElement | null): boolean {
  const range = getSelectionRange(root);
  if (!range || typeof window === "undefined") {
    return false;
  }
  const selection = window.getSelection();
  if (!selection) {
    return false;
  }
  const paragraph = document.createElement("p");
  paragraph.innerHTML = "<br>";
  range.insertNode(paragraph);
  collapseRangeAtEnd(selection, paragraph);
  return true;
}

function formatBlock(root: HTMLElement | null, value?: string): boolean {
  const range = getSelectionRange(root);
  if (!range || !value || typeof window === "undefined") {
    return false;
  }
  const selection = window.getSelection();
  if (!selection) {
    return false;
  }
  const normalized = value.replace(/[<>]/g, "").trim().toLowerCase();
  const tagName = normalized === "p" ? "p" : normalized;
  const block = document.createElement(tagName);
  const text = range.toString();
  block.innerHTML = text.length > 0 ? text : "<br>";
  range.deleteContents();
  range.insertNode(block);
  collapseRangeAtEnd(selection, block);
  return true;
}

export function runContentEditableCommand({
  command,
  value,
  root,
}: {
  command: string;
  value?: string;
  root: HTMLElement | null;
}): boolean {
  if (!root || typeof document === "undefined") {
    return false;
  }
  root.focus();

  const hasExec = typeof document.execCommand === "function";
  if (hasExec) {
    const ok = document.execCommand(command, false, value);
    if (ok) {
      return true;
    }
  }

  if (command === "insertUnorderedList") {
    return insertList(root, false);
  }
  if (command === "insertOrderedList") {
    return insertList(root, true);
  }
  if (command === "insertParagraph") {
    return insertParagraph(root);
  }
  if (command === "formatBlock") {
    return formatBlock(root, value);
  }

  return false;
}