@colixsystems/widget-sdk 0.275.1 → 0.276.0

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.
@@ -0,0 +1,111 @@
1
+ // sc-15080 — `<MarkdownInput>`'s editing surface (web).
2
+ //
3
+ // Mounts the shared editor (./markdown-editor-core.js) straight into the page.
4
+ // The editor is loaded on first use, so a widget without a `<MarkdownInput>`
5
+ // never downloads it. The native mirror is ./markdown-editor-field.native.js.
6
+
7
+ import React from "react";
8
+ import { markdownEditorCss } from "./markdown-editor-css.js";
9
+ import { withTouchInputFontFloor } from "./touch-input-font-floor.js";
10
+
11
+ function hasCoarsePointer() {
12
+ return typeof window !== "undefined" && typeof window.matchMedia === "function"
13
+ ? window.matchMedia("(pointer: coarse)").matches
14
+ : false;
15
+ }
16
+
17
+ function touchFontSize(size) {
18
+ const floored = withTouchInputFontFloor({ fontSize: size }, hasCoarsePointer());
19
+ return Array.isArray(floored) ? floored[1].fontSize : size;
20
+ }
21
+
22
+ export const MarkdownEditorField = React.forwardRef(function MarkdownEditorField(
23
+ { value, onChangeText, onStateChange, placeholder, accessibilityLabel, tokens, minHeight, maxHeight, testID },
24
+ ref,
25
+ ) {
26
+ const host = React.useRef(null);
27
+ const editor = React.useRef(null);
28
+ const latest = React.useRef({ value, onChangeText, onStateChange });
29
+ latest.current = { value, onChangeText, onStateChange };
30
+ const [failed, setFailed] = React.useState(false);
31
+ const scope = `md-editor-${React.useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
32
+
33
+ React.useEffect(() => {
34
+ let cancelled = false;
35
+ import("./markdown-editor-core.js")
36
+ .then(({ createMarkdownEditor }) => {
37
+ if (cancelled || !host.current) return;
38
+ editor.current = createMarkdownEditor({
39
+ element: host.current,
40
+ markdown: latest.current.value,
41
+ placeholder,
42
+ label: accessibilityLabel,
43
+ onChange: (markdown) => {
44
+ const onChange = latest.current.onChangeText;
45
+ if (typeof onChange === "function") onChange(markdown);
46
+ },
47
+ onState: (state) => {
48
+ const onState = latest.current.onStateChange;
49
+ if (typeof onState === "function") onState(state);
50
+ },
51
+ });
52
+ // The parent may have replaced the value while the editor loaded.
53
+ editor.current.setMarkdown(latest.current.value);
54
+ })
55
+ .catch(() => {
56
+ if (!cancelled) setFailed(true);
57
+ });
58
+ return () => {
59
+ cancelled = true;
60
+ if (editor.current) editor.current.destroy();
61
+ editor.current = null;
62
+ };
63
+ // The editor keeps its undo history for the life of the field.
64
+ // eslint-disable-next-line react-hooks/exhaustive-deps
65
+ }, []);
66
+
67
+ React.useEffect(() => {
68
+ if (editor.current) editor.current.setMarkdown(value);
69
+ }, [value]);
70
+
71
+ React.useImperativeHandle(
72
+ ref,
73
+ () => ({
74
+ command: (name, args) => (editor.current ? editor.current.command(name, args) : false),
75
+ selectedText: () => (editor.current ? editor.current.selectedText() : ""),
76
+ }),
77
+ [],
78
+ );
79
+
80
+ const css = React.useMemo(
81
+ () =>
82
+ markdownEditorCss(tokens, `.${scope}`, {
83
+ fontSize: touchFontSize(tokens.bodySize),
84
+ minHeight,
85
+ framed: true,
86
+ }),
87
+ [tokens, scope, minHeight],
88
+ );
89
+
90
+ return React.createElement(
91
+ "div",
92
+ {
93
+ className: scope,
94
+ "data-testid": testID,
95
+ style: {
96
+ minHeight,
97
+ maxHeight,
98
+ overflowY: "auto",
99
+ boxSizing: "border-box",
100
+ },
101
+ },
102
+ React.createElement("style", null, css),
103
+ failed
104
+ ? React.createElement(
105
+ "div",
106
+ { role: "alert", style: { padding: tokens.padding, color: tokens.mutedColor } },
107
+ "The editor could not load. Check your connection and reload the page.",
108
+ )
109
+ : React.createElement("div", { ref: host }),
110
+ );
111
+ });
@@ -0,0 +1,9 @@
1
+ // sc-15080 — `<MarkdownInput>`'s editing surface (iOS and Android): the shared
2
+ // editor in a WebView, through ./markdown-editor-bridge.js. The web mirror is
3
+ // ./markdown-editor-field.js.
4
+
5
+ import { View } from "react-native";
6
+ import { WebView } from "react-native-webview";
7
+ import { makeMarkdownEditorField } from "./markdown-editor-bridge.js";
8
+
9
+ export const MarkdownEditorField = makeMarkdownEditorField({ View, WebView });