Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
createContentController stories,typescript
Sultan43b6ced2d84e
diff --git a/app/shared/design-system/components/primitives/createContentController.stories.tsx b/app/shared/design-system/components/primitives/createContentController.stories.tsxnew file mode 100644index 0000000000..091859f1a8--- /dev/null+++ b/app/shared/design-system/components/primitives/createContentController.stories.tsx@@ -0,0 +1,71 @@+import { useState, useRef, useEffect, RefObject } from "react";+import { createContentController } from "./createContentController";++const useContentController = ({+ ref,+ read,+ write,+ callback,+}: {+ ref: RefObject<HTMLInputElement>;+ read: (value: HTMLInputElement) => string;+ write: (target: HTMLInputElement, value: string) => void;+ callback: (value: { name: string; value: string }) => void;+}) => {+ useEffect(() => {+ const { disconnectedCallback } = createContentController(ref.current, {+ contents: [+ {+ name: "unit",+ match: (value: string) => ["px", "em", "rem", "%"].includes(value),+ },+ {+ name: "number",+ match: (value: string) => isNaN(parseFloat(value)) === false,+ },+ { name: "unknown", match: (value: string) => value },+ ],+ read,+ write,+ onMouseMove: callback,+ onCaretMove: callback,+ });+ return () => disconnectedCallback();+ }, [ref, read, write, callback]);+};++const ExampleContentController = () => {+ const [content, setContent] = useState({ name: "none", value: "" });+ const ref = useRef<HTMLInputElement | null>(null);+ useContentController({+ ref,+ callback: (payload) => setContent(payload),+ read: (target) => target.value,+ write: (target, value) => (target.value = value),+ });+ return (+ <form style={{ fontFamily: "sans-serif" }}>+ <fieldset>+ <input+ defaultValue="100px 20% fit-content calc(0.2px, -99em)"+ ref={ref}+ style={{ width: "50%" }}+ />+ </fieldset>+ <fieldset>+ <h1>Currently on: {content.name}</h1>+ <h1>With a value of: {content.value}</h1>+ </fieldset>+ </form>+ );+};++export const DetectInput = Object.assign(ExampleContentController.bind({}), {+ args: {},+});++export default {+ title: "createContentController",+ component: ExampleContentController,+ argTypes: {},+};diff --git a/app/shared/design-system/components/primitives/createContentController.js b/app/shared/design-system/components/primitives/createContentController.tssimilarity index 60%rename from app/shared/design-system/components/primitives/createContentController.jsrename to app/shared/design-system/components/primitives/createContentController.tsindex c04679879a..667c05c01b 100644--- a/app/shared/design-system/components/primitives/createContentController.js+++ b/app/shared/design-system/components/primitives/createContentController.ts@@ -14,15 +14,16 @@* ],* onMouseMove: ({name, value, position}) => { },* onCaretMove: ({name, value, position}) => { },- * read: (value) => target.value,- * write: (value) => target.value = value,+ * read: (target) => target.value,+ * write: (target, value) => target.value = value,* });*/const canvasContext = globalThis.document- ?.createElement("canvas")- .getContext("2d");-const measureTextWidth = (value) => canvasContext.measureText(value).width;-const measureTextPadding = (node, value) => {+ .createElement("canvas")+ .getContext("2d") as CanvasRenderingContext2D & { letterSpacing: string };+const measureTextWidth = (value: string): number =>+ canvasContext.measureText(value).width;+const measureTextPadding = (node: HTMLElement, value: string) => {const {fontSize,fontFamily,@@ -34,13 +35,14 @@ const measureTextPadding = (node, value) => {} = getComputedStyle(node);if (letterSpacing !== "normal") canvasContext.letterSpacing = letterSpacing;canvasContext.font = `${fontWeight} ${fontSize} ${fontFamily}`;- return textAlign === "left"+ return ["left", "start"].includes(textAlign)? parseFloat(paddingLeft): node.clientWidth -canvasContext.measureText(value).width -parseFloat(paddingRight);};-const measureTextMetrics = (value, index, offset) => {+const measureTextMetrics = (value: string, index: number, offset: number) => {+ let length = 0;const DECIMAL_OR_WHITESPACE_OR_NON_WORD = /([-+]?\d*\.?\d+)|([\s\W])/;return value.split(DECIMAL_OR_WHITESPACE_OR_NON_WORD)@@ -56,23 +58,50 @@ const measureTextMetrics = (value, index, offset) => {});};export const createContentController = (- targetNode,+ targetNode: HTMLInputElement,{- read = (node) => node.value,- write = (value) => {},+ read = (node: HTMLInputElement) => node.value,+ write = (node: HTMLInputElement, value: string) => (node.value = value),contents = [],- onMouseMove = () => {},- onCaretMove = () => {},+ onMouseMove = (value: { name: string; value: string; position: string }) =>+ value,+ onCaretMove = (value: { name: string; value: string; position: string }) =>+ value,+ }: {+ read: (node: HTMLInputElement) => string;+ write: (node: HTMLInputElement, value: string) => void;+ contents: {+ name: string;+ value: string;+ match: (value: string) => boolean;+ }[];+ onMouseMove: (value: {+ name: string;+ value: string;+ position: string;+ }) => void;+ onCaretMove: (value: {+ name: string;+ value: string;+ position: string;+ }) => void;}-) => {- const eventNames = ["keyup", "pointermove"];- const handleEvent = ({ type, offsetX }) => {+): {+ update: (value: string) => void;+ disconnectedCallback: () => void;+} => {+ const eventNames = ["keyup", "pointermove"] as const;+ const handleEvent = (event: KeyboardEvent | PointerEvent): void => {+ let { offsetX } = event as PointerEvent;+ const { type } = event;const targetValue = read(targetNode);const targetPadding = measureTextPadding(targetNode, targetValue);if (type === eventNames[0])offsetX =targetPadding +- measureTextWidth(targetValue.slice(0, targetNode.selectionStart));+ measureTextWidth(+ targetValue.slice(0, targetNode.selectionStart || undefined)+ );const targetMetrics = measureTextMetrics(targetValue,offsetX,@@ -95,18 +124,19 @@ export const createContentController = (}}};+eventNames.forEach((eventName) =>- targetNode.addEventListener(eventName, handleEvent)+ targetNode.addEventListener(eventName, handleEvent, false));return {update: (value) => {- let { selectionStart, selectionEnd } = targetNode;- write(value);- targetNode.setSelectionRange?.(selectionStart, selectionEnd);+ const { selectionStart, selectionEnd } = targetNode;+ write(targetNode, value);+ targetNode.setSelectionRange(selectionStart, selectionEnd);},disconnectedCallback: () => {- eventName.forEach((eventName) =>- targetNode.addEventListener(eventName, handleEvent)+ eventNames.forEach((eventName) =>+ targetNode.removeEventListener(eventName, handleEvent, false));},};diff --git a/app/shared/text-editor/example-text-editor.stories.tsx b/app/shared/text-editor/example-text-editor.stories.tsxindex 01dbe938a5..a9b1e12f67 100644--- a/app/shared/text-editor/example-text-editor.stories.tsx+++ b/app/shared/text-editor/example-text-editor.stories.tsx@@ -11,7 +11,7 @@ const onChange = (editorState: EditorState) => {// Read the contents of the EditorState here.const root = $getRoot();const selection = $getSelection();-+ // eslint-disable-next-lineconsole.log(root, selection);});};