UNPKG

@wordpress/block-library

Version:
1 lines • 4.97 kB
{"version":3,"file":"plain-text-edit.mjs","names":[],"sources":["../../src/utils/plain-text-edit.ts"],"sourcesContent":["import {\n\tcreate,\n\tinsert,\n\ttoHTMLString,\n\ttype RichTextFormatList,\n\ttype RichTextValue,\n} from '@wordpress/rich-text';\n\n/**\n * What a single-line field shows for the characters it cannot hold: the\n * newline of a line break, which an input drops from its value, and the\n * object replacement character that stands for an inline image, which\n * renders as a box.\n */\nconst FIELD_SYMBOLS: Record< string, string > = {\n\t'\\n': '↵',\n\t'': '🖼️',\n};\n\n/**\n * The text of a rich text value as a single-line field shows it, with the\n * field offset at which each character of the value starts. The emoji spans\n * several UTF-16 code units, so the two texts do not line up by index.\n *\n * @param value The rich text value.\n * @return The field text and, for each value index, its field offset. The\n * last entry is the length of the field text.\n */\nfunction toFieldText( value: RichTextValue ): {\n\ttext: string;\n\toffsets: number[];\n} {\n\tlet text = '';\n\tconst offsets: number[] = [];\n\t// Step by UTF-16 index, not code point, as the formats and the offsets\n\t// passed to insert do.\n\tfor ( let index = 0; index < value.text.length; index++ ) {\n\t\tconst character = value.text[ index ];\n\t\toffsets.push( text.length );\n\t\ttext += FIELD_SYMBOLS[ character ] ?? character;\n\t}\n\toffsets.push( text.length );\n\treturn { text, offsets };\n}\n\n/**\n * The text of a rich text attribute, for showing in a plain text field.\n *\n * @param html The attribute's value.\n * @return The text with the markup removed.\n */\nexport function getPlainText( html?: string ): string {\n\treturn html ? toFieldText( create( { html } ) ).text : '';\n}\n\n/**\n * Applies an edit made in a plain text field to a rich text attribute.\n *\n * Only the characters that changed are replaced, so the formatting, line\n * breaks and inline images in the rest of the text survive. Typed text takes\n * the format of the character before it, as it does on the canvas.\n *\n * @param html The attribute's value before the edit.\n * @param newText The field's text after the edit.\n * @return The attribute's value after the edit.\n */\nexport function applyPlainTextEdit(\n\thtml: string | undefined,\n\tnewText: string\n): string {\n\tconst value = create( { html: html ?? '' } );\n\tconst { text: oldText, offsets } = toFieldText( value );\n\tif ( oldText === newText ) {\n\t\treturn html ?? '';\n\t}\n\n\t// The edit is the span between the longest common prefix and suffix.\n\tconst max = Math.min( oldText.length, newText.length );\n\tlet prefix = 0;\n\twhile ( prefix < max && oldText[ prefix ] === newText[ prefix ] ) {\n\t\tprefix++;\n\t}\n\tlet suffix = 0;\n\twhile (\n\t\tsuffix < max - prefix &&\n\t\toldText[ oldText.length - 1 - suffix ] ===\n\t\t\tnewText[ newText.length - 1 - suffix ]\n\t) {\n\t\tsuffix++;\n\t}\n\n\t// Map the span back onto the value. A symbol the span cuts through\n\t// counts as changed, so the whole symbol is replaced.\n\tlet start = 0;\n\twhile ( offsets[ start + 1 ] <= prefix ) {\n\t\tstart++;\n\t}\n\tlet end = value.text.length;\n\twhile ( offsets[ end - 1 ] >= oldText.length - suffix ) {\n\t\tend--;\n\t}\n\n\tconst inserted = create( {\n\t\ttext: newText.slice(\n\t\t\toffsets[ start ],\n\t\t\tnewText.length - ( oldText.length - offsets[ end ] )\n\t\t),\n\t} );\n\tconst formats: RichTextFormatList | undefined =\n\t\tstart > 0 ? value.formats[ start - 1 ] : value.formats[ 0 ];\n\tif ( formats ) {\n\t\tinserted.formats = Array.from(\n\t\t\t{ length: inserted.text.length },\n\t\t\t() => formats\n\t\t);\n\t}\n\n\treturn toHTMLString( { value: insert( value, inserted, start, end ) } );\n}\n"],"mappings":";;;;;;;;AAcA,MAAM,gBAA0C;CAC/C,MAAM;CACN,KAAK;AACN;;;;;;;;;;AAWA,SAAS,YAAa,OAGpB;CACD,IAAI,OAAO;CACX,MAAM,UAAoB,CAAC;CAG3B,KAAM,IAAI,QAAQ,GAAG,QAAQ,MAAM,KAAK,QAAQ,SAAU;EACzD,MAAM,YAAY,MAAM,KAAM;EAC9B,QAAQ,KAAM,KAAK,MAAO;EAC1B,QAAQ,cAAe,cAAe;CACvC;CACA,QAAQ,KAAM,KAAK,MAAO;CAC1B,OAAO;EAAE;EAAM;CAAQ;AACxB;;;;;;;AAQA,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