fix: prevent asset conflicts between React and Grid.js versions

Add coexistence checks to all enqueue methods to prevent loading
both React and Grid.js assets simultaneously.

Changes:
- ReactAdmin.php: Only enqueue React assets when ?react=1
- Init.php: Skip Grid.js when React active on admin pages
- Form.php, Coupon.php, Access.php: Restore classic assets when ?react=0
- Customer.php, Product.php, License.php: Add coexistence checks

Now the toggle between Classic and React versions works correctly.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
dwindown
2026-04-18 17:02:14 +07:00
parent bd9cdac02e
commit e8fbfb14c1
74973 changed files with 6658406 additions and 71 deletions

View File

@@ -0,0 +1,87 @@
/**
* Internal dependencies
*/
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
function replace(array, index, value) {
array = array.slice();
array[index] = value;
return array;
}
/**
* Apply a format object to a Rich Text value from the given `startIndex` to the
* given `endIndex`. Indices are retrieved from the selection if none are
* provided.
*
* @param {RichTextValue} value Value to modify.
* @param {RichTextFormat} format Format to apply.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the format applied.
*/
export function applyFormat(value, format, startIndex = value.start, endIndex = value.end) {
const {
formats,
activeFormats
} = value;
const newFormats = formats.slice();
// The selection is collapsed.
if (startIndex === endIndex) {
const startFormat = newFormats[startIndex]?.find(({
type
}) => type === format.type);
// If the caret is at a format of the same type, expand start and end to
// the edges of the format. This is useful to apply new attributes.
if (startFormat) {
const index = newFormats[startIndex].indexOf(startFormat);
while (newFormats[startIndex] && newFormats[startIndex][index] === startFormat) {
newFormats[startIndex] = replace(newFormats[startIndex], index, format);
startIndex--;
}
endIndex++;
while (newFormats[endIndex] && newFormats[endIndex][index] === startFormat) {
newFormats[endIndex] = replace(newFormats[endIndex], index, format);
endIndex++;
}
}
} else {
// Determine the highest position the new format can be inserted at.
let position = +Infinity;
for (let index = startIndex; index < endIndex; index++) {
if (newFormats[index]) {
newFormats[index] = newFormats[index].filter(({
type
}) => type !== format.type);
const length = newFormats[index].length;
if (length < position) {
position = length;
}
} else {
newFormats[index] = [];
position = 0;
}
}
for (let index = startIndex; index < endIndex; index++) {
newFormats[index].splice(position, 0, format);
}
}
return normaliseFormats({
...value,
formats: newFormats,
// Always revise active formats. This serves as a placeholder for new
// inputs with the format so new input appears with the format applied,
// and ensures a format of the same type uses the latest values.
activeFormats: [...(activeFormats?.filter(({
type
}) => type !== format.type) || []), format]
});
}
//# sourceMappingURL=apply-format.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,42 @@
/**
* Internal dependencies
*/
import { toHTMLString } from '../../to-html-string';
import { isCollapsed } from '../../is-collapsed';
import { slice } from '../../slice';
import { getTextContent } from '../../get-text-content';
export default (props => element => {
function onCopy(event) {
const {
record
} = props.current;
const {
ownerDocument
} = element;
if (isCollapsed(record.current) || !element.contains(ownerDocument.activeElement)) {
return;
}
const selectedRecord = slice(record.current);
const plainText = getTextContent(selectedRecord);
const html = toHTMLString({
value: selectedRecord
});
event.clipboardData.setData('text/plain', plainText);
event.clipboardData.setData('text/html', html);
event.clipboardData.setData('rich-text', 'true');
event.preventDefault();
if (event.type === 'cut') {
ownerDocument.execCommand('delete');
}
}
const {
defaultView
} = element.ownerDocument;
defaultView.addEventListener('copy', onCopy);
defaultView.addEventListener('cut', onCopy);
return () => {
defaultView.removeEventListener('copy', onCopy);
defaultView.removeEventListener('cut', onCopy);
};
});
//# sourceMappingURL=copy-handler.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["toHTMLString","isCollapsed","slice","getTextContent","props","element","onCopy","event","record","current","ownerDocument","contains","activeElement","selectedRecord","plainText","html","value","clipboardData","setData","preventDefault","type","execCommand","defaultView","addEventListener","removeEventListener"],"sources":["@wordpress/rich-text/src/component/event-listeners/copy-handler.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { toHTMLString } from '../../to-html-string';\nimport { isCollapsed } from '../../is-collapsed';\nimport { slice } from '../../slice';\nimport { getTextContent } from '../../get-text-content';\n\nexport default ( props ) => ( element ) => {\n\tfunction onCopy( event ) {\n\t\tconst { record } = props.current;\n\t\tconst { ownerDocument } = element;\n\t\tif (\n\t\t\tisCollapsed( record.current ) ||\n\t\t\t! element.contains( ownerDocument.activeElement )\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst selectedRecord = slice( record.current );\n\t\tconst plainText = getTextContent( selectedRecord );\n\t\tconst html = toHTMLString( { value: selectedRecord } );\n\t\tevent.clipboardData.setData( 'text/plain', plainText );\n\t\tevent.clipboardData.setData( 'text/html', html );\n\t\tevent.clipboardData.setData( 'rich-text', 'true' );\n\t\tevent.preventDefault();\n\n\t\tif ( event.type === 'cut' ) {\n\t\t\townerDocument.execCommand( 'delete' );\n\t\t}\n\t}\n\n\tconst { defaultView } = element.ownerDocument;\n\n\tdefaultView.addEventListener( 'copy', onCopy );\n\tdefaultView.addEventListener( 'cut', onCopy );\n\treturn () => {\n\t\tdefaultView.removeEventListener( 'copy', onCopy );\n\t\tdefaultView.removeEventListener( 'cut', onCopy );\n\t};\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAY,QAAQ,sBAAsB;AACnD,SAASC,WAAW,QAAQ,oBAAoB;AAChD,SAASC,KAAK,QAAQ,aAAa;AACnC,SAASC,cAAc,QAAQ,wBAAwB;AAEvD,gBAAiBC,KAAK,IAAQC,OAAO,IAAM;EAC1C,SAASC,MAAMA,CAAEC,KAAK,EAAG;IACxB,MAAM;MAAEC;IAAO,CAAC,GAAGJ,KAAK,CAACK,OAAO;IAChC,MAAM;MAAEC;IAAc,CAAC,GAAGL,OAAO;IACjC,IACCJ,WAAW,CAAEO,MAAM,CAACC,OAAQ,CAAC,IAC7B,CAAEJ,OAAO,CAACM,QAAQ,CAAED,aAAa,CAACE,aAAc,CAAC,EAChD;MACD;IACD;IAEA,MAAMC,cAAc,GAAGX,KAAK,CAAEM,MAAM,CAACC,OAAQ,CAAC;IAC9C,MAAMK,SAAS,GAAGX,cAAc,CAAEU,cAAe,CAAC;IAClD,MAAME,IAAI,GAAGf,YAAY,CAAE;MAAEgB,KAAK,EAAEH;IAAe,CAAE,CAAC;IACtDN,KAAK,CAACU,aAAa,CAACC,OAAO,CAAE,YAAY,EAAEJ,SAAU,CAAC;IACtDP,KAAK,CAACU,aAAa,CAACC,OAAO,CAAE,WAAW,EAAEH,IAAK,CAAC;IAChDR,KAAK,CAACU,aAAa,CAACC,OAAO,CAAE,WAAW,EAAE,MAAO,CAAC;IAClDX,KAAK,CAACY,cAAc,CAAC,CAAC;IAEtB,IAAKZ,KAAK,CAACa,IAAI,KAAK,KAAK,EAAG;MAC3BV,aAAa,CAACW,WAAW,CAAE,QAAS,CAAC;IACtC;EACD;EAEA,MAAM;IAAEC;EAAY,CAAC,GAAGjB,OAAO,CAACK,aAAa;EAE7CY,WAAW,CAACC,gBAAgB,CAAE,MAAM,EAAEjB,MAAO,CAAC;EAC9CgB,WAAW,CAACC,gBAAgB,CAAE,KAAK,EAAEjB,MAAO,CAAC;EAC7C,OAAO,MAAM;IACZgB,WAAW,CAACE,mBAAmB,CAAE,MAAM,EAAElB,MAAO,CAAC;IACjDgB,WAAW,CAACE,mBAAmB,CAAE,KAAK,EAAElB,MAAO,CAAC;EACjD,CAAC;AACF,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { BACKSPACE, DELETE } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { remove } from '../../remove';
export default (props => element => {
function onKeyDown(event) {
const {
keyCode
} = event;
const {
createRecord,
handleChange
} = props.current;
if (event.defaultPrevented) {
return;
}
if (keyCode !== DELETE && keyCode !== BACKSPACE) {
return;
}
const currentValue = createRecord();
const {
start,
end,
text
} = currentValue;
// Always handle full content deletion ourselves.
if (start === 0 && end !== 0 && end === text.length) {
handleChange(remove(currentValue));
event.preventDefault();
}
}
element.addEventListener('keydown', onKeyDown);
return () => {
element.removeEventListener('keydown', onKeyDown);
};
});
//# sourceMappingURL=delete.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["BACKSPACE","DELETE","remove","props","element","onKeyDown","event","keyCode","createRecord","handleChange","current","defaultPrevented","currentValue","start","end","text","length","preventDefault","addEventListener","removeEventListener"],"sources":["@wordpress/rich-text/src/component/event-listeners/delete.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { BACKSPACE, DELETE } from '@wordpress/keycodes';\n\n/**\n * Internal dependencies\n */\nimport { remove } from '../../remove';\n\nexport default ( props ) => ( element ) => {\n\tfunction onKeyDown( event ) {\n\t\tconst { keyCode } = event;\n\t\tconst { createRecord, handleChange } = props.current;\n\n\t\tif ( event.defaultPrevented ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( keyCode !== DELETE && keyCode !== BACKSPACE ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst currentValue = createRecord();\n\t\tconst { start, end, text } = currentValue;\n\n\t\t// Always handle full content deletion ourselves.\n\t\tif ( start === 0 && end !== 0 && end === text.length ) {\n\t\t\thandleChange( remove( currentValue ) );\n\t\t\tevent.preventDefault();\n\t\t}\n\t}\n\n\telement.addEventListener( 'keydown', onKeyDown );\n\treturn () => {\n\t\telement.removeEventListener( 'keydown', onKeyDown );\n\t};\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAS,EAAEC,MAAM,QAAQ,qBAAqB;;AAEvD;AACA;AACA;AACA,SAASC,MAAM,QAAQ,cAAc;AAErC,gBAAiBC,KAAK,IAAQC,OAAO,IAAM;EAC1C,SAASC,SAASA,CAAEC,KAAK,EAAG;IAC3B,MAAM;MAAEC;IAAQ,CAAC,GAAGD,KAAK;IACzB,MAAM;MAAEE,YAAY;MAAEC;IAAa,CAAC,GAAGN,KAAK,CAACO,OAAO;IAEpD,IAAKJ,KAAK,CAACK,gBAAgB,EAAG;MAC7B;IACD;IAEA,IAAKJ,OAAO,KAAKN,MAAM,IAAIM,OAAO,KAAKP,SAAS,EAAG;MAClD;IACD;IAEA,MAAMY,YAAY,GAAGJ,YAAY,CAAC,CAAC;IACnC,MAAM;MAAEK,KAAK;MAAEC,GAAG;MAAEC;IAAK,CAAC,GAAGH,YAAY;;IAEzC;IACA,IAAKC,KAAK,KAAK,CAAC,IAAIC,GAAG,KAAK,CAAC,IAAIA,GAAG,KAAKC,IAAI,CAACC,MAAM,EAAG;MACtDP,YAAY,CAAEP,MAAM,CAAEU,YAAa,CAAE,CAAC;MACtCN,KAAK,CAACW,cAAc,CAAC,CAAC;IACvB;EACD;EAEAb,OAAO,CAACc,gBAAgB,CAAE,SAAS,EAAEb,SAAU,CAAC;EAChD,OAAO,MAAM;IACZD,OAAO,CAACe,mBAAmB,CAAE,SAAS,EAAEd,SAAU,CAAC;EACpD,CAAC;AACF,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,101 @@
/**
* WordPress dependencies
*/
import { LEFT, RIGHT } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { isCollapsed } from '../../is-collapsed';
const EMPTY_ACTIVE_FORMATS = [];
export default (props => element => {
function onKeyDown(event) {
const {
keyCode,
shiftKey,
altKey,
metaKey,
ctrlKey
} = event;
if (
// Only override left and right keys without modifiers pressed.
shiftKey || altKey || metaKey || ctrlKey || keyCode !== LEFT && keyCode !== RIGHT) {
return;
}
const {
record,
applyRecord,
forceRender
} = props.current;
const {
text,
formats,
start,
end,
activeFormats: currentActiveFormats = []
} = record.current;
const collapsed = isCollapsed(record.current);
const {
ownerDocument
} = element;
const {
defaultView
} = ownerDocument;
// To do: ideally, we should look at visual position instead.
const {
direction
} = defaultView.getComputedStyle(element);
const reverseKey = direction === 'rtl' ? RIGHT : LEFT;
const isReverse = event.keyCode === reverseKey;
// If the selection is collapsed and at the very start, do nothing if
// navigating backward.
// If the selection is collapsed and at the very end, do nothing if
// navigating forward.
if (collapsed && currentActiveFormats.length === 0) {
if (start === 0 && isReverse) {
return;
}
if (end === text.length && !isReverse) {
return;
}
}
// If the selection is not collapsed, let the browser handle collapsing
// the selection for now. Later we could expand this logic to set
// boundary positions if needed.
if (!collapsed) {
return;
}
const formatsBefore = formats[start - 1] || EMPTY_ACTIVE_FORMATS;
const formatsAfter = formats[start] || EMPTY_ACTIVE_FORMATS;
const destination = isReverse ? formatsBefore : formatsAfter;
const isIncreasing = currentActiveFormats.every((format, index) => format === destination[index]);
let newActiveFormatsLength = currentActiveFormats.length;
if (!isIncreasing) {
newActiveFormatsLength--;
} else if (newActiveFormatsLength < destination.length) {
newActiveFormatsLength++;
}
if (newActiveFormatsLength === currentActiveFormats.length) {
record.current._newActiveFormats = destination;
return;
}
event.preventDefault();
const origin = isReverse ? formatsAfter : formatsBefore;
const source = isIncreasing ? destination : origin;
const newActiveFormats = source.slice(0, newActiveFormatsLength);
const newValue = {
...record.current,
activeFormats: newActiveFormats
};
record.current = newValue;
applyRecord(newValue);
forceRender();
}
element.addEventListener('keydown', onKeyDown);
return () => {
element.removeEventListener('keydown', onKeyDown);
};
});
//# sourceMappingURL=format-boundaries.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,28 @@
/**
* WordPress dependencies
*/
import { useMemo, useRef } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';
/**
* Internal dependencies
*/
import copyHandler from './copy-handler';
import selectObject from './select-object';
import formatBoundaries from './format-boundaries';
import deleteHandler from './delete';
import inputAndSelection from './input-and-selection';
import selectionChangeCompat from './selection-change-compat';
const allEventListeners = [copyHandler, selectObject, formatBoundaries, deleteHandler, inputAndSelection, selectionChangeCompat];
export function useEventListeners(props) {
const propsRef = useRef(props);
propsRef.current = props;
const refEffects = useMemo(() => allEventListeners.map(refEffect => refEffect(propsRef)), [propsRef]);
return useRefEffect(element => {
const cleanups = refEffects.map(effect => effect(element));
return () => {
cleanups.forEach(cleanup => cleanup());
};
}, [refEffects]);
}
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["useMemo","useRef","useRefEffect","copyHandler","selectObject","formatBoundaries","deleteHandler","inputAndSelection","selectionChangeCompat","allEventListeners","useEventListeners","props","propsRef","current","refEffects","map","refEffect","element","cleanups","effect","forEach","cleanup"],"sources":["@wordpress/rich-text/src/component/event-listeners/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useMemo, useRef } from '@wordpress/element';\nimport { useRefEffect } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport copyHandler from './copy-handler';\nimport selectObject from './select-object';\nimport formatBoundaries from './format-boundaries';\nimport deleteHandler from './delete';\nimport inputAndSelection from './input-and-selection';\nimport selectionChangeCompat from './selection-change-compat';\n\nconst allEventListeners = [\n\tcopyHandler,\n\tselectObject,\n\tformatBoundaries,\n\tdeleteHandler,\n\tinputAndSelection,\n\tselectionChangeCompat,\n];\n\nexport function useEventListeners( props ) {\n\tconst propsRef = useRef( props );\n\tpropsRef.current = props;\n\tconst refEffects = useMemo(\n\t\t() => allEventListeners.map( ( refEffect ) => refEffect( propsRef ) ),\n\t\t[ propsRef ]\n\t);\n\n\treturn useRefEffect(\n\t\t( element ) => {\n\t\t\tconst cleanups = refEffects.map( ( effect ) => effect( element ) );\n\t\t\treturn () => {\n\t\t\t\tcleanups.forEach( ( cleanup ) => cleanup() );\n\t\t\t};\n\t\t},\n\t\t[ refEffects ]\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,OAAO,EAAEC,MAAM,QAAQ,oBAAoB;AACpD,SAASC,YAAY,QAAQ,oBAAoB;;AAEjD;AACA;AACA;AACA,OAAOC,WAAW,MAAM,gBAAgB;AACxC,OAAOC,YAAY,MAAM,iBAAiB;AAC1C,OAAOC,gBAAgB,MAAM,qBAAqB;AAClD,OAAOC,aAAa,MAAM,UAAU;AACpC,OAAOC,iBAAiB,MAAM,uBAAuB;AACrD,OAAOC,qBAAqB,MAAM,2BAA2B;AAE7D,MAAMC,iBAAiB,GAAG,CACzBN,WAAW,EACXC,YAAY,EACZC,gBAAgB,EAChBC,aAAa,EACbC,iBAAiB,EACjBC,qBAAqB,CACrB;AAED,OAAO,SAASE,iBAAiBA,CAAEC,KAAK,EAAG;EAC1C,MAAMC,QAAQ,GAAGX,MAAM,CAAEU,KAAM,CAAC;EAChCC,QAAQ,CAACC,OAAO,GAAGF,KAAK;EACxB,MAAMG,UAAU,GAAGd,OAAO,CACzB,MAAMS,iBAAiB,CAACM,GAAG,CAAIC,SAAS,IAAMA,SAAS,CAAEJ,QAAS,CAAE,CAAC,EACrE,CAAEA,QAAQ,CACX,CAAC;EAED,OAAOV,YAAY,CAChBe,OAAO,IAAM;IACd,MAAMC,QAAQ,GAAGJ,UAAU,CAACC,GAAG,CAAII,MAAM,IAAMA,MAAM,CAAEF,OAAQ,CAAE,CAAC;IAClE,OAAO,MAAM;MACZC,QAAQ,CAACE,OAAO,CAAIC,OAAO,IAAMA,OAAO,CAAC,CAAE,CAAC;IAC7C,CAAC;EACF,CAAC,EACD,CAAEP,UAAU,CACb,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,235 @@
/**
* Internal dependencies
*/
import { getActiveFormats } from '../../get-active-formats';
import { updateFormats } from '../../update-formats';
/**
* All inserting input types that would insert HTML into the DOM.
*
* @see https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes
*
* @type {Set}
*/
const INSERTION_INPUT_TYPES_TO_IGNORE = new Set(['insertParagraph', 'insertOrderedList', 'insertUnorderedList', 'insertHorizontalRule', 'insertLink']);
const EMPTY_ACTIVE_FORMATS = [];
const PLACEHOLDER_ATTR_NAME = 'data-rich-text-placeholder';
/**
* If the selection is set on the placeholder element, collapse the selection to
* the start (before the placeholder).
*
* @param {Window} defaultView
*/
function fixPlaceholderSelection(defaultView) {
const selection = defaultView.getSelection();
const {
anchorNode,
anchorOffset
} = selection;
if (anchorNode.nodeType !== anchorNode.ELEMENT_NODE) {
return;
}
const targetNode = anchorNode.childNodes[anchorOffset];
if (!targetNode || targetNode.nodeType !== targetNode.ELEMENT_NODE || !targetNode.hasAttribute(PLACEHOLDER_ATTR_NAME)) {
return;
}
selection.collapseToStart();
}
export default (props => element => {
const {
ownerDocument
} = element;
const {
defaultView
} = ownerDocument;
let isComposing = false;
function onInput(event) {
// Do not trigger a change if characters are being composed. Browsers
// will usually emit a final `input` event when the characters are
// composed. As of December 2019, Safari doesn't support
// nativeEvent.isComposing.
if (isComposing) {
return;
}
let inputType;
if (event) {
inputType = event.inputType;
}
const {
record,
applyRecord,
createRecord,
handleChange
} = props.current;
// The browser formatted something or tried to insert HTML. Overwrite
// it. It will be handled later by the format library if needed.
if (inputType && (inputType.indexOf('format') === 0 || INSERTION_INPUT_TYPES_TO_IGNORE.has(inputType))) {
applyRecord(record.current);
return;
}
const currentValue = createRecord();
const {
start,
activeFormats: oldActiveFormats = []
} = record.current;
// Update the formats between the last and new caret position.
const change = updateFormats({
value: currentValue,
start,
end: currentValue.start,
formats: oldActiveFormats
});
handleChange(change);
}
/**
* Syncs the selection to local state. A callback for the `selectionchange`
* event.
*/
function handleSelectionChange() {
const {
record,
applyRecord,
createRecord,
onSelectionChange
} = props.current;
// Check if the implementor disabled editing. `contentEditable` does
// disable input, but not text selection, so we must ignore selection
// changes.
if (element.contentEditable !== 'true') {
return;
}
// Ensure the active element is the rich text element.
if (ownerDocument.activeElement !== element) {
// If it is not, we can stop listening for selection changes. We
// resume listening when the element is focused.
ownerDocument.removeEventListener('selectionchange', handleSelectionChange);
return;
}
// In case of a keyboard event, ignore selection changes during
// composition.
if (isComposing) {
return;
}
const {
start,
end,
text
} = createRecord();
const oldRecord = record.current;
// Fallback mechanism for IE11, which doesn't support the input event.
// Any input results in a selection change.
if (text !== oldRecord.text) {
onInput();
return;
}
if (start === oldRecord.start && end === oldRecord.end) {
// Sometimes the browser may set the selection on the placeholder
// element, in which case the caret is not visible. We need to set
// the caret before the placeholder if that's the case.
if (oldRecord.text.length === 0 && start === 0) {
fixPlaceholderSelection(defaultView);
}
return;
}
const newValue = {
...oldRecord,
start,
end,
// _newActiveFormats may be set on arrow key navigation to control
// the right boundary position. If undefined, getActiveFormats will
// give the active formats according to the browser.
activeFormats: oldRecord._newActiveFormats,
_newActiveFormats: undefined
};
const newActiveFormats = getActiveFormats(newValue, EMPTY_ACTIVE_FORMATS);
// Update the value with the new active formats.
newValue.activeFormats = newActiveFormats;
// It is important that the internal value is updated first,
// otherwise the value will be wrong on render!
record.current = newValue;
applyRecord(newValue, {
domOnly: true
});
onSelectionChange(start, end);
}
function onCompositionStart() {
isComposing = true;
// Do not update the selection when characters are being composed as
// this rerenders the component and might destroy internal browser
// editing state.
ownerDocument.removeEventListener('selectionchange', handleSelectionChange);
// Remove the placeholder. Since the rich text value doesn't update
// during composition, the placeholder doesn't get removed. There's no
// need to re-add it, when the value is updated on compositionend it
// will be re-added when the value is empty.
element.querySelector(`[${PLACEHOLDER_ATTR_NAME}]`)?.remove();
}
function onCompositionEnd() {
isComposing = false;
// Ensure the value is up-to-date for browsers that don't emit a final
// input event after composition.
onInput({
inputType: 'insertText'
});
// Tracking selection changes can be resumed.
ownerDocument.addEventListener('selectionchange', handleSelectionChange);
}
function onFocus() {
const {
record,
isSelected,
onSelectionChange,
applyRecord
} = props.current;
// When the whole editor is editable, let writing flow handle
// selection.
if (element.parentElement.closest('[contenteditable="true"]')) {
return;
}
if (!isSelected) {
// We know for certain that on focus, the old selection is invalid.
// It will be recalculated on the next mouseup, keyup, or touchend
// event.
const index = undefined;
record.current = {
...record.current,
start: index,
end: index,
activeFormats: EMPTY_ACTIVE_FORMATS
};
} else {
applyRecord(record.current, {
domOnly: true
});
}
onSelectionChange(record.current.start, record.current.end);
// There is no selection change event when the element is focused, so
// we need to manually trigger it. The selection is also not available
// yet in this call stack.
window.queueMicrotask(handleSelectionChange);
ownerDocument.addEventListener('selectionchange', handleSelectionChange);
}
element.addEventListener('input', onInput);
element.addEventListener('compositionstart', onCompositionStart);
element.addEventListener('compositionend', onCompositionEnd);
element.addEventListener('focus', onFocus);
return () => {
element.removeEventListener('input', onInput);
element.removeEventListener('compositionstart', onCompositionStart);
element.removeEventListener('compositionend', onCompositionEnd);
element.removeEventListener('focus', onFocus);
};
});
//# sourceMappingURL=input-and-selection.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,46 @@
export default (() => element => {
function onClick(event) {
const {
target
} = event;
// If the child element has no text content, it must be an object.
if (target === element || target.textContent && target.isContentEditable) {
return;
}
const {
ownerDocument
} = target;
const {
defaultView
} = ownerDocument;
const selection = defaultView.getSelection();
// If it's already selected, do nothing and let default behavior happen.
// This means it's "click-through".
if (selection.containsNode(target)) {
return;
}
const range = ownerDocument.createRange();
// If the target is within a non editable element, select the non
// editable element.
const nodeToSelect = target.isContentEditable ? target : target.closest('[contenteditable]');
range.selectNode(nodeToSelect);
selection.removeAllRanges();
selection.addRange(range);
event.preventDefault();
}
function onFocusIn(event) {
// When there is incoming focus from a link, select the object.
if (event.relatedTarget && !element.contains(event.relatedTarget) && event.relatedTarget.tagName === 'A') {
onClick(event);
}
}
element.addEventListener('click', onClick);
element.addEventListener('focusin', onFocusIn);
return () => {
element.removeEventListener('click', onClick);
element.removeEventListener('focusin', onFocusIn);
};
});
//# sourceMappingURL=select-object.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["element","onClick","event","target","textContent","isContentEditable","ownerDocument","defaultView","selection","getSelection","containsNode","range","createRange","nodeToSelect","closest","selectNode","removeAllRanges","addRange","preventDefault","onFocusIn","relatedTarget","contains","tagName","addEventListener","removeEventListener"],"sources":["@wordpress/rich-text/src/component/event-listeners/select-object.js"],"sourcesContent":["export default () => ( element ) => {\n\tfunction onClick( event ) {\n\t\tconst { target } = event;\n\n\t\t// If the child element has no text content, it must be an object.\n\t\tif (\n\t\t\ttarget === element ||\n\t\t\t( target.textContent && target.isContentEditable )\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst { ownerDocument } = target;\n\t\tconst { defaultView } = ownerDocument;\n\t\tconst selection = defaultView.getSelection();\n\n\t\t// If it's already selected, do nothing and let default behavior happen.\n\t\t// This means it's \"click-through\".\n\t\tif ( selection.containsNode( target ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst range = ownerDocument.createRange();\n\t\t// If the target is within a non editable element, select the non\n\t\t// editable element.\n\t\tconst nodeToSelect = target.isContentEditable\n\t\t\t? target\n\t\t\t: target.closest( '[contenteditable]' );\n\n\t\trange.selectNode( nodeToSelect );\n\t\tselection.removeAllRanges();\n\t\tselection.addRange( range );\n\n\t\tevent.preventDefault();\n\t}\n\n\tfunction onFocusIn( event ) {\n\t\t// When there is incoming focus from a link, select the object.\n\t\tif (\n\t\t\tevent.relatedTarget &&\n\t\t\t! element.contains( event.relatedTarget ) &&\n\t\t\tevent.relatedTarget.tagName === 'A'\n\t\t) {\n\t\t\tonClick( event );\n\t\t}\n\t}\n\n\telement.addEventListener( 'click', onClick );\n\telement.addEventListener( 'focusin', onFocusIn );\n\treturn () => {\n\t\telement.removeEventListener( 'click', onClick );\n\t\telement.removeEventListener( 'focusin', onFocusIn );\n\t};\n};\n"],"mappings":"AAAA,gBAAe,MAAQA,OAAO,IAAM;EACnC,SAASC,OAAOA,CAAEC,KAAK,EAAG;IACzB,MAAM;MAAEC;IAAO,CAAC,GAAGD,KAAK;;IAExB;IACA,IACCC,MAAM,KAAKH,OAAO,IAChBG,MAAM,CAACC,WAAW,IAAID,MAAM,CAACE,iBAAmB,EACjD;MACD;IACD;IAEA,MAAM;MAAEC;IAAc,CAAC,GAAGH,MAAM;IAChC,MAAM;MAAEI;IAAY,CAAC,GAAGD,aAAa;IACrC,MAAME,SAAS,GAAGD,WAAW,CAACE,YAAY,CAAC,CAAC;;IAE5C;IACA;IACA,IAAKD,SAAS,CAACE,YAAY,CAAEP,MAAO,CAAC,EAAG;MACvC;IACD;IAEA,MAAMQ,KAAK,GAAGL,aAAa,CAACM,WAAW,CAAC,CAAC;IACzC;IACA;IACA,MAAMC,YAAY,GAAGV,MAAM,CAACE,iBAAiB,GAC1CF,MAAM,GACNA,MAAM,CAACW,OAAO,CAAE,mBAAoB,CAAC;IAExCH,KAAK,CAACI,UAAU,CAAEF,YAAa,CAAC;IAChCL,SAAS,CAACQ,eAAe,CAAC,CAAC;IAC3BR,SAAS,CAACS,QAAQ,CAAEN,KAAM,CAAC;IAE3BT,KAAK,CAACgB,cAAc,CAAC,CAAC;EACvB;EAEA,SAASC,SAASA,CAAEjB,KAAK,EAAG;IAC3B;IACA,IACCA,KAAK,CAACkB,aAAa,IACnB,CAAEpB,OAAO,CAACqB,QAAQ,CAAEnB,KAAK,CAACkB,aAAc,CAAC,IACzClB,KAAK,CAACkB,aAAa,CAACE,OAAO,KAAK,GAAG,EAClC;MACDrB,OAAO,CAAEC,KAAM,CAAC;IACjB;EACD;EAEAF,OAAO,CAACuB,gBAAgB,CAAE,OAAO,EAAEtB,OAAQ,CAAC;EAC5CD,OAAO,CAACuB,gBAAgB,CAAE,SAAS,EAAEJ,SAAU,CAAC;EAChD,OAAO,MAAM;IACZnB,OAAO,CAACwB,mBAAmB,CAAE,OAAO,EAAEvB,OAAQ,CAAC;IAC/CD,OAAO,CAACwB,mBAAmB,CAAE,SAAS,EAAEL,SAAU,CAAC;EACpD,CAAC;AACF,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,50 @@
/**
* Internal dependencies
*/
import { isRangeEqual } from '../../is-range-equal';
/**
* Sometimes some browsers are not firing a `selectionchange` event when
* changing the selection by mouse or keyboard. This hook makes sure that, if we
* detect no `selectionchange` or `input` event between the up and down events,
* we fire a `selectionchange` event.
*/
export default (() => element => {
const {
ownerDocument
} = element;
const {
defaultView
} = ownerDocument;
const selection = defaultView?.getSelection();
let range;
function getRange() {
return selection.rangeCount ? selection.getRangeAt(0) : null;
}
function onDown(event) {
const type = event.type === 'keydown' ? 'keyup' : 'pointerup';
function onCancel() {
ownerDocument.removeEventListener(type, onUp);
ownerDocument.removeEventListener('selectionchange', onCancel);
ownerDocument.removeEventListener('input', onCancel);
}
function onUp() {
onCancel();
if (isRangeEqual(range, getRange())) {
return;
}
ownerDocument.dispatchEvent(new Event('selectionchange'));
}
ownerDocument.addEventListener(type, onUp);
ownerDocument.addEventListener('selectionchange', onCancel);
ownerDocument.addEventListener('input', onCancel);
range = getRange();
}
element.addEventListener('pointerdown', onDown);
element.addEventListener('keydown', onDown);
return () => {
element.removeEventListener('pointerdown', onDown);
element.removeEventListener('keydown', onDown);
};
});
//# sourceMappingURL=selection-change-compat.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isRangeEqual","element","ownerDocument","defaultView","selection","getSelection","range","getRange","rangeCount","getRangeAt","onDown","event","type","onCancel","removeEventListener","onUp","dispatchEvent","Event","addEventListener"],"sources":["@wordpress/rich-text/src/component/event-listeners/selection-change-compat.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { isRangeEqual } from '../../is-range-equal';\n\n/**\n * Sometimes some browsers are not firing a `selectionchange` event when\n * changing the selection by mouse or keyboard. This hook makes sure that, if we\n * detect no `selectionchange` or `input` event between the up and down events,\n * we fire a `selectionchange` event.\n */\nexport default () => ( element ) => {\n\tconst { ownerDocument } = element;\n\tconst { defaultView } = ownerDocument;\n\tconst selection = defaultView?.getSelection();\n\n\tlet range;\n\n\tfunction getRange() {\n\t\treturn selection.rangeCount ? selection.getRangeAt( 0 ) : null;\n\t}\n\n\tfunction onDown( event ) {\n\t\tconst type = event.type === 'keydown' ? 'keyup' : 'pointerup';\n\n\t\tfunction onCancel() {\n\t\t\townerDocument.removeEventListener( type, onUp );\n\t\t\townerDocument.removeEventListener( 'selectionchange', onCancel );\n\t\t\townerDocument.removeEventListener( 'input', onCancel );\n\t\t}\n\n\t\tfunction onUp() {\n\t\t\tonCancel();\n\t\t\tif ( isRangeEqual( range, getRange() ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\townerDocument.dispatchEvent( new Event( 'selectionchange' ) );\n\t\t}\n\n\t\townerDocument.addEventListener( type, onUp );\n\t\townerDocument.addEventListener( 'selectionchange', onCancel );\n\t\townerDocument.addEventListener( 'input', onCancel );\n\n\t\trange = getRange();\n\t}\n\n\telement.addEventListener( 'pointerdown', onDown );\n\telement.addEventListener( 'keydown', onDown );\n\treturn () => {\n\t\telement.removeEventListener( 'pointerdown', onDown );\n\t\telement.removeEventListener( 'keydown', onDown );\n\t};\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAY,QAAQ,sBAAsB;;AAEnD;AACA;AACA;AACA;AACA;AACA;AACA,gBAAe,MAAQC,OAAO,IAAM;EACnC,MAAM;IAAEC;EAAc,CAAC,GAAGD,OAAO;EACjC,MAAM;IAAEE;EAAY,CAAC,GAAGD,aAAa;EACrC,MAAME,SAAS,GAAGD,WAAW,EAAEE,YAAY,CAAC,CAAC;EAE7C,IAAIC,KAAK;EAET,SAASC,QAAQA,CAAA,EAAG;IACnB,OAAOH,SAAS,CAACI,UAAU,GAAGJ,SAAS,CAACK,UAAU,CAAE,CAAE,CAAC,GAAG,IAAI;EAC/D;EAEA,SAASC,MAAMA,CAAEC,KAAK,EAAG;IACxB,MAAMC,IAAI,GAAGD,KAAK,CAACC,IAAI,KAAK,SAAS,GAAG,OAAO,GAAG,WAAW;IAE7D,SAASC,QAAQA,CAAA,EAAG;MACnBX,aAAa,CAACY,mBAAmB,CAAEF,IAAI,EAAEG,IAAK,CAAC;MAC/Cb,aAAa,CAACY,mBAAmB,CAAE,iBAAiB,EAAED,QAAS,CAAC;MAChEX,aAAa,CAACY,mBAAmB,CAAE,OAAO,EAAED,QAAS,CAAC;IACvD;IAEA,SAASE,IAAIA,CAAA,EAAG;MACfF,QAAQ,CAAC,CAAC;MACV,IAAKb,YAAY,CAAEM,KAAK,EAAEC,QAAQ,CAAC,CAAE,CAAC,EAAG;QACxC;MACD;MACAL,aAAa,CAACc,aAAa,CAAE,IAAIC,KAAK,CAAE,iBAAkB,CAAE,CAAC;IAC9D;IAEAf,aAAa,CAACgB,gBAAgB,CAAEN,IAAI,EAAEG,IAAK,CAAC;IAC5Cb,aAAa,CAACgB,gBAAgB,CAAE,iBAAiB,EAAEL,QAAS,CAAC;IAC7DX,aAAa,CAACgB,gBAAgB,CAAE,OAAO,EAAEL,QAAS,CAAC;IAEnDP,KAAK,GAAGC,QAAQ,CAAC,CAAC;EACnB;EAEAN,OAAO,CAACiB,gBAAgB,CAAE,aAAa,EAAER,MAAO,CAAC;EACjDT,OAAO,CAACiB,gBAAgB,CAAE,SAAS,EAAER,MAAO,CAAC;EAC7C,OAAO,MAAM;IACZT,OAAO,CAACa,mBAAmB,CAAE,aAAa,EAAEJ,MAAO,CAAC;IACpDT,OAAO,CAACa,mBAAmB,CAAE,SAAS,EAAEJ,MAAO,CAAC;EACjD,CAAC;AACF,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,199 @@
/**
* WordPress dependencies
*/
import { useRef, useLayoutEffect, useReducer } from '@wordpress/element';
import { useMergeRefs, useRefEffect } from '@wordpress/compose';
import { useRegistry } from '@wordpress/data';
/**
* Internal dependencies
*/
import { create, RichTextData } from '../create';
import { apply } from '../to-dom';
import { toHTMLString } from '../to-html-string';
import { useDefaultStyle } from './use-default-style';
import { useBoundaryStyle } from './use-boundary-style';
import { useEventListeners } from './event-listeners';
export function useRichText({
value = '',
selectionStart,
selectionEnd,
placeholder,
onSelectionChange,
preserveWhiteSpace,
onChange,
__unstableDisableFormats: disableFormats,
__unstableIsSelected: isSelected,
__unstableDependencies = [],
__unstableAfterParse,
__unstableBeforeSerialize,
__unstableAddInvisibleFormats
}) {
const registry = useRegistry();
const [, forceRender] = useReducer(() => ({}));
const ref = useRef();
function createRecord() {
const {
ownerDocument: {
defaultView
}
} = ref.current;
const selection = defaultView.getSelection();
const range = selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
return create({
element: ref.current,
range,
__unstableIsEditableTree: true
});
}
function applyRecord(newRecord, {
domOnly
} = {}) {
apply({
value: newRecord,
current: ref.current,
prepareEditableTree: __unstableAddInvisibleFormats,
__unstableDomOnly: domOnly,
placeholder
});
}
// Internal values are updated synchronously, unlike props and state.
const _value = useRef(value);
const record = useRef();
function setRecordFromProps() {
_value.current = value;
record.current = value;
if (!(value instanceof RichTextData)) {
record.current = value ? RichTextData.fromHTMLString(value, {
preserveWhiteSpace
}) : RichTextData.empty();
}
// To do: make rich text internally work with RichTextData.
record.current = {
text: record.current.text,
formats: record.current.formats,
replacements: record.current.replacements
};
if (disableFormats) {
record.current.formats = Array(value.length);
record.current.replacements = Array(value.length);
}
if (__unstableAfterParse) {
record.current.formats = __unstableAfterParse(record.current);
}
record.current.start = selectionStart;
record.current.end = selectionEnd;
}
const hadSelectionUpdate = useRef(false);
if (!record.current) {
hadSelectionUpdate.current = isSelected;
setRecordFromProps();
} else if (selectionStart !== record.current.start || selectionEnd !== record.current.end) {
hadSelectionUpdate.current = isSelected;
record.current = {
...record.current,
start: selectionStart,
end: selectionEnd,
activeFormats: undefined
};
}
/**
* Sync the value to global state. The node tree and selection will also be
* updated if differences are found.
*
* @param {Object} newRecord The record to sync and apply.
*/
function handleChange(newRecord) {
record.current = newRecord;
applyRecord(newRecord);
if (disableFormats) {
_value.current = newRecord.text;
} else {
const newFormats = __unstableBeforeSerialize ? __unstableBeforeSerialize(newRecord) : newRecord.formats;
newRecord = {
...newRecord,
formats: newFormats
};
if (typeof value === 'string') {
_value.current = toHTMLString({
value: newRecord,
preserveWhiteSpace
});
} else {
_value.current = new RichTextData(newRecord);
}
}
const {
start,
end,
formats,
text
} = record.current;
// Selection must be updated first, so it is recorded in history when
// the content change happens.
// We batch both calls to only attempt to rerender once.
registry.batch(() => {
onSelectionChange(start, end);
onChange(_value.current, {
__unstableFormats: formats,
__unstableText: text
});
});
forceRender();
}
function applyFromProps() {
setRecordFromProps();
applyRecord(record.current);
}
const didMount = useRef(false);
// Value updates must happen synchonously to avoid overwriting newer values.
useLayoutEffect(() => {
if (didMount.current && value !== _value.current) {
applyFromProps();
forceRender();
}
}, [value]);
// Value updates must happen synchonously to avoid overwriting newer values.
useLayoutEffect(() => {
if (!hadSelectionUpdate.current) {
return;
}
if (ref.current.ownerDocument.activeElement !== ref.current) {
ref.current.focus();
}
applyRecord(record.current);
hadSelectionUpdate.current = false;
}, [hadSelectionUpdate.current]);
const mergedRefs = useMergeRefs([ref, useDefaultStyle(), useBoundaryStyle({
record
}), useEventListeners({
record,
handleChange,
applyRecord,
createRecord,
isSelected,
onSelectionChange,
forceRender
}), useRefEffect(() => {
applyFromProps();
didMount.current = true;
}, [placeholder, ...__unstableDependencies])]);
return {
value: record.current,
// A function to get the most recent value so event handlers in
// useRichText implementations have access to it. For example when
// listening to input events, we internally update the state, but this
// state is not yet available to the input event handler because React
// may re-render asynchronously.
getValue: () => record.current,
onChange: handleChange,
ref: mergedRefs
};
}
export default function __experimentalRichText() {}
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,75 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import { getActiveFormat } from '../get-active-format';
/**
* @template T
* @typedef {import('@wordpress/element').RefObject<T>} RefObject<T>
*/
/** @typedef {import('../register-format-type').WPFormat} WPFormat */
/** @typedef {import('../types').RichTextValue} RichTextValue */
/**
* This hook, to be used in a format type's Edit component, returns the active
* element that is formatted, or the selection range if no format is active.
* The returned value is meant to be used for positioning UI, e.g. by passing it
* to the `Popover` component.
*
* @param {Object} $1 Named parameters.
* @param {RefObject<HTMLElement>} $1.ref React ref of the element
* containing the editable content.
* @param {RichTextValue} $1.value Value to check for selection.
* @param {WPFormat} $1.settings The format type's settings.
*
* @return {Element|Range} The active element or selection range.
*/
export function useAnchorRef({
ref,
value,
settings = {}
}) {
deprecated('`useAnchorRef` hook', {
since: '6.1',
alternative: '`useAnchor` hook'
});
const {
tagName,
className,
name
} = settings;
const activeFormat = name ? getActiveFormat(value, name) : undefined;
return useMemo(() => {
if (!ref.current) {
return;
}
const {
ownerDocument: {
defaultView
}
} = ref.current;
const selection = defaultView.getSelection();
if (!selection.rangeCount) {
return;
}
const range = selection.getRangeAt(0);
if (!activeFormat) {
return range;
}
let element = range.startContainer;
// If the caret is right before the element, select the next element.
element = element.nextElementSibling || element;
while (element.nodeType !== element.ELEMENT_NODE) {
element = element.parentNode;
}
return element.closest(tagName + (className ? '.' + className : ''));
}, [activeFormat, value.start, value.end, tagName, className]);
}
//# sourceMappingURL=use-anchor-ref.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["useMemo","deprecated","getActiveFormat","useAnchorRef","ref","value","settings","since","alternative","tagName","className","name","activeFormat","undefined","current","ownerDocument","defaultView","selection","getSelection","rangeCount","range","getRangeAt","element","startContainer","nextElementSibling","nodeType","ELEMENT_NODE","parentNode","closest","start","end"],"sources":["@wordpress/rich-text/src/component/use-anchor-ref.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useMemo } from '@wordpress/element';\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { getActiveFormat } from '../get-active-format';\n\n/**\n * @template T\n * @typedef {import('@wordpress/element').RefObject<T>} RefObject<T>\n */\n/** @typedef {import('../register-format-type').WPFormat} WPFormat */\n/** @typedef {import('../types').RichTextValue} RichTextValue */\n\n/**\n * This hook, to be used in a format type's Edit component, returns the active\n * element that is formatted, or the selection range if no format is active.\n * The returned value is meant to be used for positioning UI, e.g. by passing it\n * to the `Popover` component.\n *\n * @param {Object} $1 Named parameters.\n * @param {RefObject<HTMLElement>} $1.ref React ref of the element\n * containing the editable content.\n * @param {RichTextValue} $1.value Value to check for selection.\n * @param {WPFormat} $1.settings The format type's settings.\n *\n * @return {Element|Range} The active element or selection range.\n */\nexport function useAnchorRef( { ref, value, settings = {} } ) {\n\tdeprecated( '`useAnchorRef` hook', {\n\t\tsince: '6.1',\n\t\talternative: '`useAnchor` hook',\n\t} );\n\n\tconst { tagName, className, name } = settings;\n\tconst activeFormat = name ? getActiveFormat( value, name ) : undefined;\n\n\treturn useMemo( () => {\n\t\tif ( ! ref.current ) {\n\t\t\treturn;\n\t\t}\n\t\tconst {\n\t\t\townerDocument: { defaultView },\n\t\t} = ref.current;\n\t\tconst selection = defaultView.getSelection();\n\n\t\tif ( ! selection.rangeCount ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst range = selection.getRangeAt( 0 );\n\n\t\tif ( ! activeFormat ) {\n\t\t\treturn range;\n\t\t}\n\n\t\tlet element = range.startContainer;\n\n\t\t// If the caret is right before the element, select the next element.\n\t\telement = element.nextElementSibling || element;\n\n\t\twhile ( element.nodeType !== element.ELEMENT_NODE ) {\n\t\t\telement = element.parentNode;\n\t\t}\n\n\t\treturn element.closest(\n\t\t\ttagName + ( className ? '.' + className : '' )\n\t\t);\n\t}, [ activeFormat, value.start, value.end, tagName, className ] );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,OAAO,QAAQ,oBAAoB;AAC5C,OAAOC,UAAU,MAAM,uBAAuB;;AAE9C;AACA;AACA;AACA,SAASC,eAAe,QAAQ,sBAAsB;;AAEtD;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAAE;EAAEC,GAAG;EAAEC,KAAK;EAAEC,QAAQ,GAAG,CAAC;AAAE,CAAC,EAAG;EAC7DL,UAAU,CAAE,qBAAqB,EAAE;IAClCM,KAAK,EAAE,KAAK;IACZC,WAAW,EAAE;EACd,CAAE,CAAC;EAEH,MAAM;IAAEC,OAAO;IAAEC,SAAS;IAAEC;EAAK,CAAC,GAAGL,QAAQ;EAC7C,MAAMM,YAAY,GAAGD,IAAI,GAAGT,eAAe,CAAEG,KAAK,EAAEM,IAAK,CAAC,GAAGE,SAAS;EAEtE,OAAOb,OAAO,CAAE,MAAM;IACrB,IAAK,CAAEI,GAAG,CAACU,OAAO,EAAG;MACpB;IACD;IACA,MAAM;MACLC,aAAa,EAAE;QAAEC;MAAY;IAC9B,CAAC,GAAGZ,GAAG,CAACU,OAAO;IACf,MAAMG,SAAS,GAAGD,WAAW,CAACE,YAAY,CAAC,CAAC;IAE5C,IAAK,CAAED,SAAS,CAACE,UAAU,EAAG;MAC7B;IACD;IAEA,MAAMC,KAAK,GAAGH,SAAS,CAACI,UAAU,CAAE,CAAE,CAAC;IAEvC,IAAK,CAAET,YAAY,EAAG;MACrB,OAAOQ,KAAK;IACb;IAEA,IAAIE,OAAO,GAAGF,KAAK,CAACG,cAAc;;IAElC;IACAD,OAAO,GAAGA,OAAO,CAACE,kBAAkB,IAAIF,OAAO;IAE/C,OAAQA,OAAO,CAACG,QAAQ,KAAKH,OAAO,CAACI,YAAY,EAAG;MACnDJ,OAAO,GAAGA,OAAO,CAACK,UAAU;IAC7B;IAEA,OAAOL,OAAO,CAACM,OAAO,CACrBnB,OAAO,IAAKC,SAAS,GAAG,GAAG,GAAGA,SAAS,GAAG,EAAE,CAC7C,CAAC;EACF,CAAC,EAAE,CAAEE,YAAY,EAAEP,KAAK,CAACwB,KAAK,EAAExB,KAAK,CAACyB,GAAG,EAAErB,OAAO,EAAEC,SAAS,CAAG,CAAC;AAClE","ignoreList":[]}

View File

@@ -0,0 +1,184 @@
/**
* WordPress dependencies
*/
import { usePrevious } from '@wordpress/compose';
import { useState, useLayoutEffect } from '@wordpress/element';
/** @typedef {import('../register-format-type').WPFormat} WPFormat */
/** @typedef {import('../types').RichTextValue} RichTextValue */
/**
* Given a range and a format tag name and class name, returns the closest
* format element.
*
* @param {Range} range The Range to check.
* @param {HTMLElement} editableContentElement The editable wrapper.
* @param {string} tagName The tag name of the format element.
* @param {string} className The class name of the format element.
*
* @return {HTMLElement|undefined} The format element, if found.
*/
function getFormatElement(range, editableContentElement, tagName, className) {
let element = range.startContainer;
// Even if the active format is defined, the actualy DOM range's start
// container may be outside of the format's DOM element:
// `a‸<strong>b</strong>` (DOM) while visually it's `a<strong>‸b</strong>`.
// So at a given selection index, start with the deepest format DOM element.
if (element.nodeType === element.TEXT_NODE && range.startOffset === element.length && element.nextSibling) {
element = element.nextSibling;
while (element.firstChild) {
element = element.firstChild;
}
}
if (element.nodeType !== element.ELEMENT_NODE) {
element = element.parentElement;
}
if (!element) {
return;
}
if (element === editableContentElement) {
return;
}
if (!editableContentElement.contains(element)) {
return;
}
const selector = tagName + (className ? '.' + className : '');
// .closest( selector ), but with a boundary. Check if the element matches
// the selector. If it doesn't match, try the parent element if it's not the
// editable wrapper. We don't want to try to match ancestors of the editable
// wrapper, which is what .closest( selector ) would do. When the element is
// the editable wrapper (which is most likely the case because most text is
// unformatted), this never runs.
while (element !== editableContentElement) {
if (element.matches(selector)) {
return element;
}
element = element.parentElement;
}
}
/**
* @typedef {Object} VirtualAnchorElement
* @property {() => DOMRect} getBoundingClientRect A function returning a DOMRect
* @property {HTMLElement} contextElement The actual DOM element
*/
/**
* Creates a virtual anchor element for a range.
*
* @param {Range} range The range to create a virtual anchor element for.
* @param {HTMLElement} editableContentElement The editable wrapper.
*
* @return {VirtualAnchorElement} The virtual anchor element.
*/
function createVirtualAnchorElement(range, editableContentElement) {
return {
contextElement: editableContentElement,
getBoundingClientRect() {
return editableContentElement.contains(range.startContainer) ? range.getBoundingClientRect() : editableContentElement.getBoundingClientRect();
}
};
}
/**
* Get the anchor: a format element if there is a matching one based on the
* tagName and className or a range otherwise.
*
* @param {HTMLElement} editableContentElement The editable wrapper.
* @param {string} tagName The tag name of the format
* element.
* @param {string} className The class name of the format
* element.
*
* @return {HTMLElement|VirtualAnchorElement|undefined} The anchor.
*/
function getAnchor(editableContentElement, tagName, className) {
if (!editableContentElement) {
return;
}
const {
ownerDocument
} = editableContentElement;
const {
defaultView
} = ownerDocument;
const selection = defaultView.getSelection();
if (!selection) {
return;
}
if (!selection.rangeCount) {
return;
}
const range = selection.getRangeAt(0);
if (!range || !range.startContainer) {
return;
}
const formatElement = getFormatElement(range, editableContentElement, tagName, className);
if (formatElement) {
return formatElement;
}
return createVirtualAnchorElement(range, editableContentElement);
}
/**
* This hook, to be used in a format type's Edit component, returns the active
* element that is formatted, or a virtual element for the selection range if
* no format is active. The returned value is meant to be used for positioning
* UI, e.g. by passing it to the `Popover` component via the `anchor` prop.
*
* @param {Object} $1 Named parameters.
* @param {HTMLElement|null} $1.editableContentElement The element containing
* the editable content.
* @param {WPFormat=} $1.settings The format type's settings.
* @return {Element|VirtualAnchorElement|undefined|null} The active element or selection range.
*/
export function useAnchor({
editableContentElement,
settings = {}
}) {
const {
tagName,
className,
isActive
} = settings;
const [anchor, setAnchor] = useState(() => getAnchor(editableContentElement, tagName, className));
const wasActive = usePrevious(isActive);
useLayoutEffect(() => {
if (!editableContentElement) {
return;
}
function callback() {
setAnchor(getAnchor(editableContentElement, tagName, className));
}
function attach() {
ownerDocument.addEventListener('selectionchange', callback);
}
function detach() {
ownerDocument.removeEventListener('selectionchange', callback);
}
const {
ownerDocument
} = editableContentElement;
if (editableContentElement === ownerDocument.activeElement ||
// When a link is created, we need to attach the popover to the newly created anchor.
!wasActive && isActive ||
// Sometimes we're _removing_ an active anchor, such as the inline color popover.
// When we add the color, it switches from a virtual anchor to a `<mark>` element.
// When we _remove_ the color, it switches from a `<mark>` element to a virtual anchor.
wasActive && !isActive) {
setAnchor(getAnchor(editableContentElement, tagName, className));
attach();
}
editableContentElement.addEventListener('focusin', attach);
editableContentElement.addEventListener('focusout', detach);
return () => {
detach();
editableContentElement.removeEventListener('focusin', attach);
editableContentElement.removeEventListener('focusout', detach);
};
}, [editableContentElement, tagName, className, isActive, wasActive]);
return anchor;
}
//# sourceMappingURL=use-anchor.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,55 @@
/**
* WordPress dependencies
*/
import { useEffect, useRef } from '@wordpress/element';
/*
* Calculates and renders the format boundary style when the active formats
* change.
*/
export function useBoundaryStyle({
record
}) {
const ref = useRef();
const {
activeFormats = [],
replacements,
start
} = record.current;
const activeReplacement = replacements[start];
useEffect(() => {
// There's no need to recalculate the boundary styles if no formats are
// active, because no boundary styles will be visible.
if ((!activeFormats || !activeFormats.length) && !activeReplacement) {
return;
}
const boundarySelector = '*[data-rich-text-format-boundary]';
const element = ref.current.querySelector(boundarySelector);
if (!element) {
return;
}
const {
ownerDocument
} = element;
const {
defaultView
} = ownerDocument;
const computedStyle = defaultView.getComputedStyle(element);
const newColor = computedStyle.color.replace(')', ', 0.2)').replace('rgb', 'rgba');
const selector = `.rich-text:focus ${boundarySelector}`;
const rule = `background-color: ${newColor}`;
const style = `${selector} {${rule}}`;
const globalStyleId = 'rich-text-boundary-style';
let globalStyle = ownerDocument.getElementById(globalStyleId);
if (!globalStyle) {
globalStyle = ownerDocument.createElement('style');
globalStyle.id = globalStyleId;
ownerDocument.head.appendChild(globalStyle);
}
if (globalStyle.innerHTML !== style) {
globalStyle.innerHTML = style;
}
}, [activeFormats, activeReplacement]);
return ref;
}
//# sourceMappingURL=use-boundary-style.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["useEffect","useRef","useBoundaryStyle","record","ref","activeFormats","replacements","start","current","activeReplacement","length","boundarySelector","element","querySelector","ownerDocument","defaultView","computedStyle","getComputedStyle","newColor","color","replace","selector","rule","style","globalStyleId","globalStyle","getElementById","createElement","id","head","appendChild","innerHTML"],"sources":["@wordpress/rich-text/src/component/use-boundary-style.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\n\n/*\n * Calculates and renders the format boundary style when the active formats\n * change.\n */\nexport function useBoundaryStyle( { record } ) {\n\tconst ref = useRef();\n\tconst { activeFormats = [], replacements, start } = record.current;\n\tconst activeReplacement = replacements[ start ];\n\tuseEffect( () => {\n\t\t// There's no need to recalculate the boundary styles if no formats are\n\t\t// active, because no boundary styles will be visible.\n\t\tif (\n\t\t\t( ! activeFormats || ! activeFormats.length ) &&\n\t\t\t! activeReplacement\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst boundarySelector = '*[data-rich-text-format-boundary]';\n\t\tconst element = ref.current.querySelector( boundarySelector );\n\n\t\tif ( ! element ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst { ownerDocument } = element;\n\t\tconst { defaultView } = ownerDocument;\n\t\tconst computedStyle = defaultView.getComputedStyle( element );\n\t\tconst newColor = computedStyle.color\n\t\t\t.replace( ')', ', 0.2)' )\n\t\t\t.replace( 'rgb', 'rgba' );\n\t\tconst selector = `.rich-text:focus ${ boundarySelector }`;\n\t\tconst rule = `background-color: ${ newColor }`;\n\t\tconst style = `${ selector } {${ rule }}`;\n\t\tconst globalStyleId = 'rich-text-boundary-style';\n\n\t\tlet globalStyle = ownerDocument.getElementById( globalStyleId );\n\n\t\tif ( ! globalStyle ) {\n\t\t\tglobalStyle = ownerDocument.createElement( 'style' );\n\t\t\tglobalStyle.id = globalStyleId;\n\t\t\townerDocument.head.appendChild( globalStyle );\n\t\t}\n\n\t\tif ( globalStyle.innerHTML !== style ) {\n\t\t\tglobalStyle.innerHTML = style;\n\t\t}\n\t}, [ activeFormats, activeReplacement ] );\n\treturn ref;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAS,EAAEC,MAAM,QAAQ,oBAAoB;;AAEtD;AACA;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAAE;EAAEC;AAAO,CAAC,EAAG;EAC9C,MAAMC,GAAG,GAAGH,MAAM,CAAC,CAAC;EACpB,MAAM;IAAEI,aAAa,GAAG,EAAE;IAAEC,YAAY;IAAEC;EAAM,CAAC,GAAGJ,MAAM,CAACK,OAAO;EAClE,MAAMC,iBAAiB,GAAGH,YAAY,CAAEC,KAAK,CAAE;EAC/CP,SAAS,CAAE,MAAM;IAChB;IACA;IACA,IACC,CAAE,CAAEK,aAAa,IAAI,CAAEA,aAAa,CAACK,MAAM,KAC3C,CAAED,iBAAiB,EAClB;MACD;IACD;IAEA,MAAME,gBAAgB,GAAG,mCAAmC;IAC5D,MAAMC,OAAO,GAAGR,GAAG,CAACI,OAAO,CAACK,aAAa,CAAEF,gBAAiB,CAAC;IAE7D,IAAK,CAAEC,OAAO,EAAG;MAChB;IACD;IAEA,MAAM;MAAEE;IAAc,CAAC,GAAGF,OAAO;IACjC,MAAM;MAAEG;IAAY,CAAC,GAAGD,aAAa;IACrC,MAAME,aAAa,GAAGD,WAAW,CAACE,gBAAgB,CAAEL,OAAQ,CAAC;IAC7D,MAAMM,QAAQ,GAAGF,aAAa,CAACG,KAAK,CAClCC,OAAO,CAAE,GAAG,EAAE,QAAS,CAAC,CACxBA,OAAO,CAAE,KAAK,EAAE,MAAO,CAAC;IAC1B,MAAMC,QAAQ,GAAI,oBAAoBV,gBAAkB,EAAC;IACzD,MAAMW,IAAI,GAAI,qBAAqBJ,QAAU,EAAC;IAC9C,MAAMK,KAAK,GAAI,GAAGF,QAAU,KAAKC,IAAM,GAAE;IACzC,MAAME,aAAa,GAAG,0BAA0B;IAEhD,IAAIC,WAAW,GAAGX,aAAa,CAACY,cAAc,CAAEF,aAAc,CAAC;IAE/D,IAAK,CAAEC,WAAW,EAAG;MACpBA,WAAW,GAAGX,aAAa,CAACa,aAAa,CAAE,OAAQ,CAAC;MACpDF,WAAW,CAACG,EAAE,GAAGJ,aAAa;MAC9BV,aAAa,CAACe,IAAI,CAACC,WAAW,CAAEL,WAAY,CAAC;IAC9C;IAEA,IAAKA,WAAW,CAACM,SAAS,KAAKR,KAAK,EAAG;MACtCE,WAAW,CAACM,SAAS,GAAGR,KAAK;IAC9B;EACD,CAAC,EAAE,CAAElB,aAAa,EAAEI,iBAAiB,CAAG,CAAC;EACzC,OAAOL,GAAG;AACX","ignoreList":[]}

View File

@@ -0,0 +1,42 @@
/**
* WordPress dependencies
*/
import { useCallback } from '@wordpress/element';
/**
* In HTML, leading and trailing spaces are not visible, and multiple spaces
* elsewhere are visually reduced to one space. This rule prevents spaces from
* collapsing so all space is visible in the editor and can be removed. It also
* prevents some browsers from inserting non-breaking spaces at the end of a
* line to prevent the space from visually disappearing. Sometimes these non
* breaking spaces can linger in the editor causing unwanted non breaking spaces
* in between words. If also prevent Firefox from inserting a trailing `br` node
* to visualise any trailing space, causing the element to be saved.
*
* > Authors are encouraged to set the 'white-space' property on editing hosts
* > and on markup that was originally created through these editing mechanisms
* > to the value 'pre-wrap'. Default HTML whitespace handling is not well
* > suited to WYSIWYG editing, and line wrapping will not work correctly in
* > some corner cases if 'white-space' is left at its default value.
*
* https://html.spec.whatwg.org/multipage/interaction.html#best-practices-for-in-page-editors
*
* @type {string}
*/
const whiteSpace = 'pre-wrap';
/**
* A minimum width of 1px will prevent the rich text container from collapsing
* to 0 width and hiding the caret. This is useful for inline containers.
*/
const minWidth = '1px';
export function useDefaultStyle() {
return useCallback(element => {
if (!element) {
return;
}
element.style.whiteSpace = whiteSpace;
element.style.minWidth = minWidth;
}, []);
}
//# sourceMappingURL=use-default-style.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["useCallback","whiteSpace","minWidth","useDefaultStyle","element","style"],"sources":["@wordpress/rich-text/src/component/use-default-style.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback } from '@wordpress/element';\n\n/**\n * In HTML, leading and trailing spaces are not visible, and multiple spaces\n * elsewhere are visually reduced to one space. This rule prevents spaces from\n * collapsing so all space is visible in the editor and can be removed. It also\n * prevents some browsers from inserting non-breaking spaces at the end of a\n * line to prevent the space from visually disappearing. Sometimes these non\n * breaking spaces can linger in the editor causing unwanted non breaking spaces\n * in between words. If also prevent Firefox from inserting a trailing `br` node\n * to visualise any trailing space, causing the element to be saved.\n *\n * > Authors are encouraged to set the 'white-space' property on editing hosts\n * > and on markup that was originally created through these editing mechanisms\n * > to the value 'pre-wrap'. Default HTML whitespace handling is not well\n * > suited to WYSIWYG editing, and line wrapping will not work correctly in\n * > some corner cases if 'white-space' is left at its default value.\n *\n * https://html.spec.whatwg.org/multipage/interaction.html#best-practices-for-in-page-editors\n *\n * @type {string}\n */\nconst whiteSpace = 'pre-wrap';\n\n/**\n * A minimum width of 1px will prevent the rich text container from collapsing\n * to 0 width and hiding the caret. This is useful for inline containers.\n */\nconst minWidth = '1px';\n\nexport function useDefaultStyle() {\n\treturn useCallback( ( element ) => {\n\t\tif ( ! element ) {\n\t\t\treturn;\n\t\t}\n\t\telement.style.whiteSpace = whiteSpace;\n\t\telement.style.minWidth = minWidth;\n\t}, [] );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,WAAW,QAAQ,oBAAoB;;AAEhD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,UAAU,GAAG,UAAU;;AAE7B;AACA;AACA;AACA;AACA,MAAMC,QAAQ,GAAG,KAAK;AAEtB,OAAO,SAASC,eAAeA,CAAA,EAAG;EACjC,OAAOH,WAAW,CAAII,OAAO,IAAM;IAClC,IAAK,CAAEA,OAAO,EAAG;MAChB;IACD;IACAA,OAAO,CAACC,KAAK,CAACJ,UAAU,GAAGA,UAAU;IACrCG,OAAO,CAACC,KAAK,CAACH,QAAQ,GAAGA,QAAQ;EAClC,CAAC,EAAE,EAAG,CAAC;AACR","ignoreList":[]}

View File

@@ -0,0 +1,37 @@
/**
* Internal dependencies
*/
import { normaliseFormats } from './normalise-formats';
import { create } from './create';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Concats a pair of rich text values. Not that this mutates `a` and does NOT
* normalise formats!
*
* @param {Object} a Value to mutate.
* @param {Object} b Value to add read from.
*
* @return {Object} `a`, mutated.
*/
export function mergePair(a, b) {
a.formats = a.formats.concat(b.formats);
a.replacements = a.replacements.concat(b.replacements);
a.text += b.text;
return a;
}
/**
* Combine all Rich Text values into one. This is similar to
* `String.prototype.concat`.
*
* @param {...RichTextValue} values Objects to combine.
*
* @return {RichTextValue} A new value combining all given records.
*/
export function concat(...values) {
return normaliseFormats(values.reduce(mergePair, create()));
}
//# sourceMappingURL=concat.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["normaliseFormats","create","mergePair","a","b","formats","concat","replacements","text","values","reduce"],"sources":["@wordpress/rich-text/src/concat.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { normaliseFormats } from './normalise-formats';\nimport { create } from './create';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Concats a pair of rich text values. Not that this mutates `a` and does NOT\n * normalise formats!\n *\n * @param {Object} a Value to mutate.\n * @param {Object} b Value to add read from.\n *\n * @return {Object} `a`, mutated.\n */\nexport function mergePair( a, b ) {\n\ta.formats = a.formats.concat( b.formats );\n\ta.replacements = a.replacements.concat( b.replacements );\n\ta.text += b.text;\n\n\treturn a;\n}\n\n/**\n * Combine all Rich Text values into one. This is similar to\n * `String.prototype.concat`.\n *\n * @param {...RichTextValue} values Objects to combine.\n *\n * @return {RichTextValue} A new value combining all given records.\n */\nexport function concat( ...values ) {\n\treturn normaliseFormats( values.reduce( mergePair, create() ) );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,gBAAgB,QAAQ,qBAAqB;AACtD,SAASC,MAAM,QAAQ,UAAU;;AAEjC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,SAASA,CAAEC,CAAC,EAAEC,CAAC,EAAG;EACjCD,CAAC,CAACE,OAAO,GAAGF,CAAC,CAACE,OAAO,CAACC,MAAM,CAAEF,CAAC,CAACC,OAAQ,CAAC;EACzCF,CAAC,CAACI,YAAY,GAAGJ,CAAC,CAACI,YAAY,CAACD,MAAM,CAAEF,CAAC,CAACG,YAAa,CAAC;EACxDJ,CAAC,CAACK,IAAI,IAAIJ,CAAC,CAACI,IAAI;EAEhB,OAAOL,CAAC;AACT;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASG,MAAMA,CAAE,GAAGG,MAAM,EAAG;EACnC,OAAOT,gBAAgB,CAAES,MAAM,CAACC,MAAM,CAAER,SAAS,EAAED,MAAM,CAAC,CAAE,CAAE,CAAC;AAChE","ignoreList":[]}

View File

@@ -0,0 +1,26 @@
/**
* Parse the given HTML into a body element.
*
* Note: The current implementation will return a shared reference, reset on
* each call to `createElement`. Therefore, you should not hold a reference to
* the value to operate upon asynchronously, as it may have unexpected results.
*
* @param {HTMLDocument} document The HTML document to use to parse.
* @param {string} html The HTML to parse.
*
* @return {HTMLBodyElement} Body element with parsed HTML.
*/
export function createElement({
implementation
}, html) {
// Because `createHTMLDocument` is an expensive operation, and with this
// function being internal to `rich-text` (full control in avoiding a risk
// of asynchronous operations on the shared reference), a single document
// is reused and reset for each call to the function.
if (!createElement.body) {
createElement.body = implementation.createHTMLDocument('').body;
}
createElement.body.innerHTML = html;
return createElement.body;
}
//# sourceMappingURL=create-element.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["createElement","implementation","html","body","createHTMLDocument","innerHTML"],"sources":["@wordpress/rich-text/src/create-element.js"],"sourcesContent":["/**\n * Parse the given HTML into a body element.\n *\n * Note: The current implementation will return a shared reference, reset on\n * each call to `createElement`. Therefore, you should not hold a reference to\n * the value to operate upon asynchronously, as it may have unexpected results.\n *\n * @param {HTMLDocument} document The HTML document to use to parse.\n * @param {string} html The HTML to parse.\n *\n * @return {HTMLBodyElement} Body element with parsed HTML.\n */\nexport function createElement( { implementation }, html ) {\n\t// Because `createHTMLDocument` is an expensive operation, and with this\n\t// function being internal to `rich-text` (full control in avoiding a risk\n\t// of asynchronous operations on the shared reference), a single document\n\t// is reused and reset for each call to the function.\n\tif ( ! createElement.body ) {\n\t\tcreateElement.body = implementation.createHTMLDocument( '' ).body;\n\t}\n\n\tcreateElement.body.innerHTML = html;\n\n\treturn createElement.body;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,aAAaA,CAAE;EAAEC;AAAe,CAAC,EAAEC,IAAI,EAAG;EACzD;EACA;EACA;EACA;EACA,IAAK,CAAEF,aAAa,CAACG,IAAI,EAAG;IAC3BH,aAAa,CAACG,IAAI,GAAGF,cAAc,CAACG,kBAAkB,CAAE,EAAG,CAAC,CAACD,IAAI;EAClE;EAEAH,aAAa,CAACG,IAAI,CAACE,SAAS,GAAGH,IAAI;EAEnC,OAAOF,aAAa,CAACG,IAAI;AAC1B","ignoreList":[]}

View File

@@ -0,0 +1,585 @@
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
import { createElement } from './create-element';
import { mergePair } from './concat';
import { OBJECT_REPLACEMENT_CHARACTER, ZWNBSP } from './special-characters';
import { toHTMLString } from './to-html-string';
import { getTextContent } from './get-text-content';
/** @typedef {import('./types').RichTextValue} RichTextValue */
function createEmptyValue() {
return {
formats: [],
replacements: [],
text: ''
};
}
function toFormat({
tagName,
attributes
}) {
let formatType;
if (attributes && attributes.class) {
formatType = select(richTextStore).getFormatTypeForClassName(attributes.class);
if (formatType) {
// Preserve any additional classes.
attributes.class = ` ${attributes.class} `.replace(` ${formatType.className} `, ' ').trim();
if (!attributes.class) {
delete attributes.class;
}
}
}
if (!formatType) {
formatType = select(richTextStore).getFormatTypeForBareElement(tagName);
}
if (!formatType) {
return attributes ? {
type: tagName,
attributes
} : {
type: tagName
};
}
if (formatType.__experimentalCreatePrepareEditableTree && !formatType.__experimentalCreateOnChangeEditableValue) {
return null;
}
if (!attributes) {
return {
formatType,
type: formatType.name,
tagName
};
}
const registeredAttributes = {};
const unregisteredAttributes = {};
const _attributes = {
...attributes
};
for (const key in formatType.attributes) {
const name = formatType.attributes[key];
registeredAttributes[key] = _attributes[name];
// delete the attribute and what's left is considered
// to be unregistered.
delete _attributes[name];
if (typeof registeredAttributes[key] === 'undefined') {
delete registeredAttributes[key];
}
}
for (const name in _attributes) {
unregisteredAttributes[name] = attributes[name];
}
if (formatType.contentEditable === false) {
delete unregisteredAttributes.contenteditable;
}
return {
formatType,
type: formatType.name,
tagName,
attributes: registeredAttributes,
unregisteredAttributes
};
}
/**
* The RichTextData class is used to instantiate a wrapper around rich text
* values, with methods that can be used to transform or manipulate the data.
*
* - Create an empty instance: `new RichTextData()`.
* - Create one from an HTML string: `RichTextData.fromHTMLString(
* '<em>hello</em>' )`.
* - Create one from a wrapper HTMLElement: `RichTextData.fromHTMLElement(
* document.querySelector( 'p' ) )`.
* - Create one from plain text: `RichTextData.fromPlainText( '1\n2' )`.
* - Create one from a rich text value: `new RichTextData( { text: '...',
* formats: [ ... ] } )`.
*
* @todo Add methods to manipulate the data, such as applyFormat, slice etc.
*/
export class RichTextData {
#value;
static empty() {
return new RichTextData();
}
static fromPlainText(text) {
return new RichTextData(create({
text
}));
}
static fromHTMLString(html) {
return new RichTextData(create({
html
}));
}
static fromHTMLElement(htmlElement, options = {}) {
const {
preserveWhiteSpace = false
} = options;
const element = preserveWhiteSpace ? htmlElement : collapseWhiteSpace(htmlElement);
const richTextData = new RichTextData(create({
element
}));
Object.defineProperty(richTextData, 'originalHTML', {
value: htmlElement.innerHTML
});
return richTextData;
}
constructor(init = createEmptyValue()) {
this.#value = init;
}
toPlainText() {
return getTextContent(this.#value);
}
// We could expose `toHTMLElement` at some point as well, but we'd only use
// it internally.
toHTMLString({
preserveWhiteSpace
} = {}) {
return this.originalHTML || toHTMLString({
value: this.#value,
preserveWhiteSpace
});
}
valueOf() {
return this.toHTMLString();
}
toString() {
return this.toHTMLString();
}
toJSON() {
return this.toHTMLString();
}
get length() {
return this.text.length;
}
get formats() {
return this.#value.formats;
}
get replacements() {
return this.#value.replacements;
}
get text() {
return this.#value.text;
}
}
for (const name of Object.getOwnPropertyNames(String.prototype)) {
if (RichTextData.prototype.hasOwnProperty(name)) {
continue;
}
Object.defineProperty(RichTextData.prototype, name, {
value(...args) {
// Should we convert back to RichTextData?
return this.toHTMLString()[name](...args);
}
});
}
/**
* Create a RichText value from an `Element` tree (DOM), an HTML string or a
* plain text string, with optionally a `Range` object to set the selection. If
* called without any input, an empty value will be created. The optional
* functions can be used to filter out content.
*
* A value will have the following shape, which you are strongly encouraged not
* to modify without the use of helper functions:
*
* ```js
* {
* text: string,
* formats: Array,
* replacements: Array,
* ?start: number,
* ?end: number,
* }
* ```
*
* As you can see, text and formatting are separated. `text` holds the text,
* including any replacement characters for objects and lines. `formats`,
* `objects` and `lines` are all sparse arrays of the same length as `text`. It
* holds information about the formatting at the relevant text indices. Finally
* `start` and `end` state which text indices are selected. They are only
* provided if a `Range` was given.
*
* @param {Object} [$1] Optional named arguments.
* @param {Element} [$1.element] Element to create value from.
* @param {string} [$1.text] Text to create value from.
* @param {string} [$1.html] HTML to create value from.
* @param {Range} [$1.range] Range to create value from.
* @param {boolean} [$1.__unstableIsEditableTree]
* @return {RichTextValue} A rich text value.
*/
export function create({
element,
text,
html,
range,
__unstableIsEditableTree: isEditableTree
} = {}) {
if (html instanceof RichTextData) {
return {
text: html.text,
formats: html.formats,
replacements: html.replacements
};
}
if (typeof text === 'string' && text.length > 0) {
return {
formats: Array(text.length),
replacements: Array(text.length),
text
};
}
if (typeof html === 'string' && html.length > 0) {
// It does not matter which document this is, we're just using it to
// parse.
element = createElement(document, html);
}
if (typeof element !== 'object') {
return createEmptyValue();
}
return createFromElement({
element,
range,
isEditableTree
});
}
/**
* Helper to accumulate the value's selection start and end from the current
* node and range.
*
* @param {Object} accumulator Object to accumulate into.
* @param {Node} node Node to create value with.
* @param {Range} range Range to create value with.
* @param {Object} value Value that is being accumulated.
*/
function accumulateSelection(accumulator, node, range, value) {
if (!range) {
return;
}
const {
parentNode
} = node;
const {
startContainer,
startOffset,
endContainer,
endOffset
} = range;
const currentLength = accumulator.text.length;
// Selection can be extracted from value.
if (value.start !== undefined) {
accumulator.start = currentLength + value.start;
// Range indicates that the current node has selection.
} else if (node === startContainer && node.nodeType === node.TEXT_NODE) {
accumulator.start = currentLength + startOffset;
// Range indicates that the current node is selected.
} else if (parentNode === startContainer && node === startContainer.childNodes[startOffset]) {
accumulator.start = currentLength;
// Range indicates that the selection is after the current node.
} else if (parentNode === startContainer && node === startContainer.childNodes[startOffset - 1]) {
accumulator.start = currentLength + value.text.length;
// Fallback if no child inside handled the selection.
} else if (node === startContainer) {
accumulator.start = currentLength;
}
// Selection can be extracted from value.
if (value.end !== undefined) {
accumulator.end = currentLength + value.end;
// Range indicates that the current node has selection.
} else if (node === endContainer && node.nodeType === node.TEXT_NODE) {
accumulator.end = currentLength + endOffset;
// Range indicates that the current node is selected.
} else if (parentNode === endContainer && node === endContainer.childNodes[endOffset - 1]) {
accumulator.end = currentLength + value.text.length;
// Range indicates that the selection is before the current node.
} else if (parentNode === endContainer && node === endContainer.childNodes[endOffset]) {
accumulator.end = currentLength;
// Fallback if no child inside handled the selection.
} else if (node === endContainer) {
accumulator.end = currentLength + endOffset;
}
}
/**
* Adjusts the start and end offsets from a range based on a text filter.
*
* @param {Node} node Node of which the text should be filtered.
* @param {Range} range The range to filter.
* @param {Function} filter Function to use to filter the text.
*
* @return {Object|void} Object containing range properties.
*/
function filterRange(node, range, filter) {
if (!range) {
return;
}
const {
startContainer,
endContainer
} = range;
let {
startOffset,
endOffset
} = range;
if (node === startContainer) {
startOffset = filter(node.nodeValue.slice(0, startOffset)).length;
}
if (node === endContainer) {
endOffset = filter(node.nodeValue.slice(0, endOffset)).length;
}
return {
startContainer,
startOffset,
endContainer,
endOffset
};
}
/**
* Collapse any whitespace used for HTML formatting to one space character,
* because it will also be displayed as such by the browser.
*
* We need to strip it from the content because we use white-space: pre-wrap for
* displaying editable rich text. Without using white-space: pre-wrap, the
* browser will litter the content with non breaking spaces, among other issues.
* See packages/rich-text/src/component/use-default-style.js.
*
* @see
* https://developer.mozilla.org/en-US/docs/Web/CSS/white-space-collapse#collapsing_of_white_space
*
* @param {HTMLElement} element
* @param {boolean} isRoot
*
* @return {HTMLElement} New element with collapsed whitespace.
*/
function collapseWhiteSpace(element, isRoot = true) {
const clone = element.cloneNode(true);
clone.normalize();
Array.from(clone.childNodes).forEach((node, i, nodes) => {
if (node.nodeType === node.TEXT_NODE) {
let newNodeValue = node.nodeValue;
if (/[\n\t\r\f]/.test(newNodeValue)) {
newNodeValue = newNodeValue.replace(/[\n\t\r\f]+/g, ' ');
}
if (newNodeValue.indexOf(' ') !== -1) {
newNodeValue = newNodeValue.replace(/ {2,}/g, ' ');
}
if (i === 0 && newNodeValue.startsWith(' ')) {
newNodeValue = newNodeValue.slice(1);
} else if (isRoot && i === nodes.length - 1 && newNodeValue.endsWith(' ')) {
newNodeValue = newNodeValue.slice(0, -1);
}
node.nodeValue = newNodeValue;
} else if (node.nodeType === node.ELEMENT_NODE) {
collapseWhiteSpace(node, false);
}
});
return clone;
}
/**
* We need to normalise line breaks to `\n` so they are consistent across
* platforms and serialised properly. Not removing \r would cause it to
* linger and result in double line breaks when whitespace is preserved.
*/
const CARRIAGE_RETURN = '\r';
/**
* Removes reserved characters used by rich-text (zero width non breaking spaces
* added by `toTree` and object replacement characters).
*
* @param {string} string
*/
export function removeReservedCharacters(string) {
// with the global flag, note that we should create a new regex each time OR
// reset lastIndex state.
return string.replace(new RegExp(`[${ZWNBSP}${OBJECT_REPLACEMENT_CHARACTER}${CARRIAGE_RETURN}]`, 'gu'), '');
}
/**
* Creates a Rich Text value from a DOM element and range.
*
* @param {Object} $1 Named argements.
* @param {Element} [$1.element] Element to create value from.
* @param {Range} [$1.range] Range to create value from.
* @param {boolean} [$1.isEditableTree]
*
* @return {RichTextValue} A rich text value.
*/
function createFromElement({
element,
range,
isEditableTree
}) {
const accumulator = createEmptyValue();
if (!element) {
return accumulator;
}
if (!element.hasChildNodes()) {
accumulateSelection(accumulator, element, range, createEmptyValue());
return accumulator;
}
const length = element.childNodes.length;
// Optimise for speed.
for (let index = 0; index < length; index++) {
const node = element.childNodes[index];
const tagName = node.nodeName.toLowerCase();
if (node.nodeType === node.TEXT_NODE) {
const text = removeReservedCharacters(node.nodeValue);
range = filterRange(node, range, removeReservedCharacters);
accumulateSelection(accumulator, node, range, {
text
});
// Create a sparse array of the same length as `text`, in which
// formats can be added.
accumulator.formats.length += text.length;
accumulator.replacements.length += text.length;
accumulator.text += text;
continue;
}
if (node.nodeType !== node.ELEMENT_NODE) {
continue;
}
if (isEditableTree &&
// Ignore any line breaks that are not inserted by us.
tagName === 'br' && !node.getAttribute('data-rich-text-line-break')) {
accumulateSelection(accumulator, node, range, createEmptyValue());
continue;
}
if (tagName === 'script') {
const value = {
formats: [,],
replacements: [{
type: tagName,
attributes: {
'data-rich-text-script': node.getAttribute('data-rich-text-script') || encodeURIComponent(node.innerHTML)
}
}],
text: OBJECT_REPLACEMENT_CHARACTER
};
accumulateSelection(accumulator, node, range, value);
mergePair(accumulator, value);
continue;
}
if (tagName === 'br') {
accumulateSelection(accumulator, node, range, createEmptyValue());
mergePair(accumulator, create({
text: '\n'
}));
continue;
}
const format = toFormat({
tagName,
attributes: getAttributes({
element: node
})
});
// When a format type is declared as not editable, replace it with an
// object replacement character and preserve the inner HTML.
if (format?.formatType?.contentEditable === false) {
delete format.formatType;
accumulateSelection(accumulator, node, range, createEmptyValue());
mergePair(accumulator, {
formats: [,],
replacements: [{
...format,
innerHTML: node.innerHTML
}],
text: OBJECT_REPLACEMENT_CHARACTER
});
continue;
}
if (format) {
delete format.formatType;
}
const value = createFromElement({
element: node,
range,
isEditableTree
});
accumulateSelection(accumulator, node, range, value);
// Ignore any placeholders, but keep their content since the browser
// might insert text inside them when the editable element is flex.
if (!format || node.getAttribute('data-rich-text-placeholder')) {
mergePair(accumulator, value);
} else if (value.text.length === 0) {
if (format.attributes) {
mergePair(accumulator, {
formats: [,],
replacements: [format],
text: OBJECT_REPLACEMENT_CHARACTER
});
}
} else {
// Indices should share a reference to the same formats array.
// Only create a new reference if `formats` changes.
function mergeFormats(formats) {
if (mergeFormats.formats === formats) {
return mergeFormats.newFormats;
}
const newFormats = formats ? [format, ...formats] : [format];
mergeFormats.formats = formats;
mergeFormats.newFormats = newFormats;
return newFormats;
}
// Since the formats parameter can be `undefined`, preset
// `mergeFormats` with a new reference.
mergeFormats.newFormats = [format];
mergePair(accumulator, {
...value,
formats: Array.from(value.formats, mergeFormats)
});
}
}
return accumulator;
}
/**
* Gets the attributes of an element in object shape.
*
* @param {Object} $1 Named argements.
* @param {Element} $1.element Element to get attributes from.
*
* @return {Object|void} Attribute object or `undefined` if the element has no
* attributes.
*/
function getAttributes({
element
}) {
if (!element.hasAttributes()) {
return;
}
const length = element.attributes.length;
let accumulator;
// Optimise for speed.
for (let i = 0; i < length; i++) {
const {
name,
value
} = element.attributes[i];
if (name.indexOf('data-rich-text-') === 0) {
continue;
}
const safeName = /^on/i.test(name) ? 'data-disable-rich-text-' + name : name;
accumulator = accumulator || {};
accumulator[safeName] = value;
}
return accumulator;
}
//# sourceMappingURL=create.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,26 @@
/**
* Internal dependencies
*/
import { getActiveFormats } from './get-active-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Gets the format object by type at the start of the selection. This can be
* used to get e.g. the URL of a link format at the current selection, but also
* to check if a format is active at the selection. Returns undefined if there
* is no format at the selection.
*
* @param {RichTextValue} value Value to inspect.
* @param {string} formatType Format type to look for.
*
* @return {RichTextFormat|undefined} Active format object of the specified
* type, or undefined.
*/
export function getActiveFormat(value, formatType) {
return getActiveFormats(value).find(({
type
}) => type === formatType);
}
//# sourceMappingURL=get-active-format.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["getActiveFormats","getActiveFormat","value","formatType","find","type"],"sources":["@wordpress/rich-text/src/get-active-format.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { getActiveFormats } from './get-active-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Gets the format object by type at the start of the selection. This can be\n * used to get e.g. the URL of a link format at the current selection, but also\n * to check if a format is active at the selection. Returns undefined if there\n * is no format at the selection.\n *\n * @param {RichTextValue} value Value to inspect.\n * @param {string} formatType Format type to look for.\n *\n * @return {RichTextFormat|undefined} Active format object of the specified\n * type, or undefined.\n */\nexport function getActiveFormat( value, formatType ) {\n\treturn getActiveFormats( value ).find(\n\t\t( { type } ) => type === formatType\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,gBAAgB,QAAQ,sBAAsB;;AAEvD;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAAEC,KAAK,EAAEC,UAAU,EAAG;EACpD,OAAOH,gBAAgB,CAAEE,KAAM,CAAC,CAACE,IAAI,CACpC,CAAE;IAAEC;EAAK,CAAC,KAAMA,IAAI,KAAKF,UAC1B,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,83 @@
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormatList} RichTextFormatList */
/**
* Internal dependencies
*/
import { isFormatEqual } from './is-format-equal';
/**
* Gets the all format objects at the start of the selection.
*
* @param {RichTextValue} value Value to inspect.
* @param {Array} EMPTY_ACTIVE_FORMATS Array to return if there are no
* active formats.
*
* @return {RichTextFormatList} Active format objects.
*/
export function getActiveFormats(value, EMPTY_ACTIVE_FORMATS = []) {
const {
formats,
start,
end,
activeFormats
} = value;
if (start === undefined) {
return EMPTY_ACTIVE_FORMATS;
}
if (start === end) {
// For a collapsed caret, it is possible to override the active formats.
if (activeFormats) {
return activeFormats;
}
const formatsBefore = formats[start - 1] || EMPTY_ACTIVE_FORMATS;
const formatsAfter = formats[start] || EMPTY_ACTIVE_FORMATS;
// By default, select the lowest amount of formats possible (which means
// the caret is positioned outside the format boundary). The user can
// then use arrow keys to define `activeFormats`.
if (formatsBefore.length < formatsAfter.length) {
return formatsBefore;
}
return formatsAfter;
}
// If there's no formats at the start index, there are not active formats.
if (!formats[start]) {
return EMPTY_ACTIVE_FORMATS;
}
const selectedFormats = formats.slice(start, end);
// Clone the formats so we're not mutating the live value.
const _activeFormats = [...selectedFormats[0]];
let i = selectedFormats.length;
// For performance reasons, start from the end where it's much quicker to
// realise that there are no active formats.
while (i--) {
const formatsAtIndex = selectedFormats[i];
// If we run into any index without formats, we're sure that there's no
// active formats.
if (!formatsAtIndex) {
return EMPTY_ACTIVE_FORMATS;
}
let ii = _activeFormats.length;
// Loop over the active formats and remove any that are not present at
// the current index.
while (ii--) {
const format = _activeFormats[ii];
if (!formatsAtIndex.find(_format => isFormatEqual(format, _format))) {
_activeFormats.splice(ii, 1);
}
}
// If there are no active formats, we can stop.
if (_activeFormats.length === 0) {
return EMPTY_ACTIVE_FORMATS;
}
}
return _activeFormats || EMPTY_ACTIVE_FORMATS;
}
//# sourceMappingURL=get-active-formats.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isFormatEqual","getActiveFormats","value","EMPTY_ACTIVE_FORMATS","formats","start","end","activeFormats","undefined","formatsBefore","formatsAfter","length","selectedFormats","slice","_activeFormats","i","formatsAtIndex","ii","format","find","_format","splice"],"sources":["@wordpress/rich-text/src/get-active-formats.js"],"sourcesContent":["/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormatList} RichTextFormatList */\n\n/**\n * Internal dependencies\n */\nimport { isFormatEqual } from './is-format-equal';\n\n/**\n * Gets the all format objects at the start of the selection.\n *\n * @param {RichTextValue} value Value to inspect.\n * @param {Array} EMPTY_ACTIVE_FORMATS Array to return if there are no\n * active formats.\n *\n * @return {RichTextFormatList} Active format objects.\n */\nexport function getActiveFormats( value, EMPTY_ACTIVE_FORMATS = [] ) {\n\tconst { formats, start, end, activeFormats } = value;\n\tif ( start === undefined ) {\n\t\treturn EMPTY_ACTIVE_FORMATS;\n\t}\n\n\tif ( start === end ) {\n\t\t// For a collapsed caret, it is possible to override the active formats.\n\t\tif ( activeFormats ) {\n\t\t\treturn activeFormats;\n\t\t}\n\n\t\tconst formatsBefore = formats[ start - 1 ] || EMPTY_ACTIVE_FORMATS;\n\t\tconst formatsAfter = formats[ start ] || EMPTY_ACTIVE_FORMATS;\n\n\t\t// By default, select the lowest amount of formats possible (which means\n\t\t// the caret is positioned outside the format boundary). The user can\n\t\t// then use arrow keys to define `activeFormats`.\n\t\tif ( formatsBefore.length < formatsAfter.length ) {\n\t\t\treturn formatsBefore;\n\t\t}\n\n\t\treturn formatsAfter;\n\t}\n\n\t// If there's no formats at the start index, there are not active formats.\n\tif ( ! formats[ start ] ) {\n\t\treturn EMPTY_ACTIVE_FORMATS;\n\t}\n\n\tconst selectedFormats = formats.slice( start, end );\n\n\t// Clone the formats so we're not mutating the live value.\n\tconst _activeFormats = [ ...selectedFormats[ 0 ] ];\n\tlet i = selectedFormats.length;\n\n\t// For performance reasons, start from the end where it's much quicker to\n\t// realise that there are no active formats.\n\twhile ( i-- ) {\n\t\tconst formatsAtIndex = selectedFormats[ i ];\n\n\t\t// If we run into any index without formats, we're sure that there's no\n\t\t// active formats.\n\t\tif ( ! formatsAtIndex ) {\n\t\t\treturn EMPTY_ACTIVE_FORMATS;\n\t\t}\n\n\t\tlet ii = _activeFormats.length;\n\n\t\t// Loop over the active formats and remove any that are not present at\n\t\t// the current index.\n\t\twhile ( ii-- ) {\n\t\t\tconst format = _activeFormats[ ii ];\n\n\t\t\tif (\n\t\t\t\t! formatsAtIndex.find( ( _format ) =>\n\t\t\t\t\tisFormatEqual( format, _format )\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\t_activeFormats.splice( ii, 1 );\n\t\t\t}\n\t\t}\n\n\t\t// If there are no active formats, we can stop.\n\t\tif ( _activeFormats.length === 0 ) {\n\t\t\treturn EMPTY_ACTIVE_FORMATS;\n\t\t}\n\t}\n\n\treturn _activeFormats || EMPTY_ACTIVE_FORMATS;\n}\n"],"mappings":"AAAA;AACA;;AAEA;AACA;AACA;AACA,SAASA,aAAa,QAAQ,mBAAmB;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAAEC,KAAK,EAAEC,oBAAoB,GAAG,EAAE,EAAG;EACpE,MAAM;IAAEC,OAAO;IAAEC,KAAK;IAAEC,GAAG;IAAEC;EAAc,CAAC,GAAGL,KAAK;EACpD,IAAKG,KAAK,KAAKG,SAAS,EAAG;IAC1B,OAAOL,oBAAoB;EAC5B;EAEA,IAAKE,KAAK,KAAKC,GAAG,EAAG;IACpB;IACA,IAAKC,aAAa,EAAG;MACpB,OAAOA,aAAa;IACrB;IAEA,MAAME,aAAa,GAAGL,OAAO,CAAEC,KAAK,GAAG,CAAC,CAAE,IAAIF,oBAAoB;IAClE,MAAMO,YAAY,GAAGN,OAAO,CAAEC,KAAK,CAAE,IAAIF,oBAAoB;;IAE7D;IACA;IACA;IACA,IAAKM,aAAa,CAACE,MAAM,GAAGD,YAAY,CAACC,MAAM,EAAG;MACjD,OAAOF,aAAa;IACrB;IAEA,OAAOC,YAAY;EACpB;;EAEA;EACA,IAAK,CAAEN,OAAO,CAAEC,KAAK,CAAE,EAAG;IACzB,OAAOF,oBAAoB;EAC5B;EAEA,MAAMS,eAAe,GAAGR,OAAO,CAACS,KAAK,CAAER,KAAK,EAAEC,GAAI,CAAC;;EAEnD;EACA,MAAMQ,cAAc,GAAG,CAAE,GAAGF,eAAe,CAAE,CAAC,CAAE,CAAE;EAClD,IAAIG,CAAC,GAAGH,eAAe,CAACD,MAAM;;EAE9B;EACA;EACA,OAAQI,CAAC,EAAE,EAAG;IACb,MAAMC,cAAc,GAAGJ,eAAe,CAAEG,CAAC,CAAE;;IAE3C;IACA;IACA,IAAK,CAAEC,cAAc,EAAG;MACvB,OAAOb,oBAAoB;IAC5B;IAEA,IAAIc,EAAE,GAAGH,cAAc,CAACH,MAAM;;IAE9B;IACA;IACA,OAAQM,EAAE,EAAE,EAAG;MACd,MAAMC,MAAM,GAAGJ,cAAc,CAAEG,EAAE,CAAE;MAEnC,IACC,CAAED,cAAc,CAACG,IAAI,CAAIC,OAAO,IAC/BpB,aAAa,CAAEkB,MAAM,EAAEE,OAAQ,CAChC,CAAC,EACA;QACDN,cAAc,CAACO,MAAM,CAAEJ,EAAE,EAAE,CAAE,CAAC;MAC/B;IACD;;IAEA;IACA,IAAKH,cAAc,CAACH,MAAM,KAAK,CAAC,EAAG;MAClC,OAAOR,oBAAoB;IAC5B;EACD;EAEA,OAAOW,cAAc,IAAIX,oBAAoB;AAC9C","ignoreList":[]}

View File

@@ -0,0 +1,28 @@
/**
* Internal dependencies
*/
import { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Gets the active object, if there is any.
*
* @param {RichTextValue} value Value to inspect.
*
* @return {RichTextFormat|void} Active object, or undefined.
*/
export function getActiveObject({
start,
end,
replacements,
text
}) {
if (start + 1 !== end || text[start] !== OBJECT_REPLACEMENT_CHARACTER) {
return;
}
return replacements[start];
}
//# sourceMappingURL=get-active-object.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["OBJECT_REPLACEMENT_CHARACTER","getActiveObject","start","end","replacements","text"],"sources":["@wordpress/rich-text/src/get-active-object.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Gets the active object, if there is any.\n *\n * @param {RichTextValue} value Value to inspect.\n *\n * @return {RichTextFormat|void} Active object, or undefined.\n */\nexport function getActiveObject( { start, end, replacements, text } ) {\n\tif ( start + 1 !== end || text[ start ] !== OBJECT_REPLACEMENT_CHARACTER ) {\n\t\treturn;\n\t}\n\n\treturn replacements[ start ];\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,4BAA4B,QAAQ,sBAAsB;;AAEnE;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAAE;EAAEC,KAAK;EAAEC,GAAG;EAAEC,YAAY;EAAEC;AAAK,CAAC,EAAG;EACrE,IAAKH,KAAK,GAAG,CAAC,KAAKC,GAAG,IAAIE,IAAI,CAAEH,KAAK,CAAE,KAAKF,4BAA4B,EAAG;IAC1E;EACD;EAEA,OAAOI,YAAY,CAAEF,KAAK,CAAE;AAC7B","ignoreList":[]}

View File

@@ -0,0 +1,22 @@
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
/** @typedef {import('./register-format-type').RichTextFormatType} RichTextFormatType */
/**
* Returns a registered format type.
*
* @param {string} name Format name.
*
* @return {RichTextFormatType|undefined} Format type.
*/
export function getFormatType(name) {
return select(richTextStore).getFormatType(name);
}
//# sourceMappingURL=get-format-type.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["select","store","richTextStore","getFormatType","name"],"sources":["@wordpress/rich-text/src/get-format-type.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { select } from '@wordpress/data';\n/**\n * Internal dependencies\n */\nimport { store as richTextStore } from './store';\n\n/** @typedef {import('./register-format-type').RichTextFormatType} RichTextFormatType */\n\n/**\n * Returns a registered format type.\n *\n * @param {string} name Format name.\n *\n * @return {RichTextFormatType|undefined} Format type.\n */\nexport function getFormatType( name ) {\n\treturn select( richTextStore ).getFormatType( name );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,MAAM,QAAQ,iBAAiB;AACxC;AACA;AACA;AACA,SAASC,KAAK,IAAIC,aAAa,QAAQ,SAAS;;AAEhD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,aAAaA,CAAEC,IAAI,EAAG;EACrC,OAAOJ,MAAM,CAAEE,aAAc,CAAC,CAACC,aAAa,CAAEC,IAAK,CAAC;AACrD","ignoreList":[]}

View File

@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
/** @typedef {import('./register-format-type').RichTextFormatType} RichTextFormatType */
/**
* Returns all registered formats.
*
* @return {Array<RichTextFormatType>} Format settings.
*/
export function getFormatTypes() {
return select(richTextStore).getFormatTypes();
}
//# sourceMappingURL=get-format-types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["select","store","richTextStore","getFormatTypes"],"sources":["@wordpress/rich-text/src/get-format-types.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { select } from '@wordpress/data';\n/**\n * Internal dependencies\n */\nimport { store as richTextStore } from './store';\n\n/** @typedef {import('./register-format-type').RichTextFormatType} RichTextFormatType */\n\n/**\n * Returns all registered formats.\n *\n * @return {Array<RichTextFormatType>} Format settings.\n */\nexport function getFormatTypes() {\n\treturn select( richTextStore ).getFormatTypes();\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,MAAM,QAAQ,iBAAiB;AACxC;AACA;AACA;AACA,SAASC,KAAK,IAAIC,aAAa,QAAQ,SAAS;;AAEhD;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,cAAcA,CAAA,EAAG;EAChC,OAAOH,MAAM,CAAEE,aAAc,CAAC,CAACC,cAAc,CAAC,CAAC;AAChD","ignoreList":[]}

View File

@@ -0,0 +1,21 @@
/**
* Internal dependencies
*/
import { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Get the textual content of a Rich Text value. This is similar to
* `Element.textContent`.
*
* @param {RichTextValue} value Value to use.
*
* @return {string} The text content.
*/
export function getTextContent({
text
}) {
return text.replace(OBJECT_REPLACEMENT_CHARACTER, '');
}
//# sourceMappingURL=get-text-content.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["OBJECT_REPLACEMENT_CHARACTER","getTextContent","text","replace"],"sources":["@wordpress/rich-text/src/get-text-content.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Get the textual content of a Rich Text value. This is similar to\n * `Element.textContent`.\n *\n * @param {RichTextValue} value Value to use.\n *\n * @return {string} The text content.\n */\nexport function getTextContent( { text } ) {\n\treturn text.replace( OBJECT_REPLACEMENT_CHARACTER, '' );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,4BAA4B,QAAQ,sBAAsB;;AAEnE;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,cAAcA,CAAE;EAAEC;AAAK,CAAC,EAAG;EAC1C,OAAOA,IAAI,CAACC,OAAO,CAAEH,4BAA4B,EAAE,EAAG,CAAC;AACxD","ignoreList":[]}

View File

@@ -0,0 +1,33 @@
export { store } from './store';
export { applyFormat } from './apply-format';
export { concat } from './concat';
export { RichTextData, create } from './create';
export { getActiveFormat } from './get-active-format';
export { getActiveFormats } from './get-active-formats';
export { getActiveObject } from './get-active-object';
export { getTextContent } from './get-text-content';
export { isCollapsed } from './is-collapsed';
export { isEmpty } from './is-empty';
export { join } from './join';
export { registerFormatType } from './register-format-type';
export { removeFormat } from './remove-format';
export { remove } from './remove';
export { replace } from './replace';
export { insert } from './insert';
export { insertObject } from './insert-object';
export { slice } from './slice';
export { split } from './split';
export { toDom as __unstableToDom } from './to-dom';
export { toHTMLString } from './to-html-string';
export { toggleFormat } from './toggle-format';
export { unregisterFormatType } from './unregister-format-type';
export { createElement as __unstableCreateElement } from './create-element';
export { useAnchorRef } from './component/use-anchor-ref';
export { useAnchor } from './component/use-anchor';
export { default as __experimentalRichText, useRichText as __unstableUseRichText } from './component';
/**
* An object which represents a formatted string. See main `@wordpress/rich-text`
* documentation for more information.
*/
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["store","applyFormat","concat","RichTextData","create","getActiveFormat","getActiveFormats","getActiveObject","getTextContent","isCollapsed","isEmpty","join","registerFormatType","removeFormat","remove","replace","insert","insertObject","slice","split","toDom","__unstableToDom","toHTMLString","toggleFormat","unregisterFormatType","createElement","__unstableCreateElement","useAnchorRef","useAnchor","default","__experimentalRichText","useRichText","__unstableUseRichText"],"sources":["@wordpress/rich-text/src/index.ts"],"sourcesContent":["export { store } from './store';\nexport { applyFormat } from './apply-format';\nexport { concat } from './concat';\nexport { RichTextData, create } from './create';\nexport { getActiveFormat } from './get-active-format';\nexport { getActiveFormats } from './get-active-formats';\nexport { getActiveObject } from './get-active-object';\nexport { getTextContent } from './get-text-content';\nexport { isCollapsed } from './is-collapsed';\nexport { isEmpty } from './is-empty';\nexport { join } from './join';\nexport { registerFormatType } from './register-format-type';\nexport { removeFormat } from './remove-format';\nexport { remove } from './remove';\nexport { replace } from './replace';\nexport { insert } from './insert';\nexport { insertObject } from './insert-object';\nexport { slice } from './slice';\nexport { split } from './split';\nexport { toDom as __unstableToDom } from './to-dom';\nexport { toHTMLString } from './to-html-string';\nexport { toggleFormat } from './toggle-format';\nexport { unregisterFormatType } from './unregister-format-type';\nexport { createElement as __unstableCreateElement } from './create-element';\n\nexport { useAnchorRef } from './component/use-anchor-ref';\nexport { useAnchor } from './component/use-anchor';\n\nexport {\n\tdefault as __experimentalRichText,\n\tuseRichText as __unstableUseRichText,\n} from './component';\n\n/**\n * An object which represents a formatted string. See main `@wordpress/rich-text`\n * documentation for more information.\n */\nexport type { RichTextValue } from './types';\n"],"mappings":"AAAA,SAASA,KAAK,QAAQ,SAAS;AAC/B,SAASC,WAAW,QAAQ,gBAAgB;AAC5C,SAASC,MAAM,QAAQ,UAAU;AACjC,SAASC,YAAY,EAAEC,MAAM,QAAQ,UAAU;AAC/C,SAASC,eAAe,QAAQ,qBAAqB;AACrD,SAASC,gBAAgB,QAAQ,sBAAsB;AACvD,SAASC,eAAe,QAAQ,qBAAqB;AACrD,SAASC,cAAc,QAAQ,oBAAoB;AACnD,SAASC,WAAW,QAAQ,gBAAgB;AAC5C,SAASC,OAAO,QAAQ,YAAY;AACpC,SAASC,IAAI,QAAQ,QAAQ;AAC7B,SAASC,kBAAkB,QAAQ,wBAAwB;AAC3D,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,MAAM,QAAQ,UAAU;AACjC,SAASC,OAAO,QAAQ,WAAW;AACnC,SAASC,MAAM,QAAQ,UAAU;AACjC,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,KAAK,QAAQ,SAAS;AAC/B,SAASC,KAAK,QAAQ,SAAS;AAC/B,SAASC,KAAK,IAAIC,eAAe,QAAQ,UAAU;AACnD,SAASC,YAAY,QAAQ,kBAAkB;AAC/C,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,oBAAoB,QAAQ,0BAA0B;AAC/D,SAASC,aAAa,IAAIC,uBAAuB,QAAQ,kBAAkB;AAE3E,SAASC,YAAY,QAAQ,4BAA4B;AACzD,SAASC,SAAS,QAAQ,wBAAwB;AAElD,SACCC,OAAO,IAAIC,sBAAsB,EACjCC,WAAW,IAAIC,qBAAqB,QAC9B,aAAa;;AAEpB;AACA;AACA;AACA","ignoreList":[]}

View File

@@ -0,0 +1,31 @@
/**
* Internal dependencies
*/
import { insert } from './insert';
import { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Insert a format as an object into a Rich Text value at the given
* `startIndex`. Any content between `startIndex` and `endIndex` will be
* removed. Indices are retrieved from the selection if none are provided.
*
* @param {RichTextValue} value Value to modify.
* @param {RichTextFormat} formatToInsert Format to insert as object.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the object inserted.
*/
export function insertObject(value, formatToInsert, startIndex, endIndex) {
const valueToInsert = {
formats: [,],
replacements: [formatToInsert],
text: OBJECT_REPLACEMENT_CHARACTER
};
return insert(value, valueToInsert, startIndex, endIndex);
}
//# sourceMappingURL=insert-object.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["insert","OBJECT_REPLACEMENT_CHARACTER","insertObject","value","formatToInsert","startIndex","endIndex","valueToInsert","formats","replacements","text"],"sources":["@wordpress/rich-text/src/insert-object.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { insert } from './insert';\nimport { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Insert a format as an object into a Rich Text value at the given\n * `startIndex`. Any content between `startIndex` and `endIndex` will be\n * removed. Indices are retrieved from the selection if none are provided.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {RichTextFormat} formatToInsert Format to insert as object.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new value with the object inserted.\n */\nexport function insertObject( value, formatToInsert, startIndex, endIndex ) {\n\tconst valueToInsert = {\n\t\tformats: [ , ],\n\t\treplacements: [ formatToInsert ],\n\t\ttext: OBJECT_REPLACEMENT_CHARACTER,\n\t};\n\n\treturn insert( value, valueToInsert, startIndex, endIndex );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,MAAM,QAAQ,UAAU;AACjC,SAASC,4BAA4B,QAAQ,sBAAsB;;AAEnE;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAAEC,KAAK,EAAEC,cAAc,EAAEC,UAAU,EAAEC,QAAQ,EAAG;EAC3E,MAAMC,aAAa,GAAG;IACrBC,OAAO,EAAE,GAAK;IACdC,YAAY,EAAE,CAAEL,cAAc,CAAE;IAChCM,IAAI,EAAET;EACP,CAAC;EAED,OAAOD,MAAM,CAAEG,KAAK,EAAEI,aAAa,EAAEF,UAAU,EAAEC,QAAS,CAAC;AAC5D","ignoreList":[]}

View File

@@ -0,0 +1,43 @@
/**
* Internal dependencies
*/
import { create } from './create';
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Insert a Rich Text value, an HTML string, or a plain text string, into a
* Rich Text value at the given `startIndex`. Any content between `startIndex`
* and `endIndex` will be removed. Indices are retrieved from the selection if
* none are provided.
*
* @param {RichTextValue} value Value to modify.
* @param {RichTextValue|string} valueToInsert Value to insert.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the value inserted.
*/
export function insert(value, valueToInsert, startIndex = value.start, endIndex = value.end) {
const {
formats,
replacements,
text
} = value;
if (typeof valueToInsert === 'string') {
valueToInsert = create({
text: valueToInsert
});
}
const index = startIndex + valueToInsert.text.length;
return normaliseFormats({
formats: formats.slice(0, startIndex).concat(valueToInsert.formats, formats.slice(endIndex)),
replacements: replacements.slice(0, startIndex).concat(valueToInsert.replacements, replacements.slice(endIndex)),
text: text.slice(0, startIndex) + valueToInsert.text + text.slice(endIndex),
start: index,
end: index
});
}
//# sourceMappingURL=insert.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["create","normaliseFormats","insert","value","valueToInsert","startIndex","start","endIndex","end","formats","replacements","text","index","length","slice","concat"],"sources":["@wordpress/rich-text/src/insert.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { create } from './create';\nimport { normaliseFormats } from './normalise-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Insert a Rich Text value, an HTML string, or a plain text string, into a\n * Rich Text value at the given `startIndex`. Any content between `startIndex`\n * and `endIndex` will be removed. Indices are retrieved from the selection if\n * none are provided.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {RichTextValue|string} valueToInsert Value to insert.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new value with the value inserted.\n */\nexport function insert(\n\tvalue,\n\tvalueToInsert,\n\tstartIndex = value.start,\n\tendIndex = value.end\n) {\n\tconst { formats, replacements, text } = value;\n\n\tif ( typeof valueToInsert === 'string' ) {\n\t\tvalueToInsert = create( { text: valueToInsert } );\n\t}\n\n\tconst index = startIndex + valueToInsert.text.length;\n\n\treturn normaliseFormats( {\n\t\tformats: formats\n\t\t\t.slice( 0, startIndex )\n\t\t\t.concat( valueToInsert.formats, formats.slice( endIndex ) ),\n\t\treplacements: replacements\n\t\t\t.slice( 0, startIndex )\n\t\t\t.concat(\n\t\t\t\tvalueToInsert.replacements,\n\t\t\t\treplacements.slice( endIndex )\n\t\t\t),\n\t\ttext:\n\t\t\ttext.slice( 0, startIndex ) +\n\t\t\tvalueToInsert.text +\n\t\t\ttext.slice( endIndex ),\n\t\tstart: index,\n\t\tend: index,\n\t} );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,MAAM,QAAQ,UAAU;AACjC,SAASC,gBAAgB,QAAQ,qBAAqB;;AAEtD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,MAAMA,CACrBC,KAAK,EACLC,aAAa,EACbC,UAAU,GAAGF,KAAK,CAACG,KAAK,EACxBC,QAAQ,GAAGJ,KAAK,CAACK,GAAG,EACnB;EACD,MAAM;IAAEC,OAAO;IAAEC,YAAY;IAAEC;EAAK,CAAC,GAAGR,KAAK;EAE7C,IAAK,OAAOC,aAAa,KAAK,QAAQ,EAAG;IACxCA,aAAa,GAAGJ,MAAM,CAAE;MAAEW,IAAI,EAAEP;IAAc,CAAE,CAAC;EAClD;EAEA,MAAMQ,KAAK,GAAGP,UAAU,GAAGD,aAAa,CAACO,IAAI,CAACE,MAAM;EAEpD,OAAOZ,gBAAgB,CAAE;IACxBQ,OAAO,EAAEA,OAAO,CACdK,KAAK,CAAE,CAAC,EAAET,UAAW,CAAC,CACtBU,MAAM,CAAEX,aAAa,CAACK,OAAO,EAAEA,OAAO,CAACK,KAAK,CAAEP,QAAS,CAAE,CAAC;IAC5DG,YAAY,EAAEA,YAAY,CACxBI,KAAK,CAAE,CAAC,EAAET,UAAW,CAAC,CACtBU,MAAM,CACNX,aAAa,CAACM,YAAY,EAC1BA,YAAY,CAACI,KAAK,CAAEP,QAAS,CAC9B,CAAC;IACFI,IAAI,EACHA,IAAI,CAACG,KAAK,CAAE,CAAC,EAAET,UAAW,CAAC,GAC3BD,aAAa,CAACO,IAAI,GAClBA,IAAI,CAACG,KAAK,CAAEP,QAAS,CAAC;IACvBD,KAAK,EAAEM,KAAK;IACZJ,GAAG,EAAEI;EACN,CAAE,CAAC;AACJ","ignoreList":[]}

View File

@@ -0,0 +1,25 @@
/**
* Internal dependencies
*/
/**
* Check if the selection of a Rich Text value is collapsed or not. Collapsed
* means that no characters are selected, but there is a caret present. If there
* is no selection, `undefined` will be returned. This is similar to
* `window.getSelection().isCollapsed()`.
*
* @param props The rich text value to check.
* @param props.start
* @param props.end
* @return True if the selection is collapsed, false if not, undefined if there is no selection.
*/
export function isCollapsed({
start,
end
}) {
if (start === undefined || end === undefined) {
return;
}
return start === end;
}
//# sourceMappingURL=is-collapsed.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isCollapsed","start","end","undefined"],"sources":["@wordpress/rich-text/src/is-collapsed.ts"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport type { RichTextValue } from './types';\n\n/**\n * Check if the selection of a Rich Text value is collapsed or not. Collapsed\n * means that no characters are selected, but there is a caret present. If there\n * is no selection, `undefined` will be returned. This is similar to\n * `window.getSelection().isCollapsed()`.\n *\n * @param props The rich text value to check.\n * @param props.start\n * @param props.end\n * @return True if the selection is collapsed, false if not, undefined if there is no selection.\n */\nexport function isCollapsed( {\n\tstart,\n\tend,\n}: RichTextValue ): boolean | undefined {\n\tif ( start === undefined || end === undefined ) {\n\t\treturn;\n\t}\n\n\treturn start === end;\n}\n"],"mappings":"AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,WAAWA,CAAE;EAC5BC,KAAK;EACLC;AACc,CAAC,EAAwB;EACvC,IAAKD,KAAK,KAAKE,SAAS,IAAID,GAAG,KAAKC,SAAS,EAAG;IAC/C;EACD;EAEA,OAAOF,KAAK,KAAKC,GAAG;AACrB","ignoreList":[]}

View File

@@ -0,0 +1,16 @@
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Check if a Rich Text value is Empty, meaning it contains no text or any
* objects (such as images).
*
* @param {RichTextValue} value Value to use.
*
* @return {boolean} True if the value is empty, false if not.
*/
export function isEmpty({
text
}) {
return text.length === 0;
}
//# sourceMappingURL=is-empty.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isEmpty","text","length"],"sources":["@wordpress/rich-text/src/is-empty.js"],"sourcesContent":["/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Check if a Rich Text value is Empty, meaning it contains no text or any\n * objects (such as images).\n *\n * @param {RichTextValue} value Value to use.\n *\n * @return {boolean} True if the value is empty, false if not.\n */\nexport function isEmpty( { text } ) {\n\treturn text.length === 0;\n}\n"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,OAAOA,CAAE;EAAEC;AAAK,CAAC,EAAG;EACnC,OAAOA,IAAI,CAACC,MAAM,KAAK,CAAC;AACzB","ignoreList":[]}

View File

@@ -0,0 +1,52 @@
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Optimised equality check for format objects.
*
* @param {?RichTextFormat} format1 Format to compare.
* @param {?RichTextFormat} format2 Format to compare.
*
* @return {boolean} True if formats are equal, false if not.
*/
export function isFormatEqual(format1, format2) {
// Both not defined.
if (format1 === format2) {
return true;
}
// Either not defined.
if (!format1 || !format2) {
return false;
}
if (format1.type !== format2.type) {
return false;
}
const attributes1 = format1.attributes;
const attributes2 = format2.attributes;
// Both not defined.
if (attributes1 === attributes2) {
return true;
}
// Either not defined.
if (!attributes1 || !attributes2) {
return false;
}
const keys1 = Object.keys(attributes1);
const keys2 = Object.keys(attributes2);
if (keys1.length !== keys2.length) {
return false;
}
const length = keys1.length;
// Optimise for speed.
for (let i = 0; i < length; i++) {
const name = keys1[i];
if (attributes1[name] !== attributes2[name]) {
return false;
}
}
return true;
}
//# sourceMappingURL=is-format-equal.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isFormatEqual","format1","format2","type","attributes1","attributes","attributes2","keys1","Object","keys","keys2","length","i","name"],"sources":["@wordpress/rich-text/src/is-format-equal.js"],"sourcesContent":["/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Optimised equality check for format objects.\n *\n * @param {?RichTextFormat} format1 Format to compare.\n * @param {?RichTextFormat} format2 Format to compare.\n *\n * @return {boolean} True if formats are equal, false if not.\n */\nexport function isFormatEqual( format1, format2 ) {\n\t// Both not defined.\n\tif ( format1 === format2 ) {\n\t\treturn true;\n\t}\n\n\t// Either not defined.\n\tif ( ! format1 || ! format2 ) {\n\t\treturn false;\n\t}\n\n\tif ( format1.type !== format2.type ) {\n\t\treturn false;\n\t}\n\n\tconst attributes1 = format1.attributes;\n\tconst attributes2 = format2.attributes;\n\n\t// Both not defined.\n\tif ( attributes1 === attributes2 ) {\n\t\treturn true;\n\t}\n\n\t// Either not defined.\n\tif ( ! attributes1 || ! attributes2 ) {\n\t\treturn false;\n\t}\n\n\tconst keys1 = Object.keys( attributes1 );\n\tconst keys2 = Object.keys( attributes2 );\n\n\tif ( keys1.length !== keys2.length ) {\n\t\treturn false;\n\t}\n\n\tconst length = keys1.length;\n\n\t// Optimise for speed.\n\tfor ( let i = 0; i < length; i++ ) {\n\t\tconst name = keys1[ i ];\n\n\t\tif ( attributes1[ name ] !== attributes2[ name ] ) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,aAAaA,CAAEC,OAAO,EAAEC,OAAO,EAAG;EACjD;EACA,IAAKD,OAAO,KAAKC,OAAO,EAAG;IAC1B,OAAO,IAAI;EACZ;;EAEA;EACA,IAAK,CAAED,OAAO,IAAI,CAAEC,OAAO,EAAG;IAC7B,OAAO,KAAK;EACb;EAEA,IAAKD,OAAO,CAACE,IAAI,KAAKD,OAAO,CAACC,IAAI,EAAG;IACpC,OAAO,KAAK;EACb;EAEA,MAAMC,WAAW,GAAGH,OAAO,CAACI,UAAU;EACtC,MAAMC,WAAW,GAAGJ,OAAO,CAACG,UAAU;;EAEtC;EACA,IAAKD,WAAW,KAAKE,WAAW,EAAG;IAClC,OAAO,IAAI;EACZ;;EAEA;EACA,IAAK,CAAEF,WAAW,IAAI,CAAEE,WAAW,EAAG;IACrC,OAAO,KAAK;EACb;EAEA,MAAMC,KAAK,GAAGC,MAAM,CAACC,IAAI,CAAEL,WAAY,CAAC;EACxC,MAAMM,KAAK,GAAGF,MAAM,CAACC,IAAI,CAAEH,WAAY,CAAC;EAExC,IAAKC,KAAK,CAACI,MAAM,KAAKD,KAAK,CAACC,MAAM,EAAG;IACpC,OAAO,KAAK;EACb;EAEA,MAAMA,MAAM,GAAGJ,KAAK,CAACI,MAAM;;EAE3B;EACA,KAAM,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGD,MAAM,EAAEC,CAAC,EAAE,EAAG;IAClC,MAAMC,IAAI,GAAGN,KAAK,CAAEK,CAAC,CAAE;IAEvB,IAAKR,WAAW,CAAES,IAAI,CAAE,KAAKP,WAAW,CAAEO,IAAI,CAAE,EAAG;MAClD,OAAO,KAAK;IACb;EACD;EAEA,OAAO,IAAI;AACZ","ignoreList":[]}

View File

@@ -0,0 +1,14 @@
/**
* Returns true if two ranges are equal, or false otherwise. Ranges are
* considered equal if their start and end occur in the same container and
* offset.
*
* @param {Range|null} a First range object to test.
* @param {Range|null} b First range object to test.
*
* @return {boolean} Whether the two ranges are equal.
*/
export function isRangeEqual(a, b) {
return a === b || a && b && a.startContainer === b.startContainer && a.startOffset === b.startOffset && a.endContainer === b.endContainer && a.endOffset === b.endOffset;
}
//# sourceMappingURL=is-range-equal.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isRangeEqual","a","b","startContainer","startOffset","endContainer","endOffset"],"sources":["@wordpress/rich-text/src/is-range-equal.js"],"sourcesContent":["/**\n * Returns true if two ranges are equal, or false otherwise. Ranges are\n * considered equal if their start and end occur in the same container and\n * offset.\n *\n * @param {Range|null} a First range object to test.\n * @param {Range|null} b First range object to test.\n *\n * @return {boolean} Whether the two ranges are equal.\n */\nexport function isRangeEqual( a, b ) {\n\treturn (\n\t\ta === b ||\n\t\t( a &&\n\t\t\tb &&\n\t\t\ta.startContainer === b.startContainer &&\n\t\t\ta.startOffset === b.startOffset &&\n\t\t\ta.endContainer === b.endContainer &&\n\t\t\ta.endOffset === b.endOffset )\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,YAAYA,CAAEC,CAAC,EAAEC,CAAC,EAAG;EACpC,OACCD,CAAC,KAAKC,CAAC,IACLD,CAAC,IACFC,CAAC,IACDD,CAAC,CAACE,cAAc,KAAKD,CAAC,CAACC,cAAc,IACrCF,CAAC,CAACG,WAAW,KAAKF,CAAC,CAACE,WAAW,IAC/BH,CAAC,CAACI,YAAY,KAAKH,CAAC,CAACG,YAAY,IACjCJ,CAAC,CAACK,SAAS,KAAKJ,CAAC,CAACI,SAAW;AAEhC","ignoreList":[]}

36
node_modules/@wordpress/rich-text/build-module/join.js generated vendored Normal file
View File

@@ -0,0 +1,36 @@
/**
* Internal dependencies
*/
import { create } from './create';
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Combine an array of Rich Text values into one, optionally separated by
* `separator`, which can be a Rich Text value, HTML string, or plain text
* string. This is similar to `Array.prototype.join`.
*
* @param {Array<RichTextValue>} values An array of values to join.
* @param {string|RichTextValue} [separator] Separator string or value.
*
* @return {RichTextValue} A new combined value.
*/
export function join(values, separator = '') {
if (typeof separator === 'string') {
separator = create({
text: separator
});
}
return normaliseFormats(values.reduce((accumlator, {
formats,
replacements,
text
}) => ({
formats: accumlator.formats.concat(separator.formats, formats),
replacements: accumlator.replacements.concat(separator.replacements, replacements),
text: accumlator.text + separator.text + text
})));
}
//# sourceMappingURL=join.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["create","normaliseFormats","join","values","separator","text","reduce","accumlator","formats","replacements","concat"],"sources":["@wordpress/rich-text/src/join.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { create } from './create';\nimport { normaliseFormats } from './normalise-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Combine an array of Rich Text values into one, optionally separated by\n * `separator`, which can be a Rich Text value, HTML string, or plain text\n * string. This is similar to `Array.prototype.join`.\n *\n * @param {Array<RichTextValue>} values An array of values to join.\n * @param {string|RichTextValue} [separator] Separator string or value.\n *\n * @return {RichTextValue} A new combined value.\n */\nexport function join( values, separator = '' ) {\n\tif ( typeof separator === 'string' ) {\n\t\tseparator = create( { text: separator } );\n\t}\n\n\treturn normaliseFormats(\n\t\tvalues.reduce( ( accumlator, { formats, replacements, text } ) => ( {\n\t\t\tformats: accumlator.formats.concat( separator.formats, formats ),\n\t\t\treplacements: accumlator.replacements.concat(\n\t\t\t\tseparator.replacements,\n\t\t\t\treplacements\n\t\t\t),\n\t\t\ttext: accumlator.text + separator.text + text,\n\t\t} ) )\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,MAAM,QAAQ,UAAU;AACjC,SAASC,gBAAgB,QAAQ,qBAAqB;;AAEtD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,IAAIA,CAAEC,MAAM,EAAEC,SAAS,GAAG,EAAE,EAAG;EAC9C,IAAK,OAAOA,SAAS,KAAK,QAAQ,EAAG;IACpCA,SAAS,GAAGJ,MAAM,CAAE;MAAEK,IAAI,EAAED;IAAU,CAAE,CAAC;EAC1C;EAEA,OAAOH,gBAAgB,CACtBE,MAAM,CAACG,MAAM,CAAE,CAAEC,UAAU,EAAE;IAAEC,OAAO;IAAEC,YAAY;IAAEJ;EAAK,CAAC,MAAQ;IACnEG,OAAO,EAAED,UAAU,CAACC,OAAO,CAACE,MAAM,CAAEN,SAAS,CAACI,OAAO,EAAEA,OAAQ,CAAC;IAChEC,YAAY,EAAEF,UAAU,CAACE,YAAY,CAACC,MAAM,CAC3CN,SAAS,CAACK,YAAY,EACtBA,YACD,CAAC;IACDJ,IAAI,EAAEE,UAAU,CAACF,IAAI,GAAGD,SAAS,CAACC,IAAI,GAAGA;EAC1C,CAAC,CAAG,CACL,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,37 @@
/**
* Internal dependencies
*/
import { isFormatEqual } from './is-format-equal';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Normalises formats: ensures subsequent adjacent equal formats have the same
* reference.
*
* @param {RichTextValue} value Value to normalise formats of.
*
* @return {RichTextValue} New value with normalised formats.
*/
export function normaliseFormats(value) {
const newFormats = value.formats.slice();
newFormats.forEach((formatsAtIndex, index) => {
const formatsAtPreviousIndex = newFormats[index - 1];
if (formatsAtPreviousIndex) {
const newFormatsAtIndex = formatsAtIndex.slice();
newFormatsAtIndex.forEach((format, formatIndex) => {
const previousFormat = formatsAtPreviousIndex[formatIndex];
if (isFormatEqual(format, previousFormat)) {
newFormatsAtIndex[formatIndex] = previousFormat;
}
});
newFormats[index] = newFormatsAtIndex;
}
});
return {
...value,
formats: newFormats
};
}
//# sourceMappingURL=normalise-formats.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isFormatEqual","normaliseFormats","value","newFormats","formats","slice","forEach","formatsAtIndex","index","formatsAtPreviousIndex","newFormatsAtIndex","format","formatIndex","previousFormat"],"sources":["@wordpress/rich-text/src/normalise-formats.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { isFormatEqual } from './is-format-equal';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Normalises formats: ensures subsequent adjacent equal formats have the same\n * reference.\n *\n * @param {RichTextValue} value Value to normalise formats of.\n *\n * @return {RichTextValue} New value with normalised formats.\n */\nexport function normaliseFormats( value ) {\n\tconst newFormats = value.formats.slice();\n\n\tnewFormats.forEach( ( formatsAtIndex, index ) => {\n\t\tconst formatsAtPreviousIndex = newFormats[ index - 1 ];\n\n\t\tif ( formatsAtPreviousIndex ) {\n\t\t\tconst newFormatsAtIndex = formatsAtIndex.slice();\n\n\t\t\tnewFormatsAtIndex.forEach( ( format, formatIndex ) => {\n\t\t\t\tconst previousFormat = formatsAtPreviousIndex[ formatIndex ];\n\n\t\t\t\tif ( isFormatEqual( format, previousFormat ) ) {\n\t\t\t\t\tnewFormatsAtIndex[ formatIndex ] = previousFormat;\n\t\t\t\t}\n\t\t\t} );\n\n\t\t\tnewFormats[ index ] = newFormatsAtIndex;\n\t\t}\n\t} );\n\n\treturn {\n\t\t...value,\n\t\tformats: newFormats,\n\t};\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,aAAa,QAAQ,mBAAmB;;AAEjD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAAEC,KAAK,EAAG;EACzC,MAAMC,UAAU,GAAGD,KAAK,CAACE,OAAO,CAACC,KAAK,CAAC,CAAC;EAExCF,UAAU,CAACG,OAAO,CAAE,CAAEC,cAAc,EAAEC,KAAK,KAAM;IAChD,MAAMC,sBAAsB,GAAGN,UAAU,CAAEK,KAAK,GAAG,CAAC,CAAE;IAEtD,IAAKC,sBAAsB,EAAG;MAC7B,MAAMC,iBAAiB,GAAGH,cAAc,CAACF,KAAK,CAAC,CAAC;MAEhDK,iBAAiB,CAACJ,OAAO,CAAE,CAAEK,MAAM,EAAEC,WAAW,KAAM;QACrD,MAAMC,cAAc,GAAGJ,sBAAsB,CAAEG,WAAW,CAAE;QAE5D,IAAKZ,aAAa,CAAEW,MAAM,EAAEE,cAAe,CAAC,EAAG;UAC9CH,iBAAiB,CAAEE,WAAW,CAAE,GAAGC,cAAc;QAClD;MACD,CAAE,CAAC;MAEHV,UAAU,CAAEK,KAAK,CAAE,GAAGE,iBAAiB;IACxC;EACD,CAAE,CAAC;EAEH,OAAO;IACN,GAAGR,KAAK;IACRE,OAAO,EAAED;EACV,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,90 @@
/**
* WordPress dependencies
*/
import { select, dispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
/**
* @typedef {Object} WPFormat
*
* @property {string} name A string identifying the format. Must be
* unique across all registered formats.
* @property {string} tagName The HTML tag this format will wrap the
* selection with.
* @property {boolean} interactive Whether format makes content interactive or not.
* @property {string | null} [className] A class to match the format.
* @property {string} title Name of the format.
* @property {Function} edit Should return a component for the user to
* interact with the new registered format.
*/
/**
* Registers a new format provided a unique name and an object defining its
* behavior.
*
* @param {string} name Format name.
* @param {WPFormat} settings Format settings.
*
* @return {WPFormat|undefined} The format, if it has been successfully
* registered; otherwise `undefined`.
*/
export function registerFormatType(name, settings) {
settings = {
name,
...settings
};
if (typeof settings.name !== 'string') {
window.console.error('Format names must be strings.');
return;
}
if (!/^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test(settings.name)) {
window.console.error('Format names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-format');
return;
}
if (select(richTextStore).getFormatType(settings.name)) {
window.console.error('Format "' + settings.name + '" is already registered.');
return;
}
if (typeof settings.tagName !== 'string' || settings.tagName === '') {
window.console.error('Format tag names must be a string.');
return;
}
if ((typeof settings.className !== 'string' || settings.className === '') && settings.className !== null) {
window.console.error('Format class names must be a string, or null to handle bare elements.');
return;
}
if (!/^[_a-zA-Z]+[a-zA-Z0-9_-]*$/.test(settings.className)) {
window.console.error('A class name must begin with a letter, followed by any number of hyphens, underscores, letters, or numbers.');
return;
}
if (settings.className === null) {
const formatTypeForBareElement = select(richTextStore).getFormatTypeForBareElement(settings.tagName);
if (formatTypeForBareElement && formatTypeForBareElement.name !== 'core/unknown') {
window.console.error(`Format "${formatTypeForBareElement.name}" is already registered to handle bare tag name "${settings.tagName}".`);
return;
}
} else {
const formatTypeForClassName = select(richTextStore).getFormatTypeForClassName(settings.className);
if (formatTypeForClassName) {
window.console.error(`Format "${formatTypeForClassName.name}" is already registered to handle class name "${settings.className}".`);
return;
}
}
if (!('title' in settings) || settings.title === '') {
window.console.error('The format "' + settings.name + '" must have a title.');
return;
}
if ('keywords' in settings && settings.keywords.length > 3) {
window.console.error('The format "' + settings.name + '" can have a maximum of 3 keywords.');
return;
}
if (typeof settings.title !== 'string') {
window.console.error('Format titles must be strings.');
return;
}
dispatch(richTextStore).addFormatTypes(settings);
return settings;
}
//# sourceMappingURL=register-format-type.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,70 @@
/**
* Internal dependencies
*/
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Remove any format object from a Rich Text value by type from the given
* `startIndex` to the given `endIndex`. Indices are retrieved from the
* selection if none are provided.
*
* @param {RichTextValue} value Value to modify.
* @param {string} formatType Format type to remove.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the format applied.
*/
export function removeFormat(value, formatType, startIndex = value.start, endIndex = value.end) {
const {
formats,
activeFormats
} = value;
const newFormats = formats.slice();
// If the selection is collapsed, expand start and end to the edges of the
// format.
if (startIndex === endIndex) {
const format = newFormats[startIndex]?.find(({
type
}) => type === formatType);
if (format) {
while (newFormats[startIndex]?.find(newFormat => newFormat === format)) {
filterFormats(newFormats, startIndex, formatType);
startIndex--;
}
endIndex++;
while (newFormats[endIndex]?.find(newFormat => newFormat === format)) {
filterFormats(newFormats, endIndex, formatType);
endIndex++;
}
}
} else {
for (let i = startIndex; i < endIndex; i++) {
if (newFormats[i]) {
filterFormats(newFormats, i, formatType);
}
}
}
return normaliseFormats({
...value,
formats: newFormats,
activeFormats: activeFormats?.filter(({
type
}) => type !== formatType) || []
});
}
function filterFormats(formats, index, formatType) {
const newFormats = formats[index].filter(({
type
}) => type !== formatType);
if (newFormats.length) {
formats[index] = newFormats;
} else {
delete formats[index];
}
}
//# sourceMappingURL=remove-format.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["normaliseFormats","removeFormat","value","formatType","startIndex","start","endIndex","end","formats","activeFormats","newFormats","slice","format","find","type","newFormat","filterFormats","i","filter","index","length"],"sources":["@wordpress/rich-text/src/remove-format.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { normaliseFormats } from './normalise-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Remove any format object from a Rich Text value by type from the given\n * `startIndex` to the given `endIndex`. Indices are retrieved from the\n * selection if none are provided.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {string} formatType Format type to remove.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new value with the format applied.\n */\nexport function removeFormat(\n\tvalue,\n\tformatType,\n\tstartIndex = value.start,\n\tendIndex = value.end\n) {\n\tconst { formats, activeFormats } = value;\n\tconst newFormats = formats.slice();\n\n\t// If the selection is collapsed, expand start and end to the edges of the\n\t// format.\n\tif ( startIndex === endIndex ) {\n\t\tconst format = newFormats[ startIndex ]?.find(\n\t\t\t( { type } ) => type === formatType\n\t\t);\n\n\t\tif ( format ) {\n\t\t\twhile (\n\t\t\t\tnewFormats[ startIndex ]?.find(\n\t\t\t\t\t( newFormat ) => newFormat === format\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tfilterFormats( newFormats, startIndex, formatType );\n\t\t\t\tstartIndex--;\n\t\t\t}\n\n\t\t\tendIndex++;\n\n\t\t\twhile (\n\t\t\t\tnewFormats[ endIndex ]?.find(\n\t\t\t\t\t( newFormat ) => newFormat === format\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tfilterFormats( newFormats, endIndex, formatType );\n\t\t\t\tendIndex++;\n\t\t\t}\n\t\t}\n\t} else {\n\t\tfor ( let i = startIndex; i < endIndex; i++ ) {\n\t\t\tif ( newFormats[ i ] ) {\n\t\t\t\tfilterFormats( newFormats, i, formatType );\n\t\t\t}\n\t\t}\n\t}\n\n\treturn normaliseFormats( {\n\t\t...value,\n\t\tformats: newFormats,\n\t\tactiveFormats:\n\t\t\tactiveFormats?.filter( ( { type } ) => type !== formatType ) || [],\n\t} );\n}\n\nfunction filterFormats( formats, index, formatType ) {\n\tconst newFormats = formats[ index ].filter(\n\t\t( { type } ) => type !== formatType\n\t);\n\n\tif ( newFormats.length ) {\n\t\tformats[ index ] = newFormats;\n\t} else {\n\t\tdelete formats[ index ];\n\t}\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,gBAAgB,QAAQ,qBAAqB;;AAEtD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAC3BC,KAAK,EACLC,UAAU,EACVC,UAAU,GAAGF,KAAK,CAACG,KAAK,EACxBC,QAAQ,GAAGJ,KAAK,CAACK,GAAG,EACnB;EACD,MAAM;IAAEC,OAAO;IAAEC;EAAc,CAAC,GAAGP,KAAK;EACxC,MAAMQ,UAAU,GAAGF,OAAO,CAACG,KAAK,CAAC,CAAC;;EAElC;EACA;EACA,IAAKP,UAAU,KAAKE,QAAQ,EAAG;IAC9B,MAAMM,MAAM,GAAGF,UAAU,CAAEN,UAAU,CAAE,EAAES,IAAI,CAC5C,CAAE;MAAEC;IAAK,CAAC,KAAMA,IAAI,KAAKX,UAC1B,CAAC;IAED,IAAKS,MAAM,EAAG;MACb,OACCF,UAAU,CAAEN,UAAU,CAAE,EAAES,IAAI,CAC3BE,SAAS,IAAMA,SAAS,KAAKH,MAChC,CAAC,EACA;QACDI,aAAa,CAAEN,UAAU,EAAEN,UAAU,EAAED,UAAW,CAAC;QACnDC,UAAU,EAAE;MACb;MAEAE,QAAQ,EAAE;MAEV,OACCI,UAAU,CAAEJ,QAAQ,CAAE,EAAEO,IAAI,CACzBE,SAAS,IAAMA,SAAS,KAAKH,MAChC,CAAC,EACA;QACDI,aAAa,CAAEN,UAAU,EAAEJ,QAAQ,EAAEH,UAAW,CAAC;QACjDG,QAAQ,EAAE;MACX;IACD;EACD,CAAC,MAAM;IACN,KAAM,IAAIW,CAAC,GAAGb,UAAU,EAAEa,CAAC,GAAGX,QAAQ,EAAEW,CAAC,EAAE,EAAG;MAC7C,IAAKP,UAAU,CAAEO,CAAC,CAAE,EAAG;QACtBD,aAAa,CAAEN,UAAU,EAAEO,CAAC,EAAEd,UAAW,CAAC;MAC3C;IACD;EACD;EAEA,OAAOH,gBAAgB,CAAE;IACxB,GAAGE,KAAK;IACRM,OAAO,EAAEE,UAAU;IACnBD,aAAa,EACZA,aAAa,EAAES,MAAM,CAAE,CAAE;MAAEJ;IAAK,CAAC,KAAMA,IAAI,KAAKX,UAAW,CAAC,IAAI;EAClE,CAAE,CAAC;AACJ;AAEA,SAASa,aAAaA,CAAER,OAAO,EAAEW,KAAK,EAAEhB,UAAU,EAAG;EACpD,MAAMO,UAAU,GAAGF,OAAO,CAAEW,KAAK,CAAE,CAACD,MAAM,CACzC,CAAE;IAAEJ;EAAK,CAAC,KAAMA,IAAI,KAAKX,UAC1B,CAAC;EAED,IAAKO,UAAU,CAACU,MAAM,EAAG;IACxBZ,OAAO,CAAEW,KAAK,CAAE,GAAGT,UAAU;EAC9B,CAAC,MAAM;IACN,OAAOF,OAAO,CAAEW,KAAK,CAAE;EACxB;AACD","ignoreList":[]}

View File

@@ -0,0 +1,2 @@
//# sourceMappingURL=remove-unregistered-formatting.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":[],"sources":["@wordpress/rich-text/src/remove-unregistered-formatting.js"],"sourcesContent":[""],"mappings":"","ignoreList":[]}

View File

@@ -0,0 +1,23 @@
/**
* Internal dependencies
*/
import { insert } from './insert';
import { create } from './create';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Remove content from a Rich Text value between the given `startIndex` and
* `endIndex`. Indices are retrieved from the selection if none are provided.
*
* @param {RichTextValue} value Value to modify.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the content removed.
*/
export function remove(value, startIndex, endIndex) {
return insert(value, create(), startIndex, endIndex);
}
//# sourceMappingURL=remove.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["insert","create","remove","value","startIndex","endIndex"],"sources":["@wordpress/rich-text/src/remove.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { insert } from './insert';\nimport { create } from './create';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Remove content from a Rich Text value between the given `startIndex` and\n * `endIndex`. Indices are retrieved from the selection if none are provided.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new value with the content removed.\n */\nexport function remove( value, startIndex, endIndex ) {\n\treturn insert( value, create(), startIndex, endIndex );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,MAAM,QAAQ,UAAU;AACjC,SAASC,MAAM,QAAQ,UAAU;;AAEjC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,MAAMA,CAAEC,KAAK,EAAEC,UAAU,EAAEC,QAAQ,EAAG;EACrD,OAAOL,MAAM,CAAEG,KAAK,EAAEF,MAAM,CAAC,CAAC,EAAEG,UAAU,EAAEC,QAAS,CAAC;AACvD","ignoreList":[]}

View File

@@ -0,0 +1,66 @@
/**
* Internal dependencies
*/
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Search a Rich Text value and replace the match(es) with `replacement`. This
* is similar to `String.prototype.replace`.
*
* @param {RichTextValue} value The value to modify.
* @param {RegExp|string} pattern A RegExp object or literal. Can also be
* a string. It is treated as a verbatim
* string and is not interpreted as a
* regular expression. Only the first
* occurrence will be replaced.
* @param {Function|string} replacement The match or matches are replaced with
* the specified or the value returned by
* the specified function.
*
* @return {RichTextValue} A new value with replacements applied.
*/
export function replace({
formats,
replacements,
text,
start,
end
}, pattern, replacement) {
text = text.replace(pattern, (match, ...rest) => {
const offset = rest[rest.length - 2];
let newText = replacement;
let newFormats;
let newReplacements;
if (typeof newText === 'function') {
newText = replacement(match, ...rest);
}
if (typeof newText === 'object') {
newFormats = newText.formats;
newReplacements = newText.replacements;
newText = newText.text;
} else {
newFormats = Array(newText.length);
newReplacements = Array(newText.length);
if (formats[offset]) {
newFormats = newFormats.fill(formats[offset]);
}
}
formats = formats.slice(0, offset).concat(newFormats, formats.slice(offset + match.length));
replacements = replacements.slice(0, offset).concat(newReplacements, replacements.slice(offset + match.length));
if (start) {
start = end = offset + newText.length;
}
return newText;
});
return normaliseFormats({
formats,
replacements,
text,
start,
end
});
}
//# sourceMappingURL=replace.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["normaliseFormats","replace","formats","replacements","text","start","end","pattern","replacement","match","rest","offset","length","newText","newFormats","newReplacements","Array","fill","slice","concat"],"sources":["@wordpress/rich-text/src/replace.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { normaliseFormats } from './normalise-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Search a Rich Text value and replace the match(es) with `replacement`. This\n * is similar to `String.prototype.replace`.\n *\n * @param {RichTextValue} value The value to modify.\n * @param {RegExp|string} pattern A RegExp object or literal. Can also be\n * a string. It is treated as a verbatim\n * string and is not interpreted as a\n * regular expression. Only the first\n * occurrence will be replaced.\n * @param {Function|string} replacement The match or matches are replaced with\n * the specified or the value returned by\n * the specified function.\n *\n * @return {RichTextValue} A new value with replacements applied.\n */\nexport function replace(\n\t{ formats, replacements, text, start, end },\n\tpattern,\n\treplacement\n) {\n\ttext = text.replace( pattern, ( match, ...rest ) => {\n\t\tconst offset = rest[ rest.length - 2 ];\n\t\tlet newText = replacement;\n\t\tlet newFormats;\n\t\tlet newReplacements;\n\n\t\tif ( typeof newText === 'function' ) {\n\t\t\tnewText = replacement( match, ...rest );\n\t\t}\n\n\t\tif ( typeof newText === 'object' ) {\n\t\t\tnewFormats = newText.formats;\n\t\t\tnewReplacements = newText.replacements;\n\t\t\tnewText = newText.text;\n\t\t} else {\n\t\t\tnewFormats = Array( newText.length );\n\t\t\tnewReplacements = Array( newText.length );\n\n\t\t\tif ( formats[ offset ] ) {\n\t\t\t\tnewFormats = newFormats.fill( formats[ offset ] );\n\t\t\t}\n\t\t}\n\n\t\tformats = formats\n\t\t\t.slice( 0, offset )\n\t\t\t.concat( newFormats, formats.slice( offset + match.length ) );\n\t\treplacements = replacements\n\t\t\t.slice( 0, offset )\n\t\t\t.concat(\n\t\t\t\tnewReplacements,\n\t\t\t\treplacements.slice( offset + match.length )\n\t\t\t);\n\n\t\tif ( start ) {\n\t\t\tstart = end = offset + newText.length;\n\t\t}\n\n\t\treturn newText;\n\t} );\n\n\treturn normaliseFormats( { formats, replacements, text, start, end } );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,gBAAgB,QAAQ,qBAAqB;;AAEtD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,OAAOA,CACtB;EAAEC,OAAO;EAAEC,YAAY;EAAEC,IAAI;EAAEC,KAAK;EAAEC;AAAI,CAAC,EAC3CC,OAAO,EACPC,WAAW,EACV;EACDJ,IAAI,GAAGA,IAAI,CAACH,OAAO,CAAEM,OAAO,EAAE,CAAEE,KAAK,EAAE,GAAGC,IAAI,KAAM;IACnD,MAAMC,MAAM,GAAGD,IAAI,CAAEA,IAAI,CAACE,MAAM,GAAG,CAAC,CAAE;IACtC,IAAIC,OAAO,GAAGL,WAAW;IACzB,IAAIM,UAAU;IACd,IAAIC,eAAe;IAEnB,IAAK,OAAOF,OAAO,KAAK,UAAU,EAAG;MACpCA,OAAO,GAAGL,WAAW,CAAEC,KAAK,EAAE,GAAGC,IAAK,CAAC;IACxC;IAEA,IAAK,OAAOG,OAAO,KAAK,QAAQ,EAAG;MAClCC,UAAU,GAAGD,OAAO,CAACX,OAAO;MAC5Ba,eAAe,GAAGF,OAAO,CAACV,YAAY;MACtCU,OAAO,GAAGA,OAAO,CAACT,IAAI;IACvB,CAAC,MAAM;MACNU,UAAU,GAAGE,KAAK,CAAEH,OAAO,CAACD,MAAO,CAAC;MACpCG,eAAe,GAAGC,KAAK,CAAEH,OAAO,CAACD,MAAO,CAAC;MAEzC,IAAKV,OAAO,CAAES,MAAM,CAAE,EAAG;QACxBG,UAAU,GAAGA,UAAU,CAACG,IAAI,CAAEf,OAAO,CAAES,MAAM,CAAG,CAAC;MAClD;IACD;IAEAT,OAAO,GAAGA,OAAO,CACfgB,KAAK,CAAE,CAAC,EAAEP,MAAO,CAAC,CAClBQ,MAAM,CAAEL,UAAU,EAAEZ,OAAO,CAACgB,KAAK,CAAEP,MAAM,GAAGF,KAAK,CAACG,MAAO,CAAE,CAAC;IAC9DT,YAAY,GAAGA,YAAY,CACzBe,KAAK,CAAE,CAAC,EAAEP,MAAO,CAAC,CAClBQ,MAAM,CACNJ,eAAe,EACfZ,YAAY,CAACe,KAAK,CAAEP,MAAM,GAAGF,KAAK,CAACG,MAAO,CAC3C,CAAC;IAEF,IAAKP,KAAK,EAAG;MACZA,KAAK,GAAGC,GAAG,GAAGK,MAAM,GAAGE,OAAO,CAACD,MAAM;IACtC;IAEA,OAAOC,OAAO;EACf,CAAE,CAAC;EAEH,OAAOb,gBAAgB,CAAE;IAAEE,OAAO;IAAEC,YAAY;IAAEC,IAAI;IAAEC,KAAK;IAAEC;EAAI,CAAE,CAAC;AACvE","ignoreList":[]}

View File

@@ -0,0 +1,31 @@
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Slice a Rich Text value from `startIndex` to `endIndex`. Indices are
* retrieved from the selection if none are provided. This is similar to
* `String.prototype.slice`.
*
* @param {RichTextValue} value Value to modify.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new extracted value.
*/
export function slice(value, startIndex = value.start, endIndex = value.end) {
const {
formats,
replacements,
text
} = value;
if (startIndex === undefined || endIndex === undefined) {
return {
...value
};
}
return {
formats: formats.slice(startIndex, endIndex),
replacements: replacements.slice(startIndex, endIndex),
text: text.slice(startIndex, endIndex)
};
}
//# sourceMappingURL=slice.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["slice","value","startIndex","start","endIndex","end","formats","replacements","text","undefined"],"sources":["@wordpress/rich-text/src/slice.js"],"sourcesContent":["/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Slice a Rich Text value from `startIndex` to `endIndex`. Indices are\n * retrieved from the selection if none are provided. This is similar to\n * `String.prototype.slice`.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new extracted value.\n */\nexport function slice( value, startIndex = value.start, endIndex = value.end ) {\n\tconst { formats, replacements, text } = value;\n\n\tif ( startIndex === undefined || endIndex === undefined ) {\n\t\treturn { ...value };\n\t}\n\n\treturn {\n\t\tformats: formats.slice( startIndex, endIndex ),\n\t\treplacements: replacements.slice( startIndex, endIndex ),\n\t\ttext: text.slice( startIndex, endIndex ),\n\t};\n}\n"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,KAAKA,CAAEC,KAAK,EAAEC,UAAU,GAAGD,KAAK,CAACE,KAAK,EAAEC,QAAQ,GAAGH,KAAK,CAACI,GAAG,EAAG;EAC9E,MAAM;IAAEC,OAAO;IAAEC,YAAY;IAAEC;EAAK,CAAC,GAAGP,KAAK;EAE7C,IAAKC,UAAU,KAAKO,SAAS,IAAIL,QAAQ,KAAKK,SAAS,EAAG;IACzD,OAAO;MAAE,GAAGR;IAAM,CAAC;EACpB;EAEA,OAAO;IACNK,OAAO,EAAEA,OAAO,CAACN,KAAK,CAAEE,UAAU,EAAEE,QAAS,CAAC;IAC9CG,YAAY,EAAEA,YAAY,CAACP,KAAK,CAAEE,UAAU,EAAEE,QAAS,CAAC;IACxDI,IAAI,EAAEA,IAAI,CAACR,KAAK,CAAEE,UAAU,EAAEE,QAAS;EACxC,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,11 @@
/**
* Object replacement character, used as a placeholder for objects.
*/
export const OBJECT_REPLACEMENT_CHARACTER = '\ufffc';
/**
* Zero width non-breaking space, used as padding in the editable DOM tree when
* it is empty otherwise.
*/
export const ZWNBSP = '\ufeff';
//# sourceMappingURL=special-characters.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["OBJECT_REPLACEMENT_CHARACTER","ZWNBSP"],"sources":["@wordpress/rich-text/src/special-characters.js"],"sourcesContent":["/**\n * Object replacement character, used as a placeholder for objects.\n */\nexport const OBJECT_REPLACEMENT_CHARACTER = '\\ufffc';\n\n/**\n * Zero width non-breaking space, used as padding in the editable DOM tree when\n * it is empty otherwise.\n */\nexport const ZWNBSP = '\\ufeff';\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAO,MAAMA,4BAA4B,GAAG,QAAQ;;AAEpD;AACA;AACA;AACA;AACA,OAAO,MAAMC,MAAM,GAAG,QAAQ","ignoreList":[]}

View File

@@ -0,0 +1,75 @@
/**
* Internal dependencies
*/
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Split a Rich Text value in two at the given `startIndex` and `endIndex`, or
* split at the given separator. This is similar to `String.prototype.split`.
* Indices are retrieved from the selection if none are provided.
*
* @param {RichTextValue} value
* @param {number|string} [string] Start index, or string at which to split.
*
* @return {Array<RichTextValue>|undefined} An array of new values.
*/
export function split({
formats,
replacements,
text,
start,
end
}, string) {
if (typeof string !== 'string') {
return splitAtSelection(...arguments);
}
let nextStart = 0;
return text.split(string).map(substring => {
const startIndex = nextStart;
const value = {
formats: formats.slice(startIndex, startIndex + substring.length),
replacements: replacements.slice(startIndex, startIndex + substring.length),
text: substring
};
nextStart += string.length + substring.length;
if (start !== undefined && end !== undefined) {
if (start >= startIndex && start < nextStart) {
value.start = start - startIndex;
} else if (start < startIndex && end > startIndex) {
value.start = 0;
}
if (end >= startIndex && end < nextStart) {
value.end = end - startIndex;
} else if (start < nextStart && end > nextStart) {
value.end = substring.length;
}
}
return value;
});
}
function splitAtSelection({
formats,
replacements,
text,
start,
end
}, startIndex = start, endIndex = end) {
if (start === undefined || end === undefined) {
return;
}
const before = {
formats: formats.slice(0, startIndex),
replacements: replacements.slice(0, startIndex),
text: text.slice(0, startIndex)
};
const after = {
formats: formats.slice(endIndex),
replacements: replacements.slice(endIndex),
text: text.slice(endIndex),
start: 0,
end: 0
};
return [before, after];
}
//# sourceMappingURL=split.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["split","formats","replacements","text","start","end","string","splitAtSelection","arguments","nextStart","map","substring","startIndex","value","slice","length","undefined","endIndex","before","after"],"sources":["@wordpress/rich-text/src/split.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Split a Rich Text value in two at the given `startIndex` and `endIndex`, or\n * split at the given separator. This is similar to `String.prototype.split`.\n * Indices are retrieved from the selection if none are provided.\n *\n * @param {RichTextValue} value\n * @param {number|string} [string] Start index, or string at which to split.\n *\n * @return {Array<RichTextValue>|undefined} An array of new values.\n */\nexport function split( { formats, replacements, text, start, end }, string ) {\n\tif ( typeof string !== 'string' ) {\n\t\treturn splitAtSelection( ...arguments );\n\t}\n\n\tlet nextStart = 0;\n\n\treturn text.split( string ).map( ( substring ) => {\n\t\tconst startIndex = nextStart;\n\t\tconst value = {\n\t\t\tformats: formats.slice( startIndex, startIndex + substring.length ),\n\t\t\treplacements: replacements.slice(\n\t\t\t\tstartIndex,\n\t\t\t\tstartIndex + substring.length\n\t\t\t),\n\t\t\ttext: substring,\n\t\t};\n\n\t\tnextStart += string.length + substring.length;\n\n\t\tif ( start !== undefined && end !== undefined ) {\n\t\t\tif ( start >= startIndex && start < nextStart ) {\n\t\t\t\tvalue.start = start - startIndex;\n\t\t\t} else if ( start < startIndex && end > startIndex ) {\n\t\t\t\tvalue.start = 0;\n\t\t\t}\n\n\t\t\tif ( end >= startIndex && end < nextStart ) {\n\t\t\t\tvalue.end = end - startIndex;\n\t\t\t} else if ( start < nextStart && end > nextStart ) {\n\t\t\t\tvalue.end = substring.length;\n\t\t\t}\n\t\t}\n\n\t\treturn value;\n\t} );\n}\n\nfunction splitAtSelection(\n\t{ formats, replacements, text, start, end },\n\tstartIndex = start,\n\tendIndex = end\n) {\n\tif ( start === undefined || end === undefined ) {\n\t\treturn;\n\t}\n\n\tconst before = {\n\t\tformats: formats.slice( 0, startIndex ),\n\t\treplacements: replacements.slice( 0, startIndex ),\n\t\ttext: text.slice( 0, startIndex ),\n\t};\n\tconst after = {\n\t\tformats: formats.slice( endIndex ),\n\t\treplacements: replacements.slice( endIndex ),\n\t\ttext: text.slice( endIndex ),\n\t\tstart: 0,\n\t\tend: 0,\n\t};\n\n\treturn [ before, after ];\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,KAAKA,CAAE;EAAEC,OAAO;EAAEC,YAAY;EAAEC,IAAI;EAAEC,KAAK;EAAEC;AAAI,CAAC,EAAEC,MAAM,EAAG;EAC5E,IAAK,OAAOA,MAAM,KAAK,QAAQ,EAAG;IACjC,OAAOC,gBAAgB,CAAE,GAAGC,SAAU,CAAC;EACxC;EAEA,IAAIC,SAAS,GAAG,CAAC;EAEjB,OAAON,IAAI,CAACH,KAAK,CAAEM,MAAO,CAAC,CAACI,GAAG,CAAIC,SAAS,IAAM;IACjD,MAAMC,UAAU,GAAGH,SAAS;IAC5B,MAAMI,KAAK,GAAG;MACbZ,OAAO,EAAEA,OAAO,CAACa,KAAK,CAAEF,UAAU,EAAEA,UAAU,GAAGD,SAAS,CAACI,MAAO,CAAC;MACnEb,YAAY,EAAEA,YAAY,CAACY,KAAK,CAC/BF,UAAU,EACVA,UAAU,GAAGD,SAAS,CAACI,MACxB,CAAC;MACDZ,IAAI,EAAEQ;IACP,CAAC;IAEDF,SAAS,IAAIH,MAAM,CAACS,MAAM,GAAGJ,SAAS,CAACI,MAAM;IAE7C,IAAKX,KAAK,KAAKY,SAAS,IAAIX,GAAG,KAAKW,SAAS,EAAG;MAC/C,IAAKZ,KAAK,IAAIQ,UAAU,IAAIR,KAAK,GAAGK,SAAS,EAAG;QAC/CI,KAAK,CAACT,KAAK,GAAGA,KAAK,GAAGQ,UAAU;MACjC,CAAC,MAAM,IAAKR,KAAK,GAAGQ,UAAU,IAAIP,GAAG,GAAGO,UAAU,EAAG;QACpDC,KAAK,CAACT,KAAK,GAAG,CAAC;MAChB;MAEA,IAAKC,GAAG,IAAIO,UAAU,IAAIP,GAAG,GAAGI,SAAS,EAAG;QAC3CI,KAAK,CAACR,GAAG,GAAGA,GAAG,GAAGO,UAAU;MAC7B,CAAC,MAAM,IAAKR,KAAK,GAAGK,SAAS,IAAIJ,GAAG,GAAGI,SAAS,EAAG;QAClDI,KAAK,CAACR,GAAG,GAAGM,SAAS,CAACI,MAAM;MAC7B;IACD;IAEA,OAAOF,KAAK;EACb,CAAE,CAAC;AACJ;AAEA,SAASN,gBAAgBA,CACxB;EAAEN,OAAO;EAAEC,YAAY;EAAEC,IAAI;EAAEC,KAAK;EAAEC;AAAI,CAAC,EAC3CO,UAAU,GAAGR,KAAK,EAClBa,QAAQ,GAAGZ,GAAG,EACb;EACD,IAAKD,KAAK,KAAKY,SAAS,IAAIX,GAAG,KAAKW,SAAS,EAAG;IAC/C;EACD;EAEA,MAAME,MAAM,GAAG;IACdjB,OAAO,EAAEA,OAAO,CAACa,KAAK,CAAE,CAAC,EAAEF,UAAW,CAAC;IACvCV,YAAY,EAAEA,YAAY,CAACY,KAAK,CAAE,CAAC,EAAEF,UAAW,CAAC;IACjDT,IAAI,EAAEA,IAAI,CAACW,KAAK,CAAE,CAAC,EAAEF,UAAW;EACjC,CAAC;EACD,MAAMO,KAAK,GAAG;IACblB,OAAO,EAAEA,OAAO,CAACa,KAAK,CAAEG,QAAS,CAAC;IAClCf,YAAY,EAAEA,YAAY,CAACY,KAAK,CAAEG,QAAS,CAAC;IAC5Cd,IAAI,EAAEA,IAAI,CAACW,KAAK,CAAEG,QAAS,CAAC;IAC5Bb,KAAK,EAAE,CAAC;IACRC,GAAG,EAAE;EACN,CAAC;EAED,OAAO,CAAEa,MAAM,EAAEC,KAAK,CAAE;AACzB","ignoreList":[]}

View File

@@ -0,0 +1,36 @@
/**
* Returns an action object used in signalling that format types have been
* added.
* Ignored from documentation as registerFormatType should be used instead from @wordpress/rich-text
*
* @ignore
*
* @param {Array|Object} formatTypes Format types received.
*
* @return {Object} Action object.
*/
export function addFormatTypes(formatTypes) {
return {
type: 'ADD_FORMAT_TYPES',
formatTypes: Array.isArray(formatTypes) ? formatTypes : [formatTypes]
};
}
/**
* Returns an action object used to remove a registered format type.
*
* Ignored from documentation as unregisterFormatType should be used instead from @wordpress/rich-text
*
* @ignore
*
* @param {string|Array} names Format name.
*
* @return {Object} Action object.
*/
export function removeFormatTypes(names) {
return {
type: 'REMOVE_FORMAT_TYPES',
names: Array.isArray(names) ? names : [names]
};
}
//# sourceMappingURL=actions.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["addFormatTypes","formatTypes","type","Array","isArray","removeFormatTypes","names"],"sources":["@wordpress/rich-text/src/store/actions.js"],"sourcesContent":["/**\n * Returns an action object used in signalling that format types have been\n * added.\n * Ignored from documentation as registerFormatType should be used instead from @wordpress/rich-text\n *\n * @ignore\n *\n * @param {Array|Object} formatTypes Format types received.\n *\n * @return {Object} Action object.\n */\nexport function addFormatTypes( formatTypes ) {\n\treturn {\n\t\ttype: 'ADD_FORMAT_TYPES',\n\t\tformatTypes: Array.isArray( formatTypes )\n\t\t\t? formatTypes\n\t\t\t: [ formatTypes ],\n\t};\n}\n\n/**\n * Returns an action object used to remove a registered format type.\n *\n * Ignored from documentation as unregisterFormatType should be used instead from @wordpress/rich-text\n *\n * @ignore\n *\n * @param {string|Array} names Format name.\n *\n * @return {Object} Action object.\n */\nexport function removeFormatTypes( names ) {\n\treturn {\n\t\ttype: 'REMOVE_FORMAT_TYPES',\n\t\tnames: Array.isArray( names ) ? names : [ names ],\n\t};\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,cAAcA,CAAEC,WAAW,EAAG;EAC7C,OAAO;IACNC,IAAI,EAAE,kBAAkB;IACxBD,WAAW,EAAEE,KAAK,CAACC,OAAO,CAAEH,WAAY,CAAC,GACtCA,WAAW,GACX,CAAEA,WAAW;EACjB,CAAC;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASI,iBAAiBA,CAAEC,KAAK,EAAG;EAC1C,OAAO;IACNJ,IAAI,EAAE,qBAAqB;IAC3BI,KAAK,EAAEH,KAAK,CAACC,OAAO,CAAEE,KAAM,CAAC,GAAGA,KAAK,GAAG,CAAEA,KAAK;EAChD,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,27 @@
/**
* WordPress dependencies
*/
import { createReduxStore, register } from '@wordpress/data';
/**
* Internal dependencies
*/
import reducer from './reducer';
import * as selectors from './selectors';
import * as actions from './actions';
const STORE_NAME = 'core/rich-text';
/**
* Store definition for the rich-text namespace.
*
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
*
* @type {Object}
*/
export const store = createReduxStore(STORE_NAME, {
reducer,
selectors,
actions
});
register(store);
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["createReduxStore","register","reducer","selectors","actions","STORE_NAME","store"],"sources":["@wordpress/rich-text/src/store/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createReduxStore, register } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport reducer from './reducer';\nimport * as selectors from './selectors';\nimport * as actions from './actions';\n\nconst STORE_NAME = 'core/rich-text';\n\n/**\n * Store definition for the rich-text namespace.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore\n *\n * @type {Object}\n */\nexport const store = createReduxStore( STORE_NAME, {\n\treducer,\n\tselectors,\n\tactions,\n} );\n\nregister( store );\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,gBAAgB,EAAEC,QAAQ,QAAQ,iBAAiB;;AAE5D;AACA;AACA;AACA,OAAOC,OAAO,MAAM,WAAW;AAC/B,OAAO,KAAKC,SAAS,MAAM,aAAa;AACxC,OAAO,KAAKC,OAAO,MAAM,WAAW;AAEpC,MAAMC,UAAU,GAAG,gBAAgB;;AAEnC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,KAAK,GAAGN,gBAAgB,CAAEK,UAAU,EAAE;EAClDH,OAAO;EACPC,SAAS;EACTC;AACD,CAAE,CAAC;AAEHH,QAAQ,CAAEK,KAAM,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,33 @@
/**
* WordPress dependencies
*/
import { combineReducers } from '@wordpress/data';
/**
* Reducer managing the format types
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function formatTypes(state = {}, action) {
switch (action.type) {
case 'ADD_FORMAT_TYPES':
return {
...state,
// Key format types by their name.
...action.formatTypes.reduce((newFormatTypes, type) => ({
...newFormatTypes,
[type.name]: type
}), {})
};
case 'REMOVE_FORMAT_TYPES':
return Object.fromEntries(Object.entries(state).filter(([key]) => !action.names.includes(key)));
}
return state;
}
export default combineReducers({
formatTypes
});
//# sourceMappingURL=reducer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["combineReducers","formatTypes","state","action","type","reduce","newFormatTypes","name","Object","fromEntries","entries","filter","key","names","includes"],"sources":["@wordpress/rich-text/src/store/reducer.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\n\n/**\n * Reducer managing the format types\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nexport function formatTypes( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'ADD_FORMAT_TYPES':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t// Key format types by their name.\n\t\t\t\t...action.formatTypes.reduce(\n\t\t\t\t\t( newFormatTypes, type ) => ( {\n\t\t\t\t\t\t...newFormatTypes,\n\t\t\t\t\t\t[ type.name ]: type,\n\t\t\t\t\t} ),\n\t\t\t\t\t{}\n\t\t\t\t),\n\t\t\t};\n\t\tcase 'REMOVE_FORMAT_TYPES':\n\t\t\treturn Object.fromEntries(\n\t\t\t\tObject.entries( state ).filter(\n\t\t\t\t\t( [ key ] ) => ! action.names.includes( key )\n\t\t\t\t)\n\t\t\t);\n\t}\n\n\treturn state;\n}\n\nexport default combineReducers( { formatTypes } );\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,eAAe,QAAQ,iBAAiB;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,WAAWA,CAAEC,KAAK,GAAG,CAAC,CAAC,EAAEC,MAAM,EAAG;EACjD,QAASA,MAAM,CAACC,IAAI;IACnB,KAAK,kBAAkB;MACtB,OAAO;QACN,GAAGF,KAAK;QACR;QACA,GAAGC,MAAM,CAACF,WAAW,CAACI,MAAM,CAC3B,CAAEC,cAAc,EAAEF,IAAI,MAAQ;UAC7B,GAAGE,cAAc;UACjB,CAAEF,IAAI,CAACG,IAAI,GAAIH;QAChB,CAAC,CAAE,EACH,CAAC,CACF;MACD,CAAC;IACF,KAAK,qBAAqB;MACzB,OAAOI,MAAM,CAACC,WAAW,CACxBD,MAAM,CAACE,OAAO,CAAER,KAAM,CAAC,CAACS,MAAM,CAC7B,CAAE,CAAEC,GAAG,CAAE,KAAM,CAAET,MAAM,CAACU,KAAK,CAACC,QAAQ,CAAEF,GAAI,CAC7C,CACD,CAAC;EACH;EAEA,OAAOV,KAAK;AACb;AAEA,eAAeF,eAAe,CAAE;EAAEC;AAAY,CAAE,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,161 @@
/**
* WordPress dependencies
*/
import { createSelector } from '@wordpress/data';
/**
* Returns all the available format types.
*
* @param {Object} state Data state.
*
* @example
* ```js
* import { __, sprintf } from '@wordpress/i18n';
* import { store as richTextStore } from '@wordpress/rich-text';
* import { useSelect } from '@wordpress/data';
*
* const ExampleComponent = () => {
* const { getFormatTypes } = useSelect(
* ( select ) => select( richTextStore ),
* []
* );
*
* const availableFormats = getFormatTypes();
*
* return availableFormats ? (
* <ul>
* { availableFormats?.map( ( format ) => (
* <li>{ format.name }</li>
* ) ) }
* </ul>
* ) : (
* __( 'No Formats available' )
* );
* };
* ```
*
* @return {Array} Format types.
*/
export const getFormatTypes = createSelector(state => Object.values(state.formatTypes), state => [state.formatTypes]);
/**
* Returns a format type by name.
*
* @param {Object} state Data state.
* @param {string} name Format type name.
*
* @example
* ```js
* import { __, sprintf } from '@wordpress/i18n';
* import { store as richTextStore } from '@wordpress/rich-text';
* import { useSelect } from '@wordpress/data';
*
* const ExampleComponent = () => {
* const { getFormatType } = useSelect(
* ( select ) => select( richTextStore ),
* []
* );
*
* const boldFormat = getFormatType( 'core/bold' );
*
* return boldFormat ? (
* <ul>
* { Object.entries( boldFormat )?.map( ( [ key, value ] ) => (
* <li>
* { key } : { value }
* </li>
* ) ) }
* </ul>
* ) : (
* __( 'Not Found' )
* ;
* };
* ```
*
* @return {Object?} Format type.
*/
export function getFormatType(state, name) {
return state.formatTypes[name];
}
/**
* Gets the format type, if any, that can handle a bare element (without a
* data-format-type attribute), given the tag name of this element.
*
* @param {Object} state Data state.
* @param {string} bareElementTagName The tag name of the element to find a
* format type for.
*
* @example
* ```js
* import { __, sprintf } from '@wordpress/i18n';
* import { store as richTextStore } from '@wordpress/rich-text';
* import { useSelect } from '@wordpress/data';
*
* const ExampleComponent = () => {
* const { getFormatTypeForBareElement } = useSelect(
* ( select ) => select( richTextStore ),
* []
* );
*
* const format = getFormatTypeForBareElement( 'strong' );
*
* return format && <p>{ sprintf( __( 'Format name: %s' ), format.name ) }</p>;
* }
* ```
*
* @return {?Object} Format type.
*/
export function getFormatTypeForBareElement(state, bareElementTagName) {
const formatTypes = getFormatTypes(state);
return formatTypes.find(({
className,
tagName
}) => {
return className === null && bareElementTagName === tagName;
}) || formatTypes.find(({
className,
tagName
}) => {
return className === null && '*' === tagName;
});
}
/**
* Gets the format type, if any, that can handle an element, given its classes.
*
* @param {Object} state Data state.
* @param {string} elementClassName The classes of the element to find a format
* type for.
*
* @example
* ```js
* import { __, sprintf } from '@wordpress/i18n';
* import { store as richTextStore } from '@wordpress/rich-text';
* import { useSelect } from '@wordpress/data';
*
* const ExampleComponent = () => {
* const { getFormatTypeForClassName } = useSelect(
* ( select ) => select( richTextStore ),
* []
* );
*
* const format = getFormatTypeForClassName( 'has-inline-color' );
*
* return format && <p>{ sprintf( __( 'Format name: %s' ), format.name ) }</p>;
* };
* ```
*
* @return {?Object} Format type.
*/
export function getFormatTypeForClassName(state, elementClassName) {
return getFormatTypes(state).find(({
className
}) => {
if (className === null) {
return false;
}
return ` ${elementClassName} `.indexOf(` ${className} `) >= 0;
});
}
//# sourceMappingURL=selectors.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,282 @@
/**
* Internal dependencies
*/
import { toTree } from './to-tree';
import { createElement } from './create-element';
import { isRangeEqual } from './is-range-equal';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Creates a path as an array of indices from the given root node to the given
* node.
*
* @param {Node} node Node to find the path of.
* @param {HTMLElement} rootNode Root node to find the path from.
* @param {Array} path Initial path to build on.
*
* @return {Array} The path from the root node to the node.
*/
function createPathToNode(node, rootNode, path) {
const parentNode = node.parentNode;
let i = 0;
while (node = node.previousSibling) {
i++;
}
path = [i, ...path];
if (parentNode !== rootNode) {
path = createPathToNode(parentNode, rootNode, path);
}
return path;
}
/**
* Gets a node given a path (array of indices) from the given node.
*
* @param {HTMLElement} node Root node to find the wanted node in.
* @param {Array} path Path (indices) to the wanted node.
*
* @return {Object} Object with the found node and the remaining offset (if any).
*/
function getNodeByPath(node, path) {
path = [...path];
while (node && path.length > 1) {
node = node.childNodes[path.shift()];
}
return {
node,
offset: path[0]
};
}
function append(element, child) {
if (child.html !== undefined) {
return element.innerHTML += child.html;
}
if (typeof child === 'string') {
child = element.ownerDocument.createTextNode(child);
}
const {
type,
attributes
} = child;
if (type) {
child = element.ownerDocument.createElement(type);
for (const key in attributes) {
child.setAttribute(key, attributes[key]);
}
}
return element.appendChild(child);
}
function appendText(node, text) {
node.appendData(text);
}
function getLastChild({
lastChild
}) {
return lastChild;
}
function getParent({
parentNode
}) {
return parentNode;
}
function isText(node) {
return node.nodeType === node.TEXT_NODE;
}
function getText({
nodeValue
}) {
return nodeValue;
}
function remove(node) {
return node.parentNode.removeChild(node);
}
export function toDom({
value,
prepareEditableTree,
isEditableTree = true,
placeholder,
doc = document
}) {
let startPath = [];
let endPath = [];
if (prepareEditableTree) {
value = {
...value,
formats: prepareEditableTree(value)
};
}
/**
* Returns a new instance of a DOM tree upon which RichText operations can be
* applied.
*
* Note: The current implementation will return a shared reference, reset on
* each call to `createEmpty`. Therefore, you should not hold a reference to
* the value to operate upon asynchronously, as it may have unexpected results.
*
* @return {Object} RichText tree.
*/
const createEmpty = () => createElement(doc, '');
const tree = toTree({
value,
createEmpty,
append,
getLastChild,
getParent,
isText,
getText,
remove,
appendText,
onStartIndex(body, pointer) {
startPath = createPathToNode(pointer, body, [pointer.nodeValue.length]);
},
onEndIndex(body, pointer) {
endPath = createPathToNode(pointer, body, [pointer.nodeValue.length]);
},
isEditableTree,
placeholder
});
return {
body: tree,
selection: {
startPath,
endPath
}
};
}
/**
* Create an `Element` tree from a Rich Text value and applies the difference to
* the `Element` tree contained by `current`.
*
* @param {Object} $1 Named arguments.
* @param {RichTextValue} $1.value Value to apply.
* @param {HTMLElement} $1.current The live root node to apply the element tree to.
* @param {Function} [$1.prepareEditableTree] Function to filter editorable formats.
* @param {boolean} [$1.__unstableDomOnly] Only apply elements, no selection.
* @param {string} [$1.placeholder] Placeholder text.
*/
export function apply({
value,
current,
prepareEditableTree,
__unstableDomOnly,
placeholder
}) {
// Construct a new element tree in memory.
const {
body,
selection
} = toDom({
value,
prepareEditableTree,
placeholder,
doc: current.ownerDocument
});
applyValue(body, current);
if (value.start !== undefined && !__unstableDomOnly) {
applySelection(selection, current);
}
}
export function applyValue(future, current) {
let i = 0;
let futureChild;
while (futureChild = future.firstChild) {
const currentChild = current.childNodes[i];
if (!currentChild) {
current.appendChild(futureChild);
} else if (!currentChild.isEqualNode(futureChild)) {
if (currentChild.nodeName !== futureChild.nodeName || currentChild.nodeType === currentChild.TEXT_NODE && currentChild.data !== futureChild.data) {
current.replaceChild(futureChild, currentChild);
} else {
const currentAttributes = currentChild.attributes;
const futureAttributes = futureChild.attributes;
if (currentAttributes) {
let ii = currentAttributes.length;
// Reverse loop because `removeAttribute` on `currentChild`
// changes `currentAttributes`.
while (ii--) {
const {
name
} = currentAttributes[ii];
if (!futureChild.getAttribute(name)) {
currentChild.removeAttribute(name);
}
}
}
if (futureAttributes) {
for (let ii = 0; ii < futureAttributes.length; ii++) {
const {
name,
value
} = futureAttributes[ii];
if (currentChild.getAttribute(name) !== value) {
currentChild.setAttribute(name, value);
}
}
}
applyValue(futureChild, currentChild);
future.removeChild(futureChild);
}
} else {
future.removeChild(futureChild);
}
i++;
}
while (current.childNodes[i]) {
current.removeChild(current.childNodes[i]);
}
}
export function applySelection({
startPath,
endPath
}, current) {
const {
node: startContainer,
offset: startOffset
} = getNodeByPath(current, startPath);
const {
node: endContainer,
offset: endOffset
} = getNodeByPath(current, endPath);
const {
ownerDocument
} = current;
const {
defaultView
} = ownerDocument;
const selection = defaultView.getSelection();
const range = ownerDocument.createRange();
range.setStart(startContainer, startOffset);
range.setEnd(endContainer, endOffset);
const {
activeElement
} = ownerDocument;
if (selection.rangeCount > 0) {
// If the to be added range and the live range are the same, there's no
// need to remove the live range and add the equivalent range.
if (isRangeEqual(range, selection.getRangeAt(0))) {
return;
}
selection.removeAllRanges();
}
selection.addRange(range);
// This function is not intended to cause a shift in focus. Since the above
// selection manipulations may shift focus, ensure that focus is restored to
// its previous state.
if (activeElement !== ownerDocument.activeElement) {
// The `instanceof` checks protect against edge cases where the focused
// element is not of the interface HTMLElement (does not have a `focus`
// or `blur` property).
//
// See: https://github.com/Microsoft/TypeScript/issues/5901#issuecomment-431649653
if (activeElement instanceof defaultView.HTMLElement) {
activeElement.focus();
}
}
}
//# sourceMappingURL=to-dom.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,112 @@
/**
* WordPress dependencies
*/
import { escapeEditableHTML, escapeAttribute, isValidAttributeName } from '@wordpress/escape-html';
/**
* Internal dependencies
*/
import { toTree } from './to-tree';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Create an HTML string from a Rich Text value.
*
* @param {Object} $1 Named argements.
* @param {RichTextValue} $1.value Rich text value.
* @param {boolean} [$1.preserveWhiteSpace] Preserves newlines if true.
*
* @return {string} HTML string.
*/
export function toHTMLString({
value,
preserveWhiteSpace
}) {
const tree = toTree({
value,
preserveWhiteSpace,
createEmpty,
append,
getLastChild,
getParent,
isText,
getText,
remove,
appendText
});
return createChildrenHTML(tree.children);
}
function createEmpty() {
return {};
}
function getLastChild({
children
}) {
return children && children[children.length - 1];
}
function append(parent, object) {
if (typeof object === 'string') {
object = {
text: object
};
}
object.parent = parent;
parent.children = parent.children || [];
parent.children.push(object);
return object;
}
function appendText(object, text) {
object.text += text;
}
function getParent({
parent
}) {
return parent;
}
function isText({
text
}) {
return typeof text === 'string';
}
function getText({
text
}) {
return text;
}
function remove(object) {
const index = object.parent.children.indexOf(object);
if (index !== -1) {
object.parent.children.splice(index, 1);
}
return object;
}
function createElementHTML({
type,
attributes,
object,
children
}) {
let attributeString = '';
for (const key in attributes) {
if (!isValidAttributeName(key)) {
continue;
}
attributeString += ` ${key}="${escapeAttribute(attributes[key])}"`;
}
if (object) {
return `<${type}${attributeString}>`;
}
return `<${type}${attributeString}>${createChildrenHTML(children)}</${type}>`;
}
function createChildrenHTML(children = []) {
return children.map(child => {
if (child.html !== undefined) {
return child.html;
}
return child.text === undefined ? createElementHTML(child) : escapeEditableHTML(child.text);
}).join('');
}
//# sourceMappingURL=to-html-string.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,275 @@
/**
* Internal dependencies
*/
import { getActiveFormats } from './get-active-formats';
import { getFormatType } from './get-format-type';
import { OBJECT_REPLACEMENT_CHARACTER, ZWNBSP } from './special-characters';
function restoreOnAttributes(attributes, isEditableTree) {
if (isEditableTree) {
return attributes;
}
const newAttributes = {};
for (const key in attributes) {
let newKey = key;
if (key.startsWith('data-disable-rich-text-')) {
newKey = key.slice('data-disable-rich-text-'.length);
}
newAttributes[newKey] = attributes[key];
}
return newAttributes;
}
/**
* Converts a format object to information that can be used to create an element
* from (type, attributes and object).
*
* @param {Object} $1 Named parameters.
* @param {string} $1.type The format type.
* @param {string} $1.tagName The tag name.
* @param {Object} $1.attributes The format attributes.
* @param {Object} $1.unregisteredAttributes The unregistered format
* attributes.
* @param {boolean} $1.object Whether or not it is an object
* format.
* @param {boolean} $1.boundaryClass Whether or not to apply a boundary
* class.
* @param {boolean} $1.isEditableTree
*
* @return {Object} Information to be used for element creation.
*/
function fromFormat({
type,
tagName,
attributes,
unregisteredAttributes,
object,
boundaryClass,
isEditableTree
}) {
const formatType = getFormatType(type);
let elementAttributes = {};
if (boundaryClass && isEditableTree) {
elementAttributes['data-rich-text-format-boundary'] = 'true';
}
if (!formatType) {
if (attributes) {
elementAttributes = {
...attributes,
...elementAttributes
};
}
return {
type,
attributes: restoreOnAttributes(elementAttributes, isEditableTree),
object
};
}
elementAttributes = {
...unregisteredAttributes,
...elementAttributes
};
for (const name in attributes) {
const key = formatType.attributes ? formatType.attributes[name] : false;
if (key) {
elementAttributes[key] = attributes[name];
} else {
elementAttributes[name] = attributes[name];
}
}
if (formatType.className) {
if (elementAttributes.class) {
elementAttributes.class = `${formatType.className} ${elementAttributes.class}`;
} else {
elementAttributes.class = formatType.className;
}
}
// When a format is declared as non editable, make it non editable in the
// editor.
if (isEditableTree && formatType.contentEditable === false) {
elementAttributes.contenteditable = 'false';
}
return {
type: tagName || formatType.tagName,
object: formatType.object,
attributes: restoreOnAttributes(elementAttributes, isEditableTree)
};
}
/**
* Checks if both arrays of formats up until a certain index are equal.
*
* @param {Array} a Array of formats to compare.
* @param {Array} b Array of formats to compare.
* @param {number} index Index to check until.
*/
function isEqualUntil(a, b, index) {
do {
if (a[index] !== b[index]) {
return false;
}
} while (index--);
return true;
}
export function toTree({
value,
preserveWhiteSpace,
createEmpty,
append,
getLastChild,
getParent,
isText,
getText,
remove,
appendText,
onStartIndex,
onEndIndex,
isEditableTree,
placeholder
}) {
const {
formats,
replacements,
text,
start,
end
} = value;
const formatsLength = formats.length + 1;
const tree = createEmpty();
const activeFormats = getActiveFormats(value);
const deepestActiveFormat = activeFormats[activeFormats.length - 1];
let lastCharacterFormats;
let lastCharacter;
append(tree, '');
for (let i = 0; i < formatsLength; i++) {
const character = text.charAt(i);
const shouldInsertPadding = isEditableTree && (
// Pad the line if the line is empty.
!lastCharacter ||
// Pad the line if the previous character is a line break, otherwise
// the line break won't be visible.
lastCharacter === '\n');
const characterFormats = formats[i];
let pointer = getLastChild(tree);
if (characterFormats) {
characterFormats.forEach((format, formatIndex) => {
if (pointer && lastCharacterFormats &&
// Reuse the last element if all formats remain the same.
isEqualUntil(characterFormats, lastCharacterFormats, formatIndex)) {
pointer = getLastChild(pointer);
return;
}
const {
type,
tagName,
attributes,
unregisteredAttributes
} = format;
const boundaryClass = isEditableTree && format === deepestActiveFormat;
const parent = getParent(pointer);
const newNode = append(parent, fromFormat({
type,
tagName,
attributes,
unregisteredAttributes,
boundaryClass,
isEditableTree
}));
if (isText(pointer) && getText(pointer).length === 0) {
remove(pointer);
}
pointer = append(newNode, '');
});
}
// If there is selection at 0, handle it before characters are inserted.
if (i === 0) {
if (onStartIndex && start === 0) {
onStartIndex(tree, pointer);
}
if (onEndIndex && end === 0) {
onEndIndex(tree, pointer);
}
}
if (character === OBJECT_REPLACEMENT_CHARACTER) {
const replacement = replacements[i];
if (!replacement) {
continue;
}
const {
type,
attributes,
innerHTML
} = replacement;
const formatType = getFormatType(type);
if (!isEditableTree && type === 'script') {
pointer = append(getParent(pointer), fromFormat({
type: 'script',
isEditableTree
}));
append(pointer, {
html: decodeURIComponent(attributes['data-rich-text-script'])
});
} else if (formatType?.contentEditable === false) {
// For non editable formats, render the stored inner HTML.
pointer = append(getParent(pointer), fromFormat({
...replacement,
isEditableTree,
boundaryClass: start === i && end === i + 1
}));
if (innerHTML) {
append(pointer, {
html: innerHTML
});
}
} else {
pointer = append(getParent(pointer), fromFormat({
...replacement,
object: true,
isEditableTree
}));
}
// Ensure pointer is text node.
pointer = append(getParent(pointer), '');
} else if (!preserveWhiteSpace && character === '\n') {
pointer = append(getParent(pointer), {
type: 'br',
attributes: isEditableTree ? {
'data-rich-text-line-break': 'true'
} : undefined,
object: true
});
// Ensure pointer is text node.
pointer = append(getParent(pointer), '');
} else if (!isText(pointer)) {
pointer = append(getParent(pointer), character);
} else {
appendText(pointer, character);
}
if (onStartIndex && start === i + 1) {
onStartIndex(tree, pointer);
}
if (onEndIndex && end === i + 1) {
onEndIndex(tree, pointer);
}
if (shouldInsertPadding && i === text.length) {
append(getParent(pointer), ZWNBSP);
if (placeholder && text.length === 0) {
append(getParent(pointer), {
type: 'span',
attributes: {
'data-rich-text-placeholder': placeholder,
// Necessary to prevent the placeholder from catching
// selection and being editable.
style: 'pointer-events:none;user-select:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;'
}
});
}
}
lastCharacterFormats = characterFormats;
lastCharacter = character;
}
return tree;
}
//# sourceMappingURL=to-tree.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { speak } from '@wordpress/a11y';
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { getActiveFormat } from './get-active-format';
import { removeFormat } from './remove-format';
import { applyFormat } from './apply-format';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Toggles a format object to a Rich Text value at the current selection.
*
* @param {RichTextValue} value Value to modify.
* @param {RichTextFormat} format Format to apply or remove.
*
* @return {RichTextValue} A new value with the format applied or removed.
*/
export function toggleFormat(value, format) {
if (getActiveFormat(value, format.type)) {
// For screen readers, will announce if formatting control is disabled.
if (format.title) {
// translators: %s: title of the formatting control
speak(sprintf(__('%s removed.'), format.title), 'assertive');
}
return removeFormat(value, format.type);
}
// For screen readers, will announce if formatting control is enabled.
if (format.title) {
// translators: %s: title of the formatting control
speak(sprintf(__('%s applied.'), format.title), 'assertive');
}
return applyFormat(value, format);
}
//# sourceMappingURL=toggle-format.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["speak","__","sprintf","getActiveFormat","removeFormat","applyFormat","toggleFormat","value","format","type","title"],"sources":["@wordpress/rich-text/src/toggle-format.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\n\nimport { speak } from '@wordpress/a11y';\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\n\nimport { getActiveFormat } from './get-active-format';\nimport { removeFormat } from './remove-format';\nimport { applyFormat } from './apply-format';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Toggles a format object to a Rich Text value at the current selection.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {RichTextFormat} format Format to apply or remove.\n *\n * @return {RichTextValue} A new value with the format applied or removed.\n */\nexport function toggleFormat( value, format ) {\n\tif ( getActiveFormat( value, format.type ) ) {\n\t\t// For screen readers, will announce if formatting control is disabled.\n\t\tif ( format.title ) {\n\t\t\t// translators: %s: title of the formatting control\n\t\t\tspeak( sprintf( __( '%s removed.' ), format.title ), 'assertive' );\n\t\t}\n\t\treturn removeFormat( value, format.type );\n\t}\n\t// For screen readers, will announce if formatting control is enabled.\n\tif ( format.title ) {\n\t\t// translators: %s: title of the formatting control\n\t\tspeak( sprintf( __( '%s applied.' ), format.title ), 'assertive' );\n\t}\n\treturn applyFormat( value, format );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,KAAK,QAAQ,iBAAiB;AACvC,SAASC,EAAE,EAAEC,OAAO,QAAQ,iBAAiB;;AAE7C;AACA;AACA;;AAEA,SAASC,eAAe,QAAQ,qBAAqB;AACrD,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,WAAW,QAAQ,gBAAgB;;AAE5C;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAAEC,KAAK,EAAEC,MAAM,EAAG;EAC7C,IAAKL,eAAe,CAAEI,KAAK,EAAEC,MAAM,CAACC,IAAK,CAAC,EAAG;IAC5C;IACA,IAAKD,MAAM,CAACE,KAAK,EAAG;MACnB;MACAV,KAAK,CAAEE,OAAO,CAAED,EAAE,CAAE,aAAc,CAAC,EAAEO,MAAM,CAACE,KAAM,CAAC,EAAE,WAAY,CAAC;IACnE;IACA,OAAON,YAAY,CAAEG,KAAK,EAAEC,MAAM,CAACC,IAAK,CAAC;EAC1C;EACA;EACA,IAAKD,MAAM,CAACE,KAAK,EAAG;IACnB;IACAV,KAAK,CAAEE,OAAO,CAAED,EAAE,CAAE,aAAc,CAAC,EAAEO,MAAM,CAACE,KAAM,CAAC,EAAE,WAAY,CAAC;EACnE;EACA,OAAOL,WAAW,CAAEE,KAAK,EAAEC,MAAO,CAAC;AACpC","ignoreList":[]}

View File

@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":[],"sources":["@wordpress/rich-text/src/types.ts"],"sourcesContent":["/**\n * Stores the type of a rich text format, such as core/bold.\n */\nexport type RichTextFormat = {\n\ttype:\n\t\t| 'core/bold'\n\t\t| 'core/italic'\n\t\t| 'core/link '\n\t\t| 'core/strikethrough'\n\t\t| 'core/image'\n\t\t| string;\n};\n\n/**\n * A list of rich text format types.\n */\nexport type RichTextFormatList = Array< RichTextFormat >;\n\n/**\n * An object which represents a formatted string. The text property contains the\n * text to be formatted, and the formats property contains an array which indicates\n * the formats that are applied to each character in the text. See the main\n * `@wordpress/rich-text` documentation for more detail.\n */\nexport type RichTextValue = {\n\ttext: string;\n\tformats: Array< RichTextFormatList >;\n\treplacements: Array< RichTextFormat >;\n\tstart: number;\n\tend: number;\n};\n"],"mappings":"","ignoreList":[]}

View File

@@ -0,0 +1,31 @@
/**
* WordPress dependencies
*/
import { select, dispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
/** @typedef {import('./register-format-type').WPFormat} WPFormat */
/**
* Unregisters a format.
*
* @param {string} name Format name.
*
* @return {WPFormat|undefined} The previous format value, if it has
* been successfully unregistered;
* otherwise `undefined`.
*/
export function unregisterFormatType(name) {
const oldFormat = select(richTextStore).getFormatType(name);
if (!oldFormat) {
window.console.error(`Format ${name} is not registered.`);
return;
}
dispatch(richTextStore).removeFormatTypes(name);
return oldFormat;
}
//# sourceMappingURL=unregister-format-type.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["select","dispatch","store","richTextStore","unregisterFormatType","name","oldFormat","getFormatType","window","console","error","removeFormatTypes"],"sources":["@wordpress/rich-text/src/unregister-format-type.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { select, dispatch } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as richTextStore } from './store';\n\n/** @typedef {import('./register-format-type').WPFormat} WPFormat */\n\n/**\n * Unregisters a format.\n *\n * @param {string} name Format name.\n *\n * @return {WPFormat|undefined} The previous format value, if it has\n * been successfully unregistered;\n * otherwise `undefined`.\n */\nexport function unregisterFormatType( name ) {\n\tconst oldFormat = select( richTextStore ).getFormatType( name );\n\n\tif ( ! oldFormat ) {\n\t\twindow.console.error( `Format ${ name } is not registered.` );\n\t\treturn;\n\t}\n\n\tdispatch( richTextStore ).removeFormatTypes( name );\n\n\treturn oldFormat;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,MAAM,EAAEC,QAAQ,QAAQ,iBAAiB;;AAElD;AACA;AACA;AACA,SAASC,KAAK,IAAIC,aAAa,QAAQ,SAAS;;AAEhD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,oBAAoBA,CAAEC,IAAI,EAAG;EAC5C,MAAMC,SAAS,GAAGN,MAAM,CAAEG,aAAc,CAAC,CAACI,aAAa,CAAEF,IAAK,CAAC;EAE/D,IAAK,CAAEC,SAAS,EAAG;IAClBE,MAAM,CAACC,OAAO,CAACC,KAAK,CAAG,UAAUL,IAAM,qBAAqB,CAAC;IAC7D;EACD;EAEAJ,QAAQ,CAAEE,aAAc,CAAC,CAACQ,iBAAiB,CAAEN,IAAK,CAAC;EAEnD,OAAOC,SAAS;AACjB","ignoreList":[]}

View File

@@ -0,0 +1,56 @@
/**
* Internal dependencies
*/
import { isFormatEqual } from './is-format-equal';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Efficiently updates all the formats from `start` (including) until `end`
* (excluding) with the active formats. Mutates `value`.
*
* @param {Object} $1 Named paramentes.
* @param {RichTextValue} $1.value Value te update.
* @param {number} $1.start Index to update from.
* @param {number} $1.end Index to update until.
* @param {Array} $1.formats Replacement formats.
*
* @return {RichTextValue} Mutated value.
*/
export function updateFormats({
value,
start,
end,
formats
}) {
// Start and end may be switched in case of delete.
const min = Math.min(start, end);
const max = Math.max(start, end);
const formatsBefore = value.formats[min - 1] || [];
const formatsAfter = value.formats[max] || [];
// First, fix the references. If any format right before or after are
// equal, the replacement format should use the same reference.
value.activeFormats = formats.map((format, index) => {
if (formatsBefore[index]) {
if (isFormatEqual(format, formatsBefore[index])) {
return formatsBefore[index];
}
} else if (formatsAfter[index]) {
if (isFormatEqual(format, formatsAfter[index])) {
return formatsAfter[index];
}
}
return format;
});
while (--end >= start) {
if (value.activeFormats.length > 0) {
value.formats[end] = value.activeFormats;
} else {
delete value.formats[end];
}
}
return value;
}
//# sourceMappingURL=update-formats.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isFormatEqual","updateFormats","value","start","end","formats","min","Math","max","formatsBefore","formatsAfter","activeFormats","map","format","index","length"],"sources":["@wordpress/rich-text/src/update-formats.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { isFormatEqual } from './is-format-equal';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Efficiently updates all the formats from `start` (including) until `end`\n * (excluding) with the active formats. Mutates `value`.\n *\n * @param {Object} $1 Named paramentes.\n * @param {RichTextValue} $1.value Value te update.\n * @param {number} $1.start Index to update from.\n * @param {number} $1.end Index to update until.\n * @param {Array} $1.formats Replacement formats.\n *\n * @return {RichTextValue} Mutated value.\n */\nexport function updateFormats( { value, start, end, formats } ) {\n\t// Start and end may be switched in case of delete.\n\tconst min = Math.min( start, end );\n\tconst max = Math.max( start, end );\n\tconst formatsBefore = value.formats[ min - 1 ] || [];\n\tconst formatsAfter = value.formats[ max ] || [];\n\n\t// First, fix the references. If any format right before or after are\n\t// equal, the replacement format should use the same reference.\n\tvalue.activeFormats = formats.map( ( format, index ) => {\n\t\tif ( formatsBefore[ index ] ) {\n\t\t\tif ( isFormatEqual( format, formatsBefore[ index ] ) ) {\n\t\t\t\treturn formatsBefore[ index ];\n\t\t\t}\n\t\t} else if ( formatsAfter[ index ] ) {\n\t\t\tif ( isFormatEqual( format, formatsAfter[ index ] ) ) {\n\t\t\t\treturn formatsAfter[ index ];\n\t\t\t}\n\t\t}\n\n\t\treturn format;\n\t} );\n\n\twhile ( --end >= start ) {\n\t\tif ( value.activeFormats.length > 0 ) {\n\t\t\tvalue.formats[ end ] = value.activeFormats;\n\t\t} else {\n\t\t\tdelete value.formats[ end ];\n\t\t}\n\t}\n\n\treturn value;\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,aAAa,QAAQ,mBAAmB;;AAEjD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,aAAaA,CAAE;EAAEC,KAAK;EAAEC,KAAK;EAAEC,GAAG;EAAEC;AAAQ,CAAC,EAAG;EAC/D;EACA,MAAMC,GAAG,GAAGC,IAAI,CAACD,GAAG,CAAEH,KAAK,EAAEC,GAAI,CAAC;EAClC,MAAMI,GAAG,GAAGD,IAAI,CAACC,GAAG,CAAEL,KAAK,EAAEC,GAAI,CAAC;EAClC,MAAMK,aAAa,GAAGP,KAAK,CAACG,OAAO,CAAEC,GAAG,GAAG,CAAC,CAAE,IAAI,EAAE;EACpD,MAAMI,YAAY,GAAGR,KAAK,CAACG,OAAO,CAAEG,GAAG,CAAE,IAAI,EAAE;;EAE/C;EACA;EACAN,KAAK,CAACS,aAAa,GAAGN,OAAO,CAACO,GAAG,CAAE,CAAEC,MAAM,EAAEC,KAAK,KAAM;IACvD,IAAKL,aAAa,CAAEK,KAAK,CAAE,EAAG;MAC7B,IAAKd,aAAa,CAAEa,MAAM,EAAEJ,aAAa,CAAEK,KAAK,CAAG,CAAC,EAAG;QACtD,OAAOL,aAAa,CAAEK,KAAK,CAAE;MAC9B;IACD,CAAC,MAAM,IAAKJ,YAAY,CAAEI,KAAK,CAAE,EAAG;MACnC,IAAKd,aAAa,CAAEa,MAAM,EAAEH,YAAY,CAAEI,KAAK,CAAG,CAAC,EAAG;QACrD,OAAOJ,YAAY,CAAEI,KAAK,CAAE;MAC7B;IACD;IAEA,OAAOD,MAAM;EACd,CAAE,CAAC;EAEH,OAAQ,EAAET,GAAG,IAAID,KAAK,EAAG;IACxB,IAAKD,KAAK,CAACS,aAAa,CAACI,MAAM,GAAG,CAAC,EAAG;MACrCb,KAAK,CAACG,OAAO,CAAED,GAAG,CAAE,GAAGF,KAAK,CAACS,aAAa;IAC3C,CAAC,MAAM;MACN,OAAOT,KAAK,CAACG,OAAO,CAAED,GAAG,CAAE;IAC5B;EACD;EAEA,OAAOF,KAAK;AACb","ignoreList":[]}