Skip to content

Instantly share code, notes, and snippets.

@diegohaz
Last active August 18, 2022 12:39
Show Gist options
  • Save diegohaz/c48c82775c0ac36fda45987e73e9787c to your computer and use it in GitHub Desktop.
Save diegohaz/c48c82775c0ac36fda45987e73e9787c to your computer and use it in GitHub Desktop.

Revisions

  1. diegohaz revised this gist May 29, 2021. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -44,14 +44,15 @@ const Link = createBlock<LinkProps>((props) => {
    });

    const ListItem = createBlock((props) => {
    const { value, insertBlock } = useEditorState(props);
    const { value, insertBlock, removeBlock } = useEditorState(props);
    useEnter((defaultAction) => {
    if (value.length) {
    insertBlock("ListItem");
    } else {
    removeBlock(props.id);
    defaultAction();
    }
    }, [value, insertBlock]);
    }, [value, insertBlock, removeBlock, props.id]);
    return <Text as="li" {...props} />;
    });

  2. diegohaz revised this gist May 29, 2021. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,7 @@ const Link = createBlock<LinkProps>((props) => {

    useEffect(() => {
    return onSelectionChange((selection) => {
    // Check parents
    if (selection.startContainerId === props.id || selection.endContainerId === props.id) {
    if (selection.startContainerIds.includes(props.id) && selection.endContainerIds.includes(props.id)) {
    popover.show();
    }
    });
  3. diegohaz revised this gist May 29, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,7 @@ const Link = createBlock<LinkProps>((props) => {

    useEffect(() => {
    return onSelectionChange((selection) => {
    // Check parents
    if (selection.startContainerId === props.id || selection.endContainerId === props.id) {
    popover.show();
    }
  4. diegohaz revised this gist May 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ const Link = createBlock<LinkProps>((props) => {
    props.onClick?.(event);
    if (event.defaultPrevented) return;
    event.preventDefault();
    popover.selectBlockConents();
    selectBlockConents(props.id);
    }}
    >
    {(disclosureProps) => (
  5. diegohaz revised this gist May 29, 2021. 1 changed file with 25 additions and 3 deletions.
    28 changes: 25 additions & 3 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,41 @@
    type LinkProps = AnchorHTMLAttributes<HTMLAnchorElement>;

    const Link = createBlock<LinkProps>((props) => {
    const popover = usePopoverState();
    const { onSelectionChange, selectBlockContents } = useEditorState(props);
    const popover = usePopoverState({ portal: true });
    const [href, setHref] = useState(props.href);

    useEffect(() => {
    setHref(props.href);
    }, [props.href]);

    useEffect(() => {
    return onSelectionChange((selection) => {
    if (selection.startContainerId === props.id || selection.endContainerId === props.id) {
    popover.show();
    }
    });
    }, [onSelect, props.id, popover.show]);

    return (
    <>
    <PopoverDisclosure as="a" state={state} {...props} href={href}>
    <PopoverDisclosure
    as="a"
    state={popover}
    {...props}
    href={href}
    onClick={(event) => {
    props.onClick?.(event);
    if (event.defaultPrevented) return;
    event.preventDefault();
    popover.selectBlockConents();
    }}
    >
    {(disclosureProps) => (
    <Text as="a" {...disclosureProps} />
    )}
    </PopoverDisclosure>
    <Popover state={state}>
    <Popover autoFocusOnShow={false} state={popover}>
    <a href={href} />
    <button>Edit</button>
    </Popover>
  6. diegohaz revised this gist May 29, 2021. 1 changed file with 35 additions and 0 deletions.
    35 changes: 35 additions & 0 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,38 @@
    type LinkProps = AnchorHTMLAttributes<HTMLAnchorElement>;

    const Link = createBlock<LinkProps>((props) => {
    const popover = usePopoverState();
    const [href, setHref] = useState(props.href);
    useEffect(() => {
    setHref(props.href);
    }, [props.href]);
    return (
    <>
    <PopoverDisclosure as="a" state={state} {...props} href={href}>
    {(disclosureProps) => (
    <Text as="a" {...disclosureProps} />
    )}
    </PopoverDisclosure>
    <Popover state={state}>
    <a href={href} />
    <button>Edit</button>
    </Popover>
    </>
    );
    });

    const ListItem = createBlock((props) => {
    const { value, insertBlock } = useEditorState(props);
    useEnter((defaultAction) => {
    if (value.length) {
    insertBlock("ListItem");
    } else {
    defaultAction();
    }
    }, [value, insertBlock]);
    return <Text as="li" {...props} />;
    });

    const ListItem = createEditorComponent((props) => {
    const editor = useEditorState(props);
    useEditorEnter(() => {
  7. diegohaz revised this gist May 28, 2021. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,13 @@ type ListProps = {
    };

    const List = createEditorComponent<ListProps>((props) => {
    const editor = useEditorState(props);
    const editor = useEditorState({
    ...props,
    defaultValue: [{
    type: "ListItem",
    id: `${props.id}-1`,
    }],
    });
    const type = useEditorInput(
    ToggleGroup,
    {
    @@ -27,6 +33,7 @@ const List = createEditorComponent<ListProps>((props) => {
    );
    const component = type === "ordered" ? "ol" : "ul";

    // if defaultValue doesn't work
    useLayoutEffect(() => {
    if (!editor.value.length) {
    const selection = {
  8. diegohaz revised this gist May 28, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    const ListItem = createEditorComponent((props) => {
    const editor = useEditorState(props);
    useEditorEnter(() => {
    editor.insertElement("ListItem");
    }, [editor.insertElement]);
    return <Text as="li" {...props} />;
    });

  9. diegohaz revised this gist May 28, 2021. 1 changed file with 43 additions and 1 deletion.
    44 changes: 43 additions & 1 deletion Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,43 @@
    const ListItem = createEditorComponent((props) => {
    return <Text as="li" {...props} />;
    });

    type ListProps = {
    inputs: {
    type: "ordered" | "unordered";
    };
    };

    const List = createEditorComponent<ListProps>((props) => {
    const editor = useEditorState(props);
    const type = useEditorInput(
    ToggleGroup,
    {
    ...props,
    name: "type",
    options: [
    { value: "ordered", label: "Ordered", icon: "Ordered" },
    { value: "unordered", label: "Unordered", icon: "Unordered" },
    ],
    }
    );
    const component = type === "ordered" ? "ol" : "ul";

    useLayoutEffect(() => {
    if (!editor.value.length) {
    const selection = {
    startContainerId: props.id,
    startOffset: 0,
    endContainerId: props.id,
    endOffset: 0,
    };
    editor.insertElement("ListItem", null, selection);
    }
    }, [editor.insertElement, editor.value, props.id]);

    return <EditorComponent as={component} {...props} />;
    });

    type HeadingProps = TextProps & {
    inputs: {
    level?: number;
    @@ -8,7 +48,9 @@ const Heading = createEditorComponent<HeadingProps>((props) => {
    const level = useEditorInput(HeadingLevel, { ...props, name: "level", label: "Heading level" });
    const component = `h${level}`;

    return <Text {...props} as={component} />;
    useEditorReplace("Paragraph");

    return <Text as={component} {...props} />;
    });

    Heading.defaultProps = {
  10. diegohaz revised this gist May 28, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ const defaultComponents = {

    function App() {
    const editor = useEditorState({ defaultComponents });
    editor.getSelection(); // { containerId: "id", offset: 0, startContainerId: "id", startOffset: 0, endContainerId: "id", endOffset: 3 }
    editor.getSelection(); // { startContainerId: "id", startOffset: 0, endContainerId: "id", endOffset: 3, collapsed: false }
    editor.insertElement("Paragraph", null, editor.getSelection());
    editor.insertElement(Heading, { inputs: { level: 2 } });
    editor.replaceElement(Paragraph);
  11. diegohaz revised this gist May 28, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -39,6 +39,7 @@ const defaultComponents = {

    function App() {
    const editor = useEditorState({ defaultComponents });
    editor.getSelection(); // { containerId: "id", offset: 0, startContainerId: "id", startOffset: 0, endContainerId: "id", endOffset: 3 }
    editor.insertElement("Paragraph", null, editor.getSelection());
    editor.insertElement(Heading, { inputs: { level: 2 } });
    editor.replaceElement(Paragraph);
  12. diegohaz revised this gist May 28, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -39,5 +39,8 @@ const defaultComponents = {

    function App() {
    const editor = useEditorState({ defaultComponents });
    editor.insertElement("Paragraph", null, editor.getSelection());
    editor.insertElement(Heading, { inputs: { level: 2 } });
    editor.replaceElement(Paragraph);
    return <Editor state={editor} />;
    }
  13. diegohaz revised this gist May 28, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ Heading.triggers = {
    };

    const HeadingLevel = createEditorInput<HeadingProps>((props) => {
    const [value, setValue] = useEditorInputState(props);
    useEditorInputValue(props, "value");
    return <div />;
    });

  14. diegohaz revised this gist May 28, 2021. 1 changed file with 0 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,3 @@
    import {
    createEditorComponent,
    createEditorInput,
    useEditorInput,
    useEditorInputValue,
    } from "editor";
    import {
    Text,
    TextProps,
    } from "editor/components";

    type HeadingProps = TextProps & {
    inputs: {
    level?: number;
  15. diegohaz revised this gist May 28, 2021. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -37,6 +37,18 @@ Heading.triggers = {
    };

    const HeadingLevel = createEditorInput<HeadingProps>((props) => {
    useEditorInputValue(props, "");
    const [value, setValue] = useEditorInputState(props);
    return <div />;
    });

    const defaultComponents = {
    Paragraph,
    Bold,
    Heading,
    ...
    });
    };

    function App() {
    const editor = useEditorState({ defaultComponents });
    return <Editor state={editor} />;
    }
  16. diegohaz revised this gist May 28, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ type HeadingProps = TextProps & {
    };

    const Heading = createEditorComponent<HeadingProps>((props) => {
    const level = useEditorInput("HeadingLevel", { ...props, name: "level", label: "Heading level" });
    const level = useEditorInput(HeadingLevel, { ...props, name: "level", label: "Heading level" });
    const component = `h${level}`;

    return <Text {...props} as={component} />;
  17. diegohaz revised this gist May 28, 2021. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,10 @@ const Heading = createEditorComponent<HeadingProps>((props) => {
    return <Text {...props} as={component} />;
    });

    Heading.defaultInputs = {
    level: 1,
    Heading.defaultProps = {
    inputs: {
    level: 1,
    },
    };

    Heading.triggers = {
  18. diegohaz revised this gist May 28, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,10 @@ const Heading = createEditorComponent<HeadingProps>((props) => {
    return <Text {...props} as={component} />;
    });

    Heading.defaultInputs = {
    level: 1,
    };

    Heading.triggers = {
    "#{1,6}": (text) => ({
    inputs: {
  19. diegohaz revised this gist May 28, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ import {
    createEditorComponent,
    createEditorInput,
    useEditorInput,
    useEditorInputValue,
    } from "editor";
    import {
    Text,
    @@ -30,5 +31,6 @@ Heading.triggers = {
    };

    const HeadingLevel = createEditorInput<HeadingProps>((props) => {
    useEditorInputValue(props, "");
    ...
    });
  20. diegohaz revised this gist May 28, 2021. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    import {
    createEditorComponent,
    createEditorInput,
    useEditorInput,
    } from "editor";
    import {
    Text,
    @@ -14,7 +15,7 @@ type HeadingProps = TextProps & {
    };

    const Heading = createEditorComponent<HeadingProps>((props) => {
    const level = useHeadingLevelInput({ ...props, name: "level", label: "Heading level" });
    const level = useEditorInput("HeadingLevel", { ...props, name: "level", label: "Heading level" });
    const component = `h${level}`;

    return <Text {...props} as={component} />;
    @@ -28,6 +29,6 @@ Heading.triggers = {
    });
    };

    const useHeadingLevelInput = createEditorInput<HeadingProps>((props) => {
    useInputRender(() => <input />, []);
    const HeadingLevel = createEditorInput<HeadingProps>((props) => {
    ...
    });
  21. diegohaz revised this gist May 28, 2021. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    import {
    createEditorComponent,
    createEditorInput,
    useTrigger,
    } from "editor";
    import {
    Text,
    @@ -18,14 +17,16 @@ const Heading = createEditorComponent<HeadingProps>((props) => {
    const level = useHeadingLevelInput({ ...props, name: "level", label: "Heading level" });
    const component = `h${level}`;

    useTrigger("#{1,6}", (text) => ({
    return <Text {...props} as={component} />;
    });

    Heading.triggers = {
    "#{1,6}": (text) => ({
    inputs: {
    level: text.length,
    },
    }));

    return <Text {...props} as={component} />;
    });
    });
    };

    const useHeadingLevelInput = createEditorInput<HeadingProps>((props) => {
    useInputRender(() => <input />, []);
  22. diegohaz revised this gist May 28, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,11 @@ import {
    createEditorComponent,
    createEditorInput,
    useTrigger,
    } from "editor";
    import {
    Text,
    TextProps,
    } from "editor";
    } from "editor/components";

    type HeadingProps = TextProps & {
    inputs: {
  23. diegohaz revised this gist May 28, 2021. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions Editor.jsx
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    import {
    createEditorComponent,
    createEditorInput,
    useTrigger,
    Text,
    TextProps,
    } from "editor";
    @@ -14,6 +15,13 @@ type HeadingProps = TextProps & {
    const Heading = createEditorComponent<HeadingProps>((props) => {
    const level = useHeadingLevelInput({ ...props, name: "level", label: "Heading level" });
    const component = `h${level}`;

    useTrigger("#{1,6}", (text) => ({
    inputs: {
    level: text.length,
    },
    }));

    return <Text {...props} as={component} />;
    });

  24. diegohaz renamed this gist May 28, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  25. diegohaz revised this gist May 28, 2021. 1 changed file with 21 additions and 2 deletions.
    23 changes: 21 additions & 2 deletions Editor.tsx
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,22 @@
    import {
    createEditorComponent
    } from "editor";
    createEditorComponent,
    createEditorInput,
    Text,
    TextProps,
    } from "editor";

    type HeadingProps = TextProps & {
    inputs: {
    level?: number;
    };
    };

    const Heading = createEditorComponent<HeadingProps>((props) => {
    const level = useHeadingLevelInput({ ...props, name: "level", label: "Heading level" });
    const component = `h${level}`;
    return <Text {...props} as={component} />;
    });

    const useHeadingLevelInput = createEditorInput<HeadingProps>((props) => {
    useInputRender(() => <input />, []);
    });
  26. diegohaz created this gist May 28, 2021.
    3 changes: 3 additions & 0 deletions Editor.tsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    import {
    createEditorComponent
    } from "editor";