How to use the @typewriter/editor.getLine function in @typewriter/editor

To help you get started, we’ve selected a few @typewriter/editor examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github typewriter-editor / typewriter / packages / modules / src / input.ts View on Github external
onTab(new CustomEvent('shortcut', { detail: 'Shift+Tab' }));
          return true;
        }
        if (block && (force || block !== paper.blocks.getDefault() && !block.defaultFollows)) {
          editor.formatLine([ range[0], range[0] ], {}, SOURCE_USER);
          return true;
        }
      }

      if (range[0] + range[1] === 0) {
        const line = getLine(editor.contents, range[0]);
        if (flattenBlock(line, true)) return true;
      } else {
        // The "from" block needs to stay the same. The "to" block gets merged into it
        if (range[0] === range[1]) {
          const line = getLine(editor.contents, range[0]);
          if (range[0] === line.start && flattenBlock(line)) {
            return;
          }

          range[0]--;
        }
        editor.deleteText(range, SOURCE_USER);
      }
    }
github typewriter-editor / typewriter / packages / modules / src / input.ts View on Github external
function flattenBlock(line: Line, force?: boolean) {
        if (!range) return;
        const block = paper.blocks.findByAttributes(line.attributes) || paper.blocks.getDefault();
        if (block.indentable && line.attributes.indent) {
          onTab(new CustomEvent('shortcut', { detail: 'Shift+Tab' }));
          return true;
        }
        if (block && (force || block !== paper.blocks.getDefault() && !block.defaultFollows)) {
          editor.formatLine([ range[0], range[0] ], {}, SOURCE_USER);
          return true;
        }
      }

      if (range[0] + range[1] === 0) {
        const line = getLine(editor.contents, range[0]);
        if (flattenBlock(line, true)) return true;
      } else {
        // The "from" block needs to stay the same. The "to" block gets merged into it
        if (range[0] === range[1]) {
          const line = getLine(editor.contents, range[0]);
          if (range[0] === line.start && flattenBlock(line)) {
            return;
          }

          range[0]--;
        }
        editor.deleteText(range, SOURCE_USER);
      }
    }
github typewriter-editor / typewriter / packages / modules / src / input.ts View on Github external
function onEnter(event: KeyboardEvent) {
      if (event.defaultPrevented) return;
      event.preventDefault();
      const range = editor.getSelectedRange();
      if (!range) return;
      const isCollapsed = range[0] === range[1];

      const line = getLine(editor.contents, range[0]);
      let attributes = line.attributes;
      const block = paper.blocks.findByAttributes(attributes) || paper.blocks.getDefault();
      const isDefault = block === paper.blocks.getDefault();
      const length = line.end - line.start - 1;
      const atEnd = range[1] === line.end - 1;
      if (atEnd && !isDefault && block.defaultFollows) {
        attributes = {};
      } else if (typeof block.getNextLineAttributes === 'function') {
        attributes = block.getNextLineAttributes(attributes);
      }
      const activeFormats = editor.activeFormats;
      if (!length && !isDefault && !block.defaultFollows && isCollapsed) {
        editor.formatLine(range, {}, SOURCE_USER);
      } else {
        const selection: EditorRange = [ range[0] + 1, range[0] + 1 ];
        editor.insertText(range, '\n', attributes, SOURCE_USER, selection);

@typewriter/editor

The core editor for typewriter. Manages the data model and updates. Can be run headless.

MIT
Latest version published 4 years ago

Package Health Score

51 / 100
Full package analysis