UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 1.92 kB
{ "version": 3, "sources": ["../../src/utils/selection.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { RichTextData } from '@wordpress/rich-text';\n\n/**\n * A robust way to retain selection position through various\n * transforms is to insert a special character at the position and\n * then recover it.\n */\nexport const START_OF_SELECTED_AREA = '\\u0086';\n\n/**\n * Retrieve the block attribute that contains the selection position.\n *\n * @param {Object} blockAttributes Block attributes.\n * @return {string|void} The name of the block attribute that was previously selected.\n */\nexport function retrieveSelectedAttribute( blockAttributes ) {\n\tif ( ! blockAttributes ) {\n\t\treturn;\n\t}\n\n\treturn Object.keys( blockAttributes ).find( ( name ) => {\n\t\tconst value = blockAttributes[ name ];\n\t\treturn (\n\t\t\t( typeof value === 'string' || value instanceof RichTextData ) &&\n\t\t\t// To do: refactor this to use rich text's selection instead, so we\n\t\t\t// no longer have to use on this hack inserting a special character.\n\t\t\tvalue.toString().indexOf( START_OF_SELECTED_AREA ) !== -1\n\t\t);\n\t} );\n}\n\nexport function findRichTextAttributeKey( blockType ) {\n\tfor ( const [ key, value ] of Object.entries( blockType.attributes ) ) {\n\t\tif ( value.source === 'rich-text' || value.source === 'html' ) {\n\t\t\treturn key;\n\t\t}\n\t}\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAA6B;AAOtB,IAAM,yBAAyB;AAQ/B,SAAS,0BAA2B,iBAAkB;AAC5D,MAAK,CAAE,iBAAkB;AACxB;AAAA,EACD;AAEA,SAAO,OAAO,KAAM,eAAgB,EAAE,KAAM,CAAE,SAAU;AACvD,UAAM,QAAQ,gBAAiB,IAAK;AACpC,YACG,OAAO,UAAU,YAAY,iBAAiB;AAAA;AAAA,IAGhD,MAAM,SAAS,EAAE,QAAS,sBAAuB,MAAM;AAAA,EAEzD,CAAE;AACH;AAEO,SAAS,yBAA0B,WAAY;AACrD,aAAY,CAAE,KAAK,KAAM,KAAK,OAAO,QAAS,UAAU,UAAW,GAAI;AACtE,QAAK,MAAM,WAAW,eAAe,MAAM,WAAW,QAAS;AAC9D,aAAO;AAAA,IACR;AAAA,EACD;AACD;", "names": [] }