smartrte-core 0.2.0 → 0.2.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.1
4
+
5
+ - Keep superscript and subscript mutually exclusive when converting selected text.
6
+
3
7
  ## 0.2.0
4
8
 
5
9
  - Add block alignment properties and the `setTextAlignment` command.
@@ -89,11 +89,28 @@ const createMarkCommand = (id, nextMark, toggle) => ({
89
89
  },
90
90
  });
91
91
  const toggle = (id, type) => createMarkCommand(id, () => ({ type }), true);
92
+ const toggleScript = (id, type) => ({
93
+ id,
94
+ isEnabled: (context) => Boolean(getTextRange(context)),
95
+ execute: (context) => {
96
+ const range = getTextRange(context);
97
+ if (!range)
98
+ throw new Error(`${id} requires text in one inline container.`);
99
+ const remove = range.nodes
100
+ .slice(range.startIndex, range.endIndex + 1)
101
+ .every((node) => hasMark(node.marks, type));
102
+ const opposite = type === "superscript" ? "subscript" : "superscript";
103
+ return createMarkTransaction(id, context, undefined, (marks) => {
104
+ const withoutOpposite = removeMark(marks, opposite);
105
+ return remove ? removeMark(withoutOpposite, type) : addMark(withoutOpposite, { type });
106
+ });
107
+ },
108
+ });
92
109
  export const toggleBold = toggle("toggle-bold", "bold");
93
110
  export const toggleItalic = toggle("toggle-italic", "italic");
94
111
  export const toggleUnderline = toggle("toggle-underline", "underline");
95
- export const toggleSuperscript = toggle("toggle-superscript", "superscript");
96
- export const toggleSubscript = toggle("toggle-subscript", "subscript");
112
+ export const toggleSuperscript = toggleScript("toggle-superscript", "superscript");
113
+ export const toggleSubscript = toggleScript("toggle-subscript", "subscript");
97
114
  export const applyTextColor = createMarkCommand("apply-text-color", (value) => ({ type: "textColor", value }), false);
98
115
  export const applyBackgroundColor = createMarkCommand("apply-background-color", (value) => ({ type: "backgroundColor", value }), false);
99
116
  export const applyFontSize = createMarkCommand("apply-font-size", (valuePx) => ({ type: "fontSize", valuePx }), false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smartrte-core",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Framework-agnostic document model and editing primitives for Smart RTE",
5
5
  "repository": {
6
6
  "type": "git",