From be8b7222ee6904442f89716a487b7079c7493442 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Fri, 6 Dec 2019 12:06:53 -0500 Subject: [PATCH] update walkthroughs --- docs/walkthroughs/01-installing-slate.md | 87 +++++++++++----- docs/walkthroughs/02-adding-event-handlers.md | 54 +++++++++- .../03-defining-custom-elements.md | 80 +++++++++++++-- .../04-applying-custom-formatting.md | 69 ++++++++++--- docs/walkthroughs/05-executing-commands.md | 81 +++++++++++++-- docs/walkthroughs/06-saving-to-a-database.md | 98 +++++++++++-------- 6 files changed, 370 insertions(+), 99 deletions(-) diff --git a/docs/walkthroughs/01-installing-slate.md b/docs/walkthroughs/01-installing-slate.md index 1c2957e47..ef455a840 100644 --- a/docs/walkthroughs/01-installing-slate.md +++ b/docs/walkthroughs/01-installing-slate.md @@ -16,8 +16,6 @@ _Note, if you'd rather use a pre-bundled version of Slate, you can `yarn add sla Once you've installed Slate, you'll need to import it. -Slate exposes a set of modules that you'll use to build your editor. The most important of which are the `Editor` class and the `` component. - ```js // Import the Slate editor factory. import { createEditor } from 'slate' @@ -47,19 +45,44 @@ const App = () => { Of course we haven't rendered anything, so you won't see any changes. -Next up is to render a `` context provider. - -The provider component keeps track of your Slate editor, its plugins, its default value, and any changes that occur. It **must** be rendered above any `` components. But it can also provide the editor state to other components like toolbars, menus, etc. using the `useSlate` hook. +Next we want to create state for `value` and `selection`: ```jsx const App = () => { const editor = useMemo(() => withReact(createEditor()), []) - // Render the Slate editor context. - return + + // Keep track of state for the value and selection of the editor. + const [value, setValue] = useState([]) + const [selection, setSelection] = useState(null) + return null } ``` -You can think of the `` component as provided an "un-controlled" editor context to every component underneath it. +Next up is to render a `` context provider. + +The provider component keeps track of your Slate editor, its plugins, its value, its selection, and any changes that occur. It **must** be rendered above any `` components. But it can also provide the editor state to other components like toolbars, menus, etc. using the `useSlate` hook. + +```jsx +const App = () => { + const editor = useMemo(() => withReact(createEditor()), []) + const [value, setValue] = useState([]) + const [selection, setSelection] = useState(null) + // Render the Slate context. + return ( + { + setValue(value) + setSelection(selection) + }} + /> + ) +} +``` + +You can think of the `` component as providing a "controlled" context to every component underneath it. This is a slightly different mental model than things like `` or `