question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to restrict the editor to a single line? I’m looking for an extension of input rather than textarea, essentially.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

20reactions
PullJoshcommented, Feb 24, 2020

This issue is old and closed, but I had the same problem and came up with a nice solution. I’m posting it here in case anybody else needs it. 😉

function withSingleLine(editor) {
  const { normalizeNode } = editor;

  editor.normalizeNode = ([node, path]) => {
    if (path.length === 0) {
      if (editor.children.length > 1) {
        Transforms.mergeNodes(editor);
      }
    }

    return normalizeNode([node, path]);
  };

  return editor;
}

Then I just use the withSingleLine plugin while creating the editor:

withSingleLine(withReact(createEditor()))
7reactions
lzearcommented, Mar 31, 2020

@PullJosh’s solution worked well for us. In addition, we wanted to enable automatic scrolling to the caret (as in regular HTML inputs). I’m posting our hacky solution here, in case anybody else needs it 😃

const [prevSelection, setPrevSelection] = useState({ anchor: { path: [0, 0], offset: 0 }, focus: { path: [0, 0], offset: 0 }});
useEffect(() => {
  if (!prevSelection || !ReactEditor.isFocused(editor)) return;
  const editorDOM = ReactEditor.toDOMNode(editor, editor);
  if (!editorDOM) return;
  const padding = 5;
  const editorRects = editorDOM.getClientRects();
  const caretTextDom = ReactEditor.toDOMRange(editor, prevSelection);
  const caretTextRects = caretTextDom.getClientRects();
  if (caretTextRects.length > 0 && editorRects.length > 0) {
    const caretX = caretTextRects[0].left - editorRects[0].left + editorDOM.scrollLeft;
    if (caretX - editorDOM.clientWidth + padding > editorDOM.scrollLeft) {
      editorDOM.scrollTo(caretX - editorDOM.clientWidth + padding, 0);
    } else if (caretX - padding < editorDOM.scrollLeft) {
      editorDOM.scrollTo(caretX - padding, 0);
    }
  }
}, [editor, prevSelection]);
return (
  <Slate
    onChange={v => {
      const { selection } = editor;
      if (selection) setPrevSelection(selection);
    }}
  >
    ...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Single-line diagram
In power engineering, a single-line diagram (SLD), also sometimes called one-line diagram, is a simplest symbolic representation of an electric power system.
Read more >
Single-line Definition & Meaning
single -line. adjective. : confining commercial activity to one line of goods. a single-line manufacturer. a single-line representative ...
Read more >
How to Make a Single Line Diagram
A single-line diagram (SLD) is a high-level schematic diagram showing how incoming power is distributed to equipment. A4.1.1 Single-Line (One- ...
Read more >
What is a Single-Line Diagram and What is It Used For?
A single-line diagram (also known as an SLD or one-line diagram) is a simplified representation of an electrical system.
Read more >
Single Line Phones
View a large selection of Single Line Phones including Single Line Corded Phones and Single Line Wireless Phones at Office Depot. Shop online...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found