<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Brendan Carney on Medium]]></title>
        <description><![CDATA[Stories by Brendan Carney on Medium]]></description>
        <link>https://medium.com/@brendanrc?source=rss-3694cbb2b822------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Y_sg85UHbC2eH7et</url>
            <title>Stories by Brendan Carney on Medium</title>
            <link>https://medium.com/@brendanrc?source=rss-3694cbb2b822------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 15:44:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brendanrc/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making Keyboard Shortcuts in SlateJS Simple]]></title>
            <link>https://medium.com/@brendanrc/making-keyboard-shortcuts-in-slatejs-simple-9bfa1a10ed7c?source=rss-3694cbb2b822------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bfa1a10ed7c</guid>
            <category><![CDATA[rich-text-editor]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Brendan Carney]]></dc:creator>
            <pubDate>Mon, 18 Feb 2019 15:01:00 GMT</pubDate>
            <atom:updated>2019-03-10T12:54:36.725Z</atom:updated>
            <content:encoded><![CDATA[<p>Most good rich text editors have keyboard shortcuts. If you use SlateJS, you can implement your own without too much effort. Right now, a shortcut for making text bold might look something like this:</p><pre>const onKeyDown = (event, editor, next) =&gt; {<br>  if (event.key == &quot;b&quot; &amp;&amp; event.ctrlKey) {<br>    editor.toggleMark(&quot;bold&quot;)<br>  }<br>}</pre><p>Pretty simple, right? Let’s add a few more:</p><pre>const onKeyDown = (event, editor, next) =&gt; {<br>  if (event.key == &quot;b&quot; &amp;&amp; event.ctrlKey) {<br>    editor.toggleMark(&quot;bold&quot;)<br>  } else if (event.key == &quot;i&quot; &amp;&amp; event.ctrlKey) {<br>    editor.toggleMark(&quot;italic&quot;)<br>  } else if (event.key == &quot;u&quot; &amp;&amp; event.ctrlKey) {<br>    editor.toggleMark(&quot;underline&quot;)<br>  } else {<br>    next() // don&#39;t forget to call next if you don&#39;t handle it.<br>  }<br>}</pre><p>This still isn’t so bad, but you can see how it could grow and get harder to read. How can we make this simpler? Enter slate-keymap.</p><p>slate-keymap is a tiny plugin from <a href="https://convertkit.com/">ConvertKit</a> that makes adding keyboard shortcuts to your editor just a little bit simpler. Add it to your project with:</p><pre>yarn add @convertkit/slate-keymap</pre><p>Then, instead of an onKeyDown function like above, we use the plugin to create our shortcuts:</p><pre>import Keymap from &quot;@convertkit/slate-keymap&quot;</pre><pre>const plugins = [<br>  Keymap({<br>    &quot;mod+b&quot;: (event, editor) =&gt; editor.toggleMark(&quot;bold&quot;),<br>    &quot;mod+i&quot;: (event, editor) =&gt; editor.toggleMark(&quot;italic&quot;),<br>    &quot;mod+u&quot;: (event, editor) =&gt; editor.toggleMark(&quot;underline&quot;)<br>  })<br>]</pre><p>Because it uses the <a href="https://github.com/ianstormtaylor/is-hotkey">is-hotkey</a> package under the hood, a lot of the complications of keyboard shortcuts are handled for you.</p><p>Check it out here: <a href="https://convertkit-slate-plugins.netlify.com/plugins/slate-keymap">https://convertkit-slate-plugins.netlify.com/plugins/slate-keymap</a>.</p><p>We’re building more tools for Slate! You can check them all out at <a href="https://convertkit-slate-plugins.netlify.com/">https://convertkit-slate-plugins.netlify.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bfa1a10ed7c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multiple SlateJS Editors on the Same Page]]></title>
            <link>https://medium.com/@brendanrc/multiple-slatejs-editors-on-the-same-page-a065b6ebefe3?source=rss-3694cbb2b822------2</link>
            <guid isPermaLink="false">https://medium.com/p/a065b6ebefe3</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[slatejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Brendan Carney]]></dc:creator>
            <pubDate>Sun, 27 Jan 2019 15:48:05 GMT</pubDate>
            <atom:updated>2019-01-27T15:48:59.135Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*aQt_18GwqkfymnvtJuO3qw.png" /><figcaption>Multiple editors on the same page</figcaption></figure><p>With Slate, you can have as many editors as you want on one page. There are only a couple important rules to follow:</p><h4>Initialize the editors with different values</h4><p>When calling `Value.fromJSON`, Slate builds a value object and assigns each node a unique key. This key is rendered on the DOM, and is used to identify which nodes in the DOM correspond to which nodes in the Value. So once you’ve built your value, you should only use it in one of your editors.</p><pre>import { Value } from &quot;slate&quot;<br>// This value can only be used in one `Editor` component.<br>const value = Value.fromJSON(initialValue)</pre><pre>// If we want to use the same initial value, we can do this:<br>const otherValue = Value.fromJSON(initialValue)</pre><h4>Don’t call `KeyUtils.resetGenerator` multiple times</h4><p>If you don’t use this function, then no need to worry about this. If you do, remember that this could cause editor instances to end up with nodes that have the same keys. This is going to cause problems across multiple editors.</p><p>For a full working example of multiple editors, check out this CodeSandbox:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F4x6qz8mn10&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F4x6qz8mn10&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F4x6qz8mn10%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/52a9b00400e3a946f35c3c56d695ff8c/href">https://medium.com/media/52a9b00400e3a946f35c3c56d695ff8c/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a065b6ebefe3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SlateJS by Example: Using Marks]]></title>
            <link>https://medium.com/@brendanrc/slatejs-by-example-using-marks-41d91822e4e3?source=rss-3694cbb2b822------2</link>
            <guid isPermaLink="false">https://medium.com/p/41d91822e4e3</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[slate]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Brendan Carney]]></dc:creator>
            <pubDate>Mon, 21 Jan 2019 14:30:12 GMT</pubDate>
            <atom:updated>2019-01-21T14:30:12.825Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*WXy1OcWIT4dJEHlPphNt7g.png" /><figcaption>Example editor with support for bold, italic, and underline</figcaption></figure><p>This post jumps right into how to use <a href="https://docs.slatejs.org/guides/data-model#marks">marks</a> in <a href="https://docs.slatejs.org/">SlateJS</a>, the rich text editing framework. While it helps to have some understanding about Slate’s <a href="https://docs.slatejs.org/guides/data-model">data model</a>, this is meant to be aimed at beginners, with examples ready to be used in your own rich text editor. All examples are hosted on <a href="http://codesandbox.io/">CodeSandbox</a>.</p><h4>What is a mark?</h4><p>A mark is used to format or describe a string of characters in your editor. Examples include basic formatting, like bold, italics, and underline, but they can also be used to implement more complex features, like adding comments to the editor. The basic idea is that if it can be implemented by describing a range of the characters, but doesn’t affect the structure of the document, marks are a good choice.</p><h4>Rendering marks</h4><p>For this example, we’ll be creating an editor that support bold, italic, and<br>underline formatting. Let’s start out with a blank editor:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F0qor7onxy0&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F0qor7onxy0&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F0qor7onxy0%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b594451cb240557548fcfead03caf44/href">https://medium.com/media/9b594451cb240557548fcfead03caf44/href</a></iframe><p>I’ve set up the default value in the editor to include some text that is bold, italic and underline. But, as you’ll see, none of the text is actually formatted. That’s because Slate has no understanding of what bold, italic, or anything else, means. It’s a framework, and we get to tell it exactly how to behave. So let’s start with rendering. Slate provides a renderMark prop that allows us to do just as it’s named: render a mark. Here’s a renderMark function that renders bold, italic, and underline marks:</p><pre>const renderMark = (props, editor, next) =&gt; {<br>  const { children, mark, attributes } = props<br>  switch (mark.type) {<br>    case &#39;bold&#39;:<br>      return &lt;strong {...attributes}&gt;{children}&lt;/strong&gt;<br>    case &#39;italic&#39;:<br>      return &lt;em {...attributes}&gt;{children}&lt;/em&gt;<br>    case &#39;underline&#39;:<br>      return &lt;u {...attributes}&gt;{children}&lt;/u&gt;<br>    default:<br>      return next()<br>  }<br>}</pre><p>When slate calls our renderMark function, it provides three arguments: props, editor, and next. props is an object including the following:</p><ul><li>children: Everything below our mark in the tree. In this case, our text.</li><li>mark: The actual mark object. This can have additional data about our mark, but for now, we’re only concerned with the type.</li><li>attributes: An object with DOM attributes. You must spread these on the node that ultimately gets rendered. This is how Slate maps its value to the DOM.</li><li>editor is the instance of our editor. This will be useful in just a bit.</li></ul><p>next is a function that you call if your function doesn’t end up handling the<br>rendering. This allows other plugins, or internal slate plugins to take care of<br>it. More on that in a future post.</p><p>So now that we have our renderMark function, let’s see what our editor looks like:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fj7jk8j739v&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fj7jk8j739v&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fj7jk8j739v%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/a46c5508eeef07d8275244fa3021d663/href">https://medium.com/media/a46c5508eeef07d8275244fa3021d663/href</a></iframe><p>Now our editor knows how to render bold, italic, and underline! But, as a user, how can we actually make text bold, italic, or underline? That’s up to us too.</p><h4>Toggling marks</h4><p>Most rich text editors have some type of toolbar for selecting this, but they also support keyboard shortcuts. Let’s skip building UI for now and implement some standard keyboard shortcuts: ctrl-b for bold, ctrl-i for italic, and<br>ctrl-u for underline. For this, we’re going to be using Slate’s onKeyDown<br>prop, as well as a package made by the creator of Slate, is-hotkey. is-hotkeyhandles detecting of keyboard shortcuts for us, including knowing that on a Mac, we should use cmd-b instead of ctrl-b for bold. So let’s see how this looks in our onKeyDown function:</p><pre>const onKeyDown = (event, editor, next) =&gt; {<br>  // &quot;mod&quot; means &quot;cmd&quot; on a Mac, and &quot;ctrl&quot; everywhere else.<br>  if isHotKey(&quot;mod+b&quot;, event) {<br>    editor.toggleMark(&quot;bold&quot;)<br>  } else if isHotKey(&quot;mod+i&quot;, event) {<br>    editor.toggleMark(&quot;italic&quot;) }<br>  else if isHotKey(&quot;mod+u&quot;, event) {<br>    editor.toggleMark(&quot;underline&quot;)<br>  } else {<br>    next()<br>  }<br>}</pre><p>You’ll start to see these functions follow a similar pattern, with editor, and<br>next as the second and third argument. This time, we also get event as our<br>first argument, which is the actual browser event for the key press. We’ll use<br>this event to determine if the user pressed on of our short cuts, and if so,<br>call the toggleMark command on the editor, passing the name of the mark we<br>used earlier in renderMark. Now, our editor supports the keyboard shortcuts to toggle our marks! Try pressing ctrl-b or cmd-b in the editor below:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F18zkmwz54&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F18zkmwz54&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F18zkmwz54%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ea524b0276e666605b75bdfcbbb51aa/href">https://medium.com/media/2ea524b0276e666605b75bdfcbbb51aa/href</a></iframe><p>And that’s it! Try forking this CodeSandbox and adding support for a new mark: strikethrough. In a future post, we’ll go through how to add a toolbar to make these options toggle-able via buttons.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41d91822e4e3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>