Skip to content

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.

delete content-editable

Oleg Isonen98f93f5dfea2
diff --git a/app/canvas/features/wrapper-component/editable/index.ts b/app/canvas/features/wrapper-component/editable/index.ts
deleted file mode 100644
index 7847a93944..0000000000
--- a/app/canvas/features/wrapper-component/editable/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./use-content-editable";
diff --git a/app/canvas/features/wrapper-component/editable/use-content-editable.tsx b/app/canvas/features/wrapper-component/editable/use-content-editable.tsx
deleted file mode 100644
index be48678793..0000000000
--- a/app/canvas/features/wrapper-component/editable/use-content-editable.tsx
+++ /dev/null
@@ -1,248 +0,0 @@
-import { useState, useRef, useEffect, useCallback } from "react";
-import { useHotkeys } from "react-hotkeys-hook";
-import useDebounce from "react-use/lib/useDebounce";
-import ObjectId from "bson-objectid";
-import { type ChildrenUpdates, type Instance } from "@webstudio-is/sdk";
-import { type OnChangeChildren } from "~/shared/tree-utils";
-import { primitives } from "~/shared/canvas-components";
-import { useSelectedInstance } from "~/canvas/shared/nano-states";
-import { useEditable, type Content } from "./use-editable";
-import { useToolbar } from "./use-toolbar";
-
-type ContentItem = {
- textContent: Node["textContent"];
- dataset?: { [key: string]: string | undefined };
-};
-type EditableContent = Content<ContentItem>;
-
-// Converting DOM to a data structure
-const serialize = (element: HTMLElement): EditableContent => {
- const queue: Node[] = element.firstChild ? [element.firstChild] : [];
- const items: Array<ContentItem> = [];
- let node: Node | undefined;
- while ((node = queue.pop())) {
- if (node.nextSibling) queue.push(node.nextSibling);
- if (node.firstChild) queue.push(node.firstChild);
-
- if (node.textContent === null) continue;
- if (node.nodeType === Node.TEXT_NODE) {
- const dataset = { ...node.parentElement?.dataset };
- const lastItem = items[items.length - 1];
-
- // Text node inside the element doesn't need it's parents dataset
- if (node.parentElement === element) {
- items.push({ textContent: node.textContent });
- continue;
- }
-
- // We are handling a separate text node of the same parent,
- // we can safely merge them together
- if (dataset.id === lastItem?.dataset?.id) {
- lastItem.textContent += node.textContent;
- continue;
- }
-
- // Webstudio instance
- items.push({
- textContent: node.textContent,
- dataset,
- });
- continue;
- }
-
- if (node.nodeName === "BR") {
- items.push({ textContent: "\n" });
- }
- }
-
- return {
- items,
- toString() {
- return this.items.map((item) => item.textContent).join("");
- },
- };
-};
-
-// Converting serialized content to updates array
-const toUpdates = (items: Array<ContentItem>): ChildrenUpdates => {
- const updates: ChildrenUpdates = [];
- for (const { dataset, textContent } of items) {
- if (textContent === null) continue;
-
- // It is a text node
- if (dataset === undefined) {
- updates.push(textContent);
- continue;
- }
-
- // A new instance, where id needs to be defined
- if (dataset.id === "tbd") {
- updates.push({
- id: ObjectId().toString(),
- component: dataset.component as Instance["component"],
- text: textContent,
- createInstance: true,
- });
- continue;
- }
-
- updates.push({
- id: dataset.id as Instance["id"],
- text: textContent,
- });
- }
- return updates;
-};
-
-const nodeTypeComponentMap = {
- a: "Link",
- b: "Bold",
- i: "Italic",
-} as const;
-
-const createPlaceholder = (
- nodeType: keyof typeof nodeTypeComponentMap,
- text: string
-): HTMLElement => {
- const element = document.createElement(nodeType);
- if (element instanceof HTMLAnchorElement) element.href = "";
- element.textContent = text;
- element.dataset.id = "tbd";
- element.dataset.component = nodeTypeComponentMap[nodeType];
- return element;
-};
-
-export const useContentEditable = ({
- instance,
- children,
- onChangeChildren,
-}: {
- instance: Instance;
- children: Array<JSX.Element | string>;
- onChangeChildren?: OnChangeChildren;
-}) => {
- const { isContentEditable } = primitives[instance.component];
- const [isDisabled, setIsDisabled] = useState<boolean>(true);
- const ref = useRef<HTMLDivElement>();
- const [selectedInstance] = useSelectedInstance();
- const innerHtmlRef = useRef<{ __html: string }>();
- const [contentChanged, setContentChanged] = useState<unknown>();
-
- const handleChangeContent = () => {
- if (ref.current === undefined) return;
- const { items } = serialize(ref.current);
- const updates = toUpdates(items);
- if (onChangeChildren) {
- onChangeChildren({
- instanceId: instance.id,
- updates,
- });
- }
- };
-
- const [isChangeContentDebounceReady] = useDebounce(
- () => {
- if (contentChanged !== undefined) {
- handleChangeContent();
- }
- },
- 1000,
- [contentChanged]
- );
-
- const toggleDisable = useCallback(
- (nextIsDisabled: boolean) => {
- if (isContentEditable !== false && isDisabled !== nextIsDisabled) {
- if (nextIsDisabled === false) {
- innerHtmlRef.current = { __html: ref.current?.innerHTML || "" };
- } else {
- innerHtmlRef.current = undefined;
- }
- setIsDisabled(nextIsDisabled);
- }
- },
- [isContentEditable, isDisabled]
- );
-
- const editable = useEditable<ContentItem>(ref, handleChangeContent, {
- disabled: isDisabled,
- serialize,
- });
-
- // @todo move useHotKeys, it doesn't apply to that specific instance,its global
- useHotkeys(
- "enter",
- (event) => {
- // We only want to do this if the component is the one that was clicked
- // @todo this logic shouldn't be necessary
- if (selectedInstance?.id !== instance.id) return;
- // Prevent inserting a newline when you want to start editing mode
- event.preventDefault();
- toggleDisable(false);
- },
- [toggleDisable, instance.id, selectedInstance]
- );
-
- useHotkeys(
- "esc",
- () => {
- toggleDisable(true);
- },
- { enableOnContentEditable: true },
- [toggleDisable]
- );
-
- useEffect(() => {
- if (selectedInstance?.id !== instance.id) {
- toggleDisable(true);
- }
- }, [selectedInstance, instance.id, toggleDisable]);
-
- const toolbar = useToolbar({
- editable: isContentEditable && isDisabled === false ? editable : undefined,
- onInsert({ type, text }) {
- if (ref.current === undefined) return;
- const element = createPlaceholder(type, text);
- editable.insert(element);
- handleChangeContent();
- },
- });
-
- return {
- isDisabled,
- ref,
- toolbar,
- ...(isDisabled
- ? { children }
- : { dangerouslySetInnerHTML: innerHtmlRef.current, children: undefined }),
- onBlur() {
- // We only want to do this if the component is the one that was clicked
- // @todo this logic shouldn't be necessary
- if (selectedInstance?.id !== instance.id) return;
-
- // When toolbar is open we don't want to disable editable
- // even though we lost focus
- if (toolbar !== null) return;
- toggleDisable(true);
- },
- onDoubleClick() {
- // We only want to do this if the component is the one that was clicked
- // @todo this logic shouldn't be necessary
- if (selectedInstance?.id !== instance.id) return;
- toggleDisable(false);
- },
- onInput: () => {
- // We only want to do this if the component is the one that was clicked
- // @todo this logic shouldn't be necessary
- if (selectedInstance?.id !== instance.id) return;
- if (isDisabled) return;
-
- // Makes sure we call it right away the first time because user can disable editable before we updated
- // children and this would revert rendered changes visually
- if (isChangeContentDebounceReady()) handleChangeContent();
- // We have to do this trick because content has to be serialized later
- // so we don't know yet what the content is right here.
- setContentChanged({});
- },
- };
-};
diff --git a/app/canvas/features/wrapper-component/editable/use-editable.tsx b/app/canvas/features/wrapper-component/editable/use-editable.tsx
deleted file mode 100644
index f0f2cc7280..0000000000
--- a/app/canvas/features/wrapper-component/editable/use-editable.tsx
+++ /dev/null
@@ -1,506 +0,0 @@
-/* eslint-disable @typescript-eslint/no-non-null-assertion, react-hooks/exhaustive-deps, react-hooks/rules-of-hooks */
-// Copied from https://github.com/FormidableLabs/use-editable
-// @todo create an issue there and if they agree - contribute
-// @todo had too many bugs and had to resort to an uncontrolled mode over innerHTML. It totally defeats the idea behind this code
-// but I left it here for now, maybe we can make it work later the way it was originally designed.
-
-// Main idea behind changes - ability to serialize dom nodes along with dataset, not just textContent.
-// opts.serialize is a custom serializer that allows any serialization, potentially having just an id
-// from data would be enough to back it with any data from outside by matching with that id,
-// so it doesn't have to be a custom serializer if maintainers agree to have a built-in data serialization
-
-import { useState, useLayoutEffect, useMemo } from "react";
-
-export interface Position {
- position: number;
- extent: number;
- content: string;
- line: number;
-}
-
-type History<Item> = [Position, Content<Item>];
-
-const observerSettings = {
- characterData: true,
- characterDataOldValue: true,
- childList: true,
- subtree: true,
-};
-
-const getCurrentRange = () => window.getSelection()!.getRangeAt(0)!;
-
-const setCurrentRange = (range: Range) => {
- const selection = window.getSelection()!;
- selection.empty();
- selection.addRange(range);
-};
-
-const isUndoRedoKey = (event: KeyboardEvent): boolean =>
- (event.metaKey || event.ctrlKey) && !event.altKey && event.code === "KeyZ";
-
-const setStart = (range: Range, node: Node, offset: number) => {
- if (offset < node.textContent!.length) {
- range.setStart(node, offset);
- } else {
- range.setStartAfter(node);
- }
-};
-
-const setEnd = (range: Range, node: Node, offset: number) => {
- if (offset < node.textContent!.length) {
- range.setEnd(node, offset);
- } else {
- range.setEndAfter(node);
- }
-};
-
-const getPosition = (element: HTMLElement): Position => {
- // Firefox Quirk: Since plaintext-only is unsupported the position
- // of the text here is retrieved via a range, rather than traversal
- // as seen in makeRange()
- const range = getCurrentRange();
- const extent = !range.collapsed ? range.toString().length : 0;
- const untilRange = document.createRange();
- untilRange.setStart(element, 0);
- untilRange.setEnd(range.startContainer, range.startOffset);
- let content = untilRange.toString();
- const position = content.length;
- const lines = content.split("\n");
- const line = lines.length - 1;
- content = lines[line];
- return { position, extent, content, line };
-};
-
-const makeRange = (
- element: HTMLElement,
- start: number,
- end?: number
-): Range => {
- if (start <= 0) start = 0;
- if (!end || end < 0) end = start;
-
- const range = document.createRange();
- const queue: Node[] = [element.firstChild!];
- let current = 0;
-
- let node: Node;
- let position = start;
- while ((node = queue[queue.length - 1])) {
- if (node.nodeType === Node.TEXT_NODE) {
- const length = node.textContent!.length;
- if (current + length >= position) {
- const offset = position - current;
- if (position === start) {
- setStart(range, node, offset);
- if (end !== start) {
- position = end;
- continue;
- } else {
- break;
- }
- } else {
- setEnd(range, node, offset);
- break;
- }
- }
-
- current += node.textContent!.length;
- } else if (node.nodeType === Node.ELEMENT_NODE && node.nodeName === "BR") {
- if (current + 1 >= position) {
- if (position === start) {
- setStart(range, node, 0);
- if (end !== start) {
- position = end;
- continue;
- } else {
- break;
- }
- } else {
- setEnd(range, node, 0);
- break;
- }
- }
-
- current++;
- }
-
- queue.pop();
- if (node.nextSibling) queue.push(node.nextSibling);
- if (node.firstChild) queue.push(node.firstChild);
- }
-
- return range;
-};
-
-interface State<Item> {
- observer: MutationObserver;
- disconnected: boolean;
- onChange(content: Content<Item>, position: Position): void;
- queue: MutationRecord[];
- history: Array<History<Item>>;
- historyAt: number;
- position: Position | null;
-}
-
-export interface Content<Item> {
- items: Array<Item>;
- toString: () => string;
-}
-
-export interface Options<Item> {
- disabled?: boolean;
- indentation?: number;
- serialize: (element: HTMLElement) => Content<Item>;
-}
-
-export interface Edit<Item> {
- /** Replaces the entire content of the editable while adjusting the caret position. */
- update(content: string): void;
- /** Inserts new text at the caret position while deleting text in range of the offset (which accepts negative offsets). */
- insert(append: string | HTMLElement, offset?: number): void;
- /** Positions the caret where specified */
- move(pos: number | { row: number; column: number }): void;
- /** Returns the current editor state, as usually received in onChange */
- getState(): { content: Content<Item>; position: Position };
-}
-
-const serialize = () => {
- throw new Error("Not implemented");
-};
-
-export const useEditable = <Item extends Record<string, unknown>>(
- elementRef: { current: HTMLElement | undefined | null },
- onChange: (content: Content<Item>, position: Position) => void,
- opts: Options<Item> = { serialize }
-) => {
- const unblock = useState([])[1];
- const state: State<Item> = useState(() => {
- const state: State<Item> = {
- observer: null as unknown as MutationObserver,
- disconnected: false,
- onChange,
- queue: [],
- history: [],
- historyAt: -1,
- position: null,
- };
-
- if (typeof MutationObserver !== "undefined") {
- state.observer = new MutationObserver((batch) => {
- state.queue.push(...batch);
- });
- }
-
- return state;
- })[0];
-
- const edit = useMemo<Edit<Item>>(
- () => ({
- update(content: string) {
- const { current: element } = elementRef;
- if (element) {
- const position = getPosition(element);
- const prevContent = opts.serialize(element).toString();
- position.position += content.toString().length - prevContent.length;
- state.position = position;
- state.onChange(opts.serialize(element), position);
- }
- },
- insert(append: string | HTMLElement, deleteOffset?: number) {
- const { current: element } = elementRef;
- if (element) {
- //const range = getCurrentRange();
- //range.deleteContents();
- //range.insertNode(document.createTextNode(append));
- let range = getCurrentRange();
- range.deleteContents();
- range.collapse();
- const position = getPosition(element);
- const offset = deleteOffset || 0;
- const start = position.position + (offset < 0 ? offset : 0);
- const end = position.position + (offset > 0 ? offset : 0);
- range = makeRange(element, start, end);
- range.deleteContents();
- if (typeof append === "string")
- range.insertNode(document.createTextNode(append));
- if (typeof append === "object") range.insertNode(append);
- setCurrentRange(
- makeRange(
- element,
- start +
- (typeof append === "string"
- ? append.length
- : append.textContent!.length)
- )
- );
- }
- },
- move(pos: number | { row: number; column: number }) {
- const { current: element } = elementRef;
- if (element) {
- element.focus();
- let position = 0;
- if (typeof pos === "number") {
- position = pos;
- } else {
- const lines = opts
- .serialize(element)
- .toString()
- .split("\n")
- .slice(0, pos.row);
- if (pos.row) position += lines.join("\n").length + 1;
- position += pos.column;
- }
-
- setCurrentRange(makeRange(element, position));
- }
- },
- getState() {
- const { current: element } = elementRef;
- const content = opts.serialize(element!);
- const position = getPosition(element!);
- return { content, position };
- },
- }),
- []
- );
-
- // Only for SSR / server-side logic
- if (typeof navigator !== "object") return edit;
-
- useLayoutEffect(() => {
- state.onChange = onChange;
-
- if (!elementRef.current || opts!.disabled) return;
-
- state.disconnected = false;
- state.observer.observe(elementRef.current, observerSettings);
- if (state.position) {
- const { position, extent } = state.position;
- setCurrentRange(
- makeRange(elementRef.current, position, position + extent)
- );
- }
-
- return () => {
- state.observer.disconnect();
- };
- });
-
- useLayoutEffect(() => {
- if (!elementRef.current || opts!.disabled) {
- state.history.length = 0;
- state.historyAt = -1;
- return;
- }
-
- const element = elementRef.current!;
- if (state.position) {
- element.focus();
- const { position, extent } = state.position;
- setCurrentRange(makeRange(element, position, position + extent));
- }
-
- const prevWhiteSpace = element.style.whiteSpace;
- const prevContentEditable = element.contentEditable;
- let hasPlaintextSupport = true;
- try {
- // Firefox and IE11 do not support plaintext-only mode
- element.contentEditable = "plaintext-only";
- } catch (_error) {
- element.contentEditable = "true";
- hasPlaintextSupport = false;
- }
-
- if (prevWhiteSpace !== "pre") element.style.whiteSpace = "pre-wrap";
-
- if (opts!.indentation) {
- element.style.tabSize = (
- element.style as ElementCSSInlineStyle["style"] & { MozTabSize: string }
- ).MozTabSize = "" + opts!.indentation;
- }
-
- const indentPattern = `${" ".repeat(opts!.indentation || 0)}`;
- const indentRe = new RegExp(`^(?:${indentPattern})`);
- const blanklineRe = new RegExp(`^(?:${indentPattern})*(${indentPattern})$`);
-
- let _trackStateTimestamp: number;
- const trackState = (ignoreTimestamp?: boolean) => {
- if (!elementRef.current || !state.position) return;
- const contentData = opts.serialize(element);
- const content = contentData.toString();
- const position = getPosition(element);
- const timestamp = new Date().valueOf();
-
- // Prevent recording new state in list if last one has been new enough
- const lastEntry = state.history[state.historyAt];
- if (
- (!ignoreTimestamp && timestamp - _trackStateTimestamp < 500) ||
- (lastEntry && lastEntry[1].toString() === content)
- ) {
- _trackStateTimestamp = timestamp;
- return;
- }
-
- const at = ++state.historyAt;
- state.history[at] = [position, contentData];
- state.history.splice(at + 1);
- if (at > 500) {
- state.historyAt--;
- state.history.shift();
- }
- };
-
- const disconnect = () => {
- state.observer.disconnect();
- state.disconnected = true;
- };
-
- const flushChanges = () => {
- state.queue.push(...state.observer.takeRecords());
- const position = getPosition(element);
- if (state.queue.length) {
- disconnect();
- const content = opts.serialize(element);
- state.position = position;
- let mutation: MutationRecord | void;
- let i = 0;
- while ((mutation = state.queue.pop())) {
- if (mutation.oldValue !== null)
- mutation.target.textContent = mutation.oldValue;
- for (i = mutation.removedNodes.length - 1; i >= 0; i--)
- mutation.target.insertBefore(
- mutation.removedNodes[i],
- mutation.nextSibling
- );
- for (i = mutation.addedNodes.length - 1; i >= 0; i--)
- if (mutation.addedNodes[i].parentNode)
- mutation.target.removeChild(mutation.addedNodes[i]);
- }
-
- state.onChange(content, position);
- }
- };
-
- const onKeyDown = (event: HTMLElementEventMap["keydown"]) => {
- if (event.defaultPrevented || event.target !== element) {
- return;
- } else if (state.disconnected) {
- // React Quirk: It's expected that we may lose events while disconnected, which is why
- // we'd like to block some inputs if they're unusually fast. However, this always
- // coincides with React not executing the update immediately and then getting stuck,
- // which can be prevented by issuing a dummy state change.
- event.preventDefault();
- return unblock([]);
- }
-
- if (isUndoRedoKey(event)) {
- event.preventDefault();
-
- let history: History<Item>;
- if (!event.shiftKey) {
- const at = --state.historyAt;
- history = state.history[at];
- if (!history) state.historyAt = 0;
- } else {
- const at = ++state.historyAt;
- history = state.history[at];
- if (!history) state.historyAt = state.history.length - 1;
- }
-
- if (history) {
- disconnect();
- state.position = history[0];
- state.onChange(history[1], history[0]);
- }
- return;
- } else {
- trackState();
- }
-
- if (event.key === "Enter") {
- event.preventDefault();
- // Firefox Quirk: Since plaintext-only is unsupported we must
- // ensure that only newline characters are inserted
- const position = getPosition(element);
- // We also get the current line and preserve indentation for the next
- // line that's created
- const match = /\S/g.exec(position.content);
- const index = match ? match.index : position.content.length;
- const text = "\n" + position.content.slice(0, index);
- edit.insert(text);
- } else if (
- (!hasPlaintextSupport || opts!.indentation) &&
- event.key === "Backspace"
- ) {
- // Firefox Quirk: Since plaintext-only is unsupported we must
- // ensure that only a single character is deleted
- event.preventDefault();
- const range = getCurrentRange();
- if (!range.collapsed) {
- edit.insert("", 0);
- } else {
- const position = getPosition(element);
- const match = blanklineRe.exec(position.content);
- edit.insert("", match ? -match[1].length : -1);
- }
- } else if (opts!.indentation && event.key === "Tab") {
- event.preventDefault();
- const position = getPosition(element);
- const start = position.position - position.content.length;
- const content = opts.serialize(element).toString();
- const newContent = event.shiftKey
- ? content.slice(0, start) +
- position.content.replace(indentRe, "") +
- content.slice(start + position.content.length)
- : content.slice(0, start) +
- (opts!.indentation ? " ".repeat(opts!.indentation) : "\t") +
- content.slice(start);
- edit.update(newContent);
- }
-
- // Flush changes as a key is held so the app can catch up
- if (event.repeat) flushChanges();
- };
-
- const onKeyUp = (event: HTMLElementEventMap["keyup"]) => {
- if (event.defaultPrevented || event.isComposing) return;
- if (!isUndoRedoKey(event)) trackState();
- flushChanges();
- // Chrome Quirk: The contenteditable may lose focus after the first edit or so
- element.focus();
- };
-
- const onSelect = (event: Event) => {
- // Chrome Quirk: The contenteditable may lose its selection immediately on first focus
- state.position =
- window.getSelection()!.rangeCount && event.target === element
- ? getPosition(element)
- : null;
- };
-
- //const onPaste = (event: HTMLElementEventMap["paste"]) => {
- //event.preventDefault();
- //trackState(true);
- //edit.insert(event.clipboardData!.getData("text/plain"));
- //trackState(true);
- //flushChanges();
- //};
-
- //document.addEventListener("selectstart", onSelect);
- //window.addEventListener("keydown", onKeyDown);
- //element.addEventListener("paste", onPaste);
- //element.addEventListener("keyup", onKeyUp);
-
- return () => {
- document.removeEventListener("selectstart", onSelect);
- window.removeEventListener("keydown", onKeyDown);
- //element.removeEventListener("paste", onPaste);
- element.removeEventListener("keyup", onKeyUp);
- element.style.whiteSpace = prevWhiteSpace;
- element.contentEditable = prevContentEditable;
- };
- }, [elementRef.current!, opts!.disabled, opts!.indentation]);
-
- return edit;
-};
diff --git a/app/canvas/features/wrapper-component/editable/use-toolbar.tsx b/app/canvas/features/wrapper-component/editable/use-toolbar.tsx
deleted file mode 100644
index ac9c3858b4..0000000000
--- a/app/canvas/features/wrapper-component/editable/use-toolbar.tsx
+++ /dev/null
@@ -1,65 +0,0 @@
-import { useState, useEffect } from "react";
-import debounce from "lodash.debounce";
-import { type Edit } from "./use-editable";
-import { publish, useSubscribe } from "@webstudio-is/sdk";
-import { useSelectedElement } from "~/canvas/shared/nano-states";
-
-// @todo dedupe with text-toolbar.tsx
-type Value = "b" | "i" | "a";
-
-type UseToolbarProps<Item> = {
- editable?: Edit<Item>;
- onInsert: (insert: { type: Value; text: string }) => void;
-};
-
-export const useToolbar = <Item extends Record<string, unknown>>({
- editable,
- onInsert,
-}: UseToolbarProps<Item>) => {
- const [selectionRect, setRangeRect] = useState<DOMRect | undefined>();
- const [selectedText, setSelectedText] = useState<string | undefined>();
- const [selectedElement] = useSelectedElement();
-
- const cleanup = () => {
- setSelectedText(undefined);
- setRangeRect(undefined);
- };
-
- useEffect(() => {
- if (editable === undefined) return;
- // @todo this won't clean up when unmount happens before last callback,
- // use useDebounce
- const onSelect = debounce(() => {
- if (editable === undefined) return;
- // @todo avoid doing serialization
- const state = editable.getState();
- const { position } = state;
- if (position.extent === 0) return cleanup();
- const selection = window.getSelection();
- if (selection === null) return cleanup();
- const range = selection.getRangeAt(0);
- setRangeRect(range.getBoundingClientRect());
- setSelectedText(range.toString());
- }, 50);
-
- document.addEventListener("selectionchange", onSelect);
-
- return () => {
- document.removeEventListener("selectionchange", onSelect);
- cleanup();
- };
- }, [editable]);
-
- useEffect(() => {
- publish({ type: "selectionRect", payload: selectionRect });
- }, [selectionRect]);
-
- useSubscribe<"insertInlineInstance", Value>(
- "insertInlineInstance",
- (type) => {
- if (selectedText === undefined) return;
- console.log("selected text", selectedText);
- onInsert({ type, text: selectedText });
- }
- );
-};
diff --git a/app/canvas/features/wrapper-component/wrapper-component.tsx b/app/canvas/features/wrapper-component/wrapper-component.tsx
index bb209621c5..e9f5d203c4 100644
--- a/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -7,7 +7,6 @@ import {
renderWrapperComponentChildren,
} from "@webstudio-is/sdk";
import { primitives } from "~/shared/canvas-components";
-import { useContentEditable } from "./editable";
import { useCss } from "./use-css";
import { useDraggable } from "./use-draggable";
import { useEnsureFocus } from "./use-ensure-focus";
@@ -23,37 +22,25 @@ export const WrapperComponentDev = ({
instance,
css,
children,
- onChangeChildren,
+ onChangeChildren: _onChangeChildren,
...rest
}: WrapperComponentDevProps) => {
const className = useCss({ instance, css });
- const {
- ref: contentEditableRef,
- isDisabled: isContentEditableDisabled,
- toolbar: editableToolbar,
- ...contentEditableProps
- } = useContentEditable({
- instance,
- children,
- onChangeChildren,
- });
-
const { dragRefCallback, ...draggableProps } = useDraggable({
instance,
- isDisabled: isContentEditableDisabled === false,
+ // We can't drag if we are editing text.
+ isDisabled: false,
});
const focusRefCallback = useEnsureFocus();
const refCallback = useCallback(
(element) => {
- contentEditableRef.current = element;
- // We can't drag if we are editing text.
dragRefCallback(element);
focusRefCallback(element);
},
- [contentEditableRef, dragRefCallback, focusRefCallback]
+ [dragRefCallback, focusRefCallback]
);
const userProps = useUserProps(instance.id);
@@ -63,11 +50,9 @@ export const WrapperComponentDev = ({
const { Component } = primitives[instance.component];
return (
<>
- {editableToolbar}
<Component
{...userProps}
{...rest}
- {...contentEditableProps}
{...draggableProps}
{...readonly}
className={className}
@@ -86,7 +71,7 @@ export const WrapperComponentDev = ({
event.preventDefault();
}}
>
- {renderWrapperComponentChildren(contentEditableProps.children)}
+ {renderWrapperComponentChildren(children)}
</Component>
</>
);
diff --git a/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx b/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
index 1f5d07b37d..3c81ab7c09 100644
--- a/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
+++ b/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
@@ -41,7 +41,6 @@ const getPlacement = ({
type Value = "b" | "i" | "a";
const onClickPreventDefault: MouseEventHandler<HTMLDivElement> = (event) => {
- console.log(event);
event.preventDefault();
};
@@ -86,7 +85,6 @@ type TextToolbarProps = {
export const TextToolbar = ({ publish }: TextToolbarProps) => {
const [selectionRect] = useSelectionRect();
const [element, setElement] = useState<HTMLElement | null>(null);
- console.log({ selectionRect });
const placement = useMemo(() => {
if (selectionRect === undefined || element === null) return;
const toolbarRect = element.getBoundingClientRect();