<?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 Sruli Rapps on Medium]]></title>
        <description><![CDATA[Stories by Sruli Rapps on Medium]]></description>
        <link>https://medium.com/@srulirapps?source=rss-14ce501ecf42------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ZiaTgiTSFPv_jQZm.</url>
            <title>Stories by Sruli Rapps on Medium</title>
            <link>https://medium.com/@srulirapps?source=rss-14ce501ecf42------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 07:09:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@srulirapps/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[Paginating a DraftJS editor]]></title>
            <link>https://medium.com/@srulirapps/paginating-a-draftjs-editor-79b6e0f86596?source=rss-14ce501ecf42------2</link>
            <guid isPermaLink="false">https://medium.com/p/79b6e0f86596</guid>
            <category><![CDATA[draftjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[pagination]]></category>
            <dc:creator><![CDATA[Sruli Rapps]]></dc:creator>
            <pubDate>Tue, 09 Apr 2019 18:49:13 GMT</pubDate>
            <atom:updated>2019-04-10T11:08:22.276Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EMGjW3vnZnIMCA909uHqAQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/K3uOmmlQmOo?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Angelina Litvin</a> on <a href="https://unsplash.com/search/photos/notebook?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I’m working on a notebook application where users create notebooks and fill them with any amount of content. This means notebooks get large. But users are generally not interested in viewing the content at the beginning of the notebook. They want to build upon their most recent content. This requires a solution where just the most recent content is loaded, but with an allowance for prepending more content by scrolling up on the page.</p><h3>Solution</h3><p>I’ll start by showing the entire solution and then break it down piece by piece. Because <a href="https://github.com/facebook/draft-js/issues/898">this is not currently a use case that DraftJS supports out of the box</a>, we need to implement it ourselves.</p><p>I have a redux editorStateReducer that manages updates to the DraftJS EditorState. I’m using redux but the concept can be applied to other React implementations. Here is the part that prepends paginated content to the current ContentState of the editor. It’s a lot to swallow but I’ll go through each part and why it is necessary to achieve a wholesome editor state that performs in an expected way.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87a85834d7cc0eb213e8bd8f3ed6985a/href">https://medium.com/media/87a85834d7cc0eb213e8bd8f3ed6985a/href</a></iframe><h3>Piece by piece</h3><p>Let’s go through this code one step at a time.</p><h4>Step 1: Capture current editor state</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59a212705874af17c178234acf5125cc/href">https://medium.com/media/59a212705874af17c178234acf5125cc/href</a></iframe><p>We store the current block map, which is an ImmutableJS OrderedList of the DraftJS ContentBlocks currently loaded in the editor. We will be prepending the new content to this block map.</p><p>We then note the current selection state of the editor. This will be important later to make sure that the cursor remains at the same position it was at before the new content was loaded.</p><h4>Step 2: Convert fetched content into DraftJS ContentBlocks</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1d9fe57ac7cd4d11270a6a7fa7dd5bf/href">https://medium.com/media/c1d9fe57ac7cd4d11270a6a7fa7dd5bf/href</a></iframe><p>First we grab the content blocks that were fetched from the server and passed into the reducer via a redux action. blocks is an array of objects similar to this:</p><pre>[<br>  {        <br>    data: {},        <br>    depth: 0,        <br>    entityRanges: [],        <br>    inlineStyleRanges: [],        <br>    key: &#39;c7ppu&#39;,        <br>    text: &#39;Some text&#39;,        <br>    type: &#39;unstyled&#39;,        <br>  },<br>]</pre><p><em>A DraftJS </em><strong>EditorState</strong><em> contains a </em><strong>ContentState</strong><em> that manages all of the content of the editor as an ImmutableJS OrderedMap of DraftJS </em><strong>ContentBlocks</strong><em>.</em> We need to convert the retrieved content into DraftJS ContentBlocks so that we can merge it with the existing content.</p><p>We do this by using DraftJS’s convertFromRaw() to create a ContentState from the fetched content. We then get the ContentBlocks from this ContentState which we will merge in the next step with the existing ContentBlocks to create a ContentState containing the new and existing content.</p><h4>Step 3: Combine all content into one ContentState</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5439fa587f257e30622145aa9a904a9/href">https://medium.com/media/e5439fa587f257e30622145aa9a904a9/href</a></iframe><p>First we combine the new ContentBlocks with the existing ContentBlocks, placing the new content at the beginning. Next we create a new ContentState from the combined ContentBlocks. This ContentState will be applied to our EditorState in the next step.</p><h4>Step 4: Push a new EditorState while excluding it from the undo/redo stack</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfc8bedbd7ab0a50794643a1319f9742/href">https://medium.com/media/bfc8bedbd7ab0a50794643a1319f9742/href</a></iframe><p><em>This step is important. </em>We need to make sure that undoing changes (by typing Cmd + Z) will not remove the fetched blocks from the editor. That would be unexpected behavior which could lead to bugs where chunks of content are accidentally deleted from your editor.</p><p>We do this by first setting the allowUndo EditorState option to false before pushing the combined content to the EditorState. We then push our new combined ContentState to our EditorState. Once we have pushed the new state, we reinstate allowUndo so that our future changes will be undoable.</p><h4>Step 5: Maintain same SelectionState</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5db7e26d9c7a579be8f8be6f87761d3/href">https://medium.com/media/c5db7e26d9c7a579be8f8be6f87761d3/href</a></iframe><p>In the final step before returning our new EditorState, we make sure that the cursor remains in the same place in the editor. Back in step 1, we stored the current SelectionState. This maintains a reference to the ContentBlock where the cursor was before the new content was loaded. Once that new content is loaded we make sure that the cursor remains in the same place by forcing the editor selection to that same place.</p><h4>Step 6: Return combined EditorState</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70f7ea26b20f9291cc220cf437182260/href">https://medium.com/media/70f7ea26b20f9291cc220cf437182260/href</a></iframe><h3>Conclusion</h3><p>That was a lot. Partly due to ImmutableJS returning new objects for each change—objects that need to be stored in variables. And partially because this is something that has not been considered a standard use case for DraftJS—which is generally more focused on social media commenting and blog posts—so we had to build this ourselves.</p><p>We accounted for several details, like ensuring the integrity of the undo/redo functionality and preserving the editor’s selection state, details that ensure a smooth and expected experience. I hope this solution provides value for you and your project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79b6e0f86596" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gotcha with jQuery’s off() method]]></title>
            <link>https://medium.com/@srulirapps/gotcha-with-jquerys-off-method-c5ed36557c9b?source=rss-14ce501ecf42------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5ed36557c9b</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[events]]></category>
            <category><![CDATA[underscorejs]]></category>
            <dc:creator><![CDATA[Sruli Rapps]]></dc:creator>
            <pubDate>Thu, 07 Dec 2017 06:41:10 GMT</pubDate>
            <atom:updated>2017-12-10T03:23:18.260Z</atom:updated>
            <content:encoded><![CDATA[<p>So the other day I was working on a modal for displaying a video that is shown the first time a user visits a page, seen once, and then closed. And I was writing this modal in jQuery. You know, I love React but sometimes you gotta work with legacy code, right?</p><p>Now, I wanted to close the modal via the esc key. Here’s the gist of what that would look like:</p><pre>// Note: this example uses the Underscore.js bind() function</pre><pre>(function() {<br>  var Modal = {<br>    init: function() {<br>      $(document).on(&#39;keydown&#39;, _.bind(this.onKeyDown, this));<br>    },<br>    <br>    onKeyDown: function(e) {<br>      if (e.keyCode === 27) this.close();<br>    },<br>    <br>    close: function() {<br>      $(&#39;#modal&#39;).hide();<br>    }<br>  };<br>  <br>  Modal.init();<br>})();</pre><p>Alright, very nice. However, because this modal is intended to be closed after the video is viewed, never to be seen again, there is no need to be listening for the keydown event every time a key is pressed on the page once the modal has been closed. So, I checked out the <a href="http://api.jquery.com/off/">documentation for jQuery’s off() function</a> and I figured that I should be able to just add one line of code to the end of the close() method and remove the event listener like this:</p><pre>(function() {<br>  var Modal = {<br>    init: function() {<br>      $(document).on(&#39;keydown&#39;, _.bind(this.onKeyDown, this));<br>    },<br>    <br>    onKeyDown: function(e) {<br>      if (e.keyCode === 27) this.close();<br>    },<br>    <br>    close: function() {<br>      $(&#39;#modal&#39;).hide();<br>      $(document).off(&#39;keydown&#39;, _.bind(this.onKeyDown, this));<br>    }<br>  };<br>  <br>  Modal.init();<br>})();</pre><p>Right? I mean, if I added the event handler by binding onKeyDown() to the Modal object and passing it to the on() function, I should be able to remove it by doing the same thing with the off() function.</p><p>Or so I thought…</p><p>But, alas, that did not work 🤔</p><p>Okay, here’s the catch so listen closely: when removing an event listener, it is not enough to simply reference the method that you referenced when adding the event handler. It must be the <em>same</em> <em>exact </em>instance of the method.</p><p>In the initial approach, one instance of the onKeyDown() method was being passed to the on() function and a <em>separate</em> instance of the onKeyDown() function was being passed to the off() function. off() was attempting to remove a different instance of the onKeyDown() method from the page than the one that was passed into the on() function.</p><p>The solution?</p><pre>(function() {<br>  var Modal = {<br>    init: function() {<br>      $(document).on(&#39;keydown&#39;, ON_KEYDOWN);<br>    },<br>    <br>    close: function() {<br>      $(&#39;#modal&#39;).hide();<br>      $(document).off(&#39;keydown&#39;, ON_KEYDOWN);<br>    }<br>  };</pre><pre>  var ON_KEYDOWN = function(e) {<br>    if (e.keyCode === 27) Modal.close();<br>  };<br>  <br>  Modal.init();<br>})();</pre><p>Now isn’t that great? I moved the event handler function outside of the Modal object and stored it in a constant so that I can reference the <em>exact instance</em> of the function in both the on() and off() functions.</p><p>And it works 💥</p><p>So, there you have it. I hope this is helpful.</p><p>Cheers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5ed36557c9b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>