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.
type HeadingProps = TextProps & {
inputs: {
level?: number;
};
};
const Heading = createEditorComponent<HeadingProps>((props) => {
const level = useEditorInput(HeadingLevel, { ...props, name: "level", label: "Heading level" });
const component = `h${level}`;
return <Text {...props} as={component} />;
});
Heading.defaultProps = {
inputs: {
level: 1,
},
};
Heading.triggers = {
"#{1,6}": (text) => ({
inputs: {
level: text.length,
},
});
};
const HeadingLevel = createEditorInput<HeadingProps>((props) => {
useEditorInputValue(props, "value");
return <div />;
});
const defaultComponents = {
Paragraph,
Bold,
Heading,
...
};
function App() {
const editor = useEditorState({ defaultComponents });
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);
return <Editor state={editor} />;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment