UNPKG

@atlaskit/editor-plugin-code-block

Version:

Code block plugin for @atlaskit/editor-core

94 lines (92 loc) 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLineInfo = exports.getEndOfCurrentLine = exports.forEachLine = void 0; exports.getLinesFromSelection = getLinesFromSelection; exports.isSelectionEntirelyInsideCodeBlock = exports.isCursorInsideCodeBlock = exports.getStartOfCurrentLine = void 0; var _utils = require("../utils"); var isSelectionEntirelyInsideCodeBlock = exports.isSelectionEntirelyInsideCodeBlock = function isSelectionEntirelyInsideCodeBlock(state) { return state.selection.$from.sameParent(state.selection.$to) && state.selection.$from.parent.type === state.schema.nodes.codeBlock; }; var isCursorInsideCodeBlock = exports.isCursorInsideCodeBlock = function isCursorInsideCodeBlock(state) { return !!(0, _utils.getCursor)(state.selection) && isSelectionEntirelyInsideCodeBlock(state); }; var getStartOfCurrentLine = exports.getStartOfCurrentLine = function getStartOfCurrentLine(state) { var $from = state.selection.$from; if ($from.nodeBefore && $from.nodeBefore.isText) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion var prevNewLineIndex = $from.nodeBefore.text.lastIndexOf('\n'); return { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion text: $from.nodeBefore.text.substring(prevNewLineIndex + 1), pos: $from.start() + prevNewLineIndex + 1 }; } return { text: '', pos: $from.pos }; }; var getEndOfCurrentLine = exports.getEndOfCurrentLine = function getEndOfCurrentLine(state) { var $to = state.selection.$to; if ($to.nodeAfter && $to.nodeAfter.isText) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion var nextNewLineIndex = $to.nodeAfter.text.indexOf('\n'); return { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion text: $to.nodeAfter.text.substring(0, nextNewLineIndex >= 0 ? nextNewLineIndex : undefined), pos: nextNewLineIndex >= 0 ? $to.pos + nextNewLineIndex : $to.end() }; } return { text: '', pos: $to.pos }; }; function getLinesFromSelection(state) { var _getStartOfCurrentLin = getStartOfCurrentLine(state), start = _getStartOfCurrentLin.pos; var _getEndOfCurrentLine = getEndOfCurrentLine(state), end = _getEndOfCurrentLine.pos; var text = state.doc.textBetween(start, end); return { text: text, start: start, end: end }; } var forEachLine = exports.forEachLine = function forEachLine(text, callback) { var offset = 0; text.split('\n').forEach(function (line) { callback(line, offset); offset += line.length + 1; }); }; // Ignored via go/ees005 // eslint-disable-next-line require-unicode-regexp var SPACE = { token: ' ', size: 2, regex: /[^ ]/ }; // Ignored via go/ees005 // eslint-disable-next-line require-unicode-regexp var TAB = { token: '\t', size: 1, regex: /[^\t]/ }; var getLineInfo = exports.getLineInfo = function getLineInfo(line) { var indentToken = line.startsWith('\t') ? TAB : SPACE; var indentLength = line.search(indentToken.regex); var indentText = line.substring(0, indentLength >= 0 ? indentLength : line.length); return { indentToken: indentToken, indentText: indentText }; };