UNPKG

@liveblocks/react-ui

Version:

A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.

83 lines (79 loc) 2.39 kB
'use strict'; var slate = require('slate'); var getCharacter = require('../utils/get-character.cjs'); var getMatchRange = require('../utils/get-match-range.cjs'); var isEmptyString = require('../utils/is-empty-string.cjs'); const formatters = [ { type: "mark", mark: "bold", character: "*" }, { type: "mark", mark: "italic", character: "_" }, { type: "mark", mark: "strikethrough", character: "~" }, { type: "mark", mark: "code", character: "`" } ]; const markFormattingCharacters = formatters.filter((formatter) => formatter.type === "mark").map((formatter) => formatter.character); function formatMark(editor, text, formatter) { if (text !== formatter.character) { return false; } const match = getMatchRange.getMatchRange(editor, editor.selection, [formatter.character]); if (!match || slate.Range.isCollapsed(match)) { return false; } const formattingCharacter = getCharacter.getCharacterBefore(editor, match); if (!formattingCharacter || formattingCharacter.text !== formatter.character) { return false; } const beforeCharacter = getCharacter.getCharacterBefore(editor, formattingCharacter.range); if (beforeCharacter && !markFormattingCharacters.includes(beforeCharacter.text) && !isEmptyString.isEmptyString(beforeCharacter.text)) { return false; } const matchText = slate.Editor.string(editor, match); if (matchText.trim() !== matchText) { return false; } slate.Transforms.select(editor, match); editor.addMark(formatter.mark, true); slate.Transforms.collapse(editor, { edge: "end" }); editor.removeMark(formatter.mark); slate.Transforms.delete(editor, { at: formattingCharacter.range }); return true; } function withAutoFormatting(editor) { const { insertText } = editor; editor.insertText = (text, options) => { if (!editor.selection || !slate.Range.isCollapsed(editor.selection)) { return insertText(text, options); } let shouldInsertText = true; for (const formatter of formatters) { if (formatter.type === "mark") { if (formatMark(editor, text, formatter)) { shouldInsertText = false; } } } if (shouldInsertText) { insertText(text, options); } }; return editor; } exports.withAutoFormatting = withAutoFormatting; //# sourceMappingURL=auto-formatting.cjs.map