roosterjs-content-model-api 9.26.0 → 9.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/lib/index.d.ts +1 -0
  2. package/lib/index.js +4 -2
  3. package/lib/index.js.map +1 -1
  4. package/lib/modelApi/common/queryContentModelBlocks.d.ts +3 -2
  5. package/lib/modelApi/common/queryContentModelBlocks.js +15 -4
  6. package/lib/modelApi/common/queryContentModelBlocks.js.map +1 -1
  7. package/lib/publicApi/link/insertLink.js +3 -2
  8. package/lib/publicApi/link/insertLink.js.map +1 -1
  9. package/lib/publicApi/utils/createEditorContextForEntity.d.ts +8 -0
  10. package/lib/publicApi/utils/createEditorContextForEntity.js +30 -0
  11. package/lib/publicApi/utils/createEditorContextForEntity.js.map +1 -0
  12. package/lib/publicApi/utils/formatSegmentWithContentModel.js +4 -21
  13. package/lib/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
  14. package/lib-amd/index.d.ts +1 -0
  15. package/lib-amd/index.js +3 -2
  16. package/lib-amd/index.js.map +1 -1
  17. package/lib-amd/modelApi/common/queryContentModelBlocks.d.ts +3 -2
  18. package/lib-amd/modelApi/common/queryContentModelBlocks.js +15 -5
  19. package/lib-amd/modelApi/common/queryContentModelBlocks.js.map +1 -1
  20. package/lib-amd/publicApi/link/insertLink.js +3 -2
  21. package/lib-amd/publicApi/link/insertLink.js.map +1 -1
  22. package/lib-amd/publicApi/utils/createEditorContextForEntity.d.ts +8 -0
  23. package/lib-amd/publicApi/utils/createEditorContextForEntity.js +31 -0
  24. package/lib-amd/publicApi/utils/createEditorContextForEntity.js.map +1 -0
  25. package/lib-amd/publicApi/utils/formatSegmentWithContentModel.js +3 -21
  26. package/lib-amd/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
  27. package/lib-mjs/index.d.ts +1 -0
  28. package/lib-mjs/index.js +1 -0
  29. package/lib-mjs/index.js.map +1 -1
  30. package/lib-mjs/modelApi/common/queryContentModelBlocks.d.ts +3 -2
  31. package/lib-mjs/modelApi/common/queryContentModelBlocks.js +15 -4
  32. package/lib-mjs/modelApi/common/queryContentModelBlocks.js.map +1 -1
  33. package/lib-mjs/publicApi/link/insertLink.js +3 -2
  34. package/lib-mjs/publicApi/link/insertLink.js.map +1 -1
  35. package/lib-mjs/publicApi/utils/createEditorContextForEntity.d.ts +8 -0
  36. package/lib-mjs/publicApi/utils/createEditorContextForEntity.js +26 -0
  37. package/lib-mjs/publicApi/utils/createEditorContextForEntity.js.map +1 -0
  38. package/lib-mjs/publicApi/utils/formatSegmentWithContentModel.js +3 -20
  39. package/lib-mjs/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
  40. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"file":"queryContentModelBlocks.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/common/queryContentModelBlocks.ts"],"names":[],"mappings":";;;;IAOA;;;;;;OAMG;IACH,SAAgB,uBAAuB,CACnC,KAAqC,EACrC,IAAkE,EAClE,MAAqC,EACrC,aAAuB;;QAEvB,IAAM,QAAQ,GAAQ,EAAE,CAAC;QACzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAC1C,IAAI,aAAa,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;gBACtC,OAAO,QAAQ,CAAC;aACnB;YACD,IAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;YAE9B,QAAQ,KAAK,CAAC,SAAS,EAAE;gBACrB,KAAK,WAAW,CAAC;gBACjB,KAAK,SAAS,CAAC;gBACf,KAAK,QAAQ;oBACT,IAAI,mBAAmB,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE;wBAC1C,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;qBACxB;oBACD,MA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["import type {\n ContentModelBlockType,\n ReadonlyContentModelBlock,\n ReadonlyContentModelBlockBase,\n ReadonlyContentModelBlockGroup,\n} from 'roosterjs-content-model-types';\n\n/**\n * Query content model blocks\n * @param group The block group to query\n * @param type The type of block to query\n * @param filter Optional selector to filter the blocks\n * @param findFirstOnly True to return the first block only, false to return all blocks\n */\nexport function queryContentModelBlocks<T extends ReadonlyContentModelBlock>(\n group: ReadonlyContentModelBlockGroup,\n type: T extends ReadonlyContentModelBlockBase<infer U> ? U : never,\n filter?: (element: T) => element is T,\n findFirstOnly?: boolean\n): T[] {\n const elements: T[] = [];\n for (let i = 0; i < group.blocks.length; i++) {\n if (findFirstOnly && elements.length > 0) {\n return elements;\n }\n const block = group.blocks[i];\n\n switch (block.blockType) {\n case 'Paragraph':\n case 'Divider':\n case 'Entity':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n break;\n case 'BlockGroup':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n const results = queryContentModelBlocks<T>(block, type, filter, findFirstOnly);\n elements.push(...results);\n break;\n case 'Table':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n for (const row of block.rows) {\n for (const cell of row.cells) {\n const results = queryContentModelBlocks<T>(\n cell,\n type,\n filter,\n findFirstOnly\n );\n elements.push(...results);\n }\n }\n break;\n }\n }\n return elements;\n}\n\nfunction isExpectedBlockType<T extends ReadonlyContentModelBlock>(\n block: ReadonlyContentModelBlock,\n type: ContentModelBlockType,\n filter?: (element: T) => element is T\n): block is T {\n return isBlockType<T>(block, type) && (!filter || filter(block));\n}\n\nfunction isBlockType<T extends ReadonlyContentModelBlock>(\n block: ReadonlyContentModelBlock,\n type: ContentModelBlockType\n): block is T {\n return block.blockType == type;\n}\n"]}
1
+ {"version":3,"file":"queryContentModelBlocks.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/common/queryContentModelBlocks.ts"],"names":[],"mappings":";;;;IAUA;;;;;;;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C,IAAI,CAAC,KAAK,CAAC,CAAC;qBACxB;oBACD,IAAM,OAAO,GAAG,uBAAuB,CACnC,KAAK,EACL,IAAI,EACJ,MAAM,EACN,aAAa,EACb,kBAAkB,CACrB,CAAC;oBACF,QAAQ,CAAC,IAAI,OAAb,QAAQ,qDAAS,OAAO,WAAE;oBAC1B,MAAM;gBACV,KAAK,OAAO;oBACR,IAAI,mBAAmB,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE;wBAC1C,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;qBACxB;;wBACD,KAAkB,IAAA,oBAAA,sBAAA,KAAK,CAAC,IAAI,CAAA,CAAA,gBAAA,4BAAE;4BAAzB,IAAM,GAAG,WAAA;;gCACV,KAAmB,IAAA,oBAAA,sBAAA,GAAG,CAAC,KAAK,CAAA,CAAA,gBAAA,4BAAE;oCAAzB,IAAM,IAAI,WAAA;oCACX,IAAM,SAAO,GAAG,uBAAuB,CACnC,IAAI,EACJ,IAAI,EACJ,MAAM,EACN,aAAa,EACb,kBAAkB,CACrB,CAAC;oCACF,QAAQ,CAAC,IAAI,OAAb,QAAQ,qDAAS,SAAO,WAAE;iCAC7B;;;;;;;;;yBACJ;;;;;;;;;oBACD,MAAM;aACb;SACJ;QACD,OAAO,QAAQ,CAAC;IACpB,CAAC;IAtED,0DAsEC;IAED,SAAS,mBAAmB,CACxB,KAAgC,EAChC,IAA2B,EAC3B,MAAqC;QAErC,OAAO,WAAW,CAAI,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACrE,CAAC;IAED,SAAS,WAAW,CAChB,KAAgC,EAChC,IAA2B;QAE3B,OAAO,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC;IACnC,CAAC","sourcesContent":["import { createDomToModelContext, domToContentModel } from 'roosterjs-content-model-dom';\nimport type {\n ContentModelBlockType,\n ContentModelEntity,\n EditorContext,\n ReadonlyContentModelBlock,\n ReadonlyContentModelBlockBase,\n ReadonlyContentModelBlockGroup,\n} from 'roosterjs-content-model-types';\n\n/**\n * Query content model blocks\n * @param group The block group to query\n * @param type The type of block to query\n * @param filter Optional selector to filter the blocks\n * @param findFirstOnly True to return the first block only, false to return all blocks\n * @param shouldExpandEntity Optional function to determine if an entity's children should be recursively queried, should return a EditorContext if the entity should be expanded, or null if not\n */\nexport function queryContentModelBlocks<T extends ReadonlyContentModelBlock>(\n group: ReadonlyContentModelBlockGroup,\n type: T extends ReadonlyContentModelBlockBase<infer U> ? U : never,\n filter?: (element: T) => element is T,\n findFirstOnly?: boolean,\n shouldExpandEntity?: (entity: ContentModelEntity) => EditorContext | null\n): T[] {\n const elements: T[] = [];\n for (let i = 0; i < group.blocks.length; i++) {\n if (findFirstOnly && elements.length > 0) {\n return elements;\n }\n const block = group.blocks[i];\n\n switch (block.blockType) {\n case 'Paragraph':\n case 'Divider':\n case 'Entity':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n if (block.blockType == 'Entity' && shouldExpandEntity) {\n const editorContext = shouldExpandEntity(block);\n if (editorContext) {\n const context = createDomToModelContext(editorContext);\n const model = domToContentModel(block.wrapper, context);\n const results = queryContentModelBlocks<T>(\n model,\n type,\n filter,\n findFirstOnly,\n shouldExpandEntity\n );\n elements.push(...results);\n }\n }\n break;\n case 'BlockGroup':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n const results = queryContentModelBlocks<T>(\n block,\n type,\n filter,\n findFirstOnly,\n shouldExpandEntity\n );\n elements.push(...results);\n break;\n case 'Table':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n for (const row of block.rows) {\n for (const cell of row.cells) {\n const results = queryContentModelBlocks<T>(\n cell,\n type,\n filter,\n findFirstOnly,\n shouldExpandEntity\n );\n elements.push(...results);\n }\n }\n break;\n }\n }\n return elements;\n}\n\nfunction isExpectedBlockType<T extends ReadonlyContentModelBlock>(\n block: ReadonlyContentModelBlock,\n type: ContentModelBlockType,\n filter?: (element: T) => element is T\n): block is T {\n return isBlockType<T>(block, type) && (!filter || filter(block));\n}\n\nfunction isBlockType<T extends ReadonlyContentModelBlock>(\n block: ReadonlyContentModelBlock,\n type: ContentModelBlockType\n): block is T {\n return block.blockType == type;\n}\n"]}
@@ -36,8 +36,9 @@ define(["require", "exports", "tslib", "../../modelApi/selection/adjustTrailingS
36
36
  .map(function (x) { return (x.segmentType == 'Text' ? x.text : ''); })
37
37
  .join('');
38
38
  var text = displayText || originalText || '';
39
- if (segments.some(function (x) { return x.segmentType != 'SelectionMarker'; }) &&
40
- originalText == text) {
39
+ if ((segments.some(function (x) { return x.segmentType != 'SelectionMarker'; }) &&
40
+ originalText == text) ||
41
+ (segments.length == 1 && segments[0].segmentType == 'Image')) {
41
42
  segments.forEach(function (x) {
42
43
  var link = createLink(linkUrl_1, anchorTitle, target, x.segmentType == 'Text');
43
44
  (0, roosterjs_content_model_dom_1.addLink)(x, link);
@@ -1 +1 @@
1
- {"version":3,"file":"insertLink.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/link/insertLink.ts"],"names":[],"mappings":";;;;IAaA,4BAA4B;IAC5B,IAAM,SAAS,GAAG,cAAc,CAAC;IACjC,wCAAwC;IACxC,IAAM,YAAY,GAAG,cAAc,CAAC;IACpC,sDAAsD;IACtD,IAAM,SAAS,GAAG,SAAS,CAAC;IAE5B;;;;;;;;;;;;OAYG;IACH,SAAgB,UAAU,CACtB,MAAe,EACf,IAAY,EACZ,WAAoB,EACpB,WAAoB,EACpB,MAAe;QAEf,MAAM,CAAC,KAAK,EAAE,CAAC;QAEf,IAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QAC1C,IAAI,GAAG,EAAE;YACL,IAAM,UAAQ,GAAG,IAAA,qBAAS,EAAC,GAAG,CAAC,CAAC;YAChC,IAAM,SAAO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IAAM,UAAU,GAAG,UACf,GAAW,EACX,WAAoB,EACpB,MAAe,EACf,SAAyB;QAAzB,0BAAA,EAAA,gBAAyB;QAEzB,OAAO;YACH,OAAO,EAAE,EAAE;YACX,MAAM,EAAE;gBACJ,IAAI,EAAE,GAAG;gBACT,WAAW,aAAA;gBACX,MAAM,QAAA;gBACN,SAAS,EAAE,SAAS;aACvB;SACJ,CAAC;IACN,CAAC,CAAC;IAEF,SAAS,eAAe,CAAC,GAAW;QAChC,IAAI,CAAC,GAAG,EAAE;YACN,OAAO,GAAG,CAAC;SACd;QAED,4BAA4B;QAC5B,oEAAoE;QACpE,iEAAiE;QACjE,qDAAqD;QACrD,+BAA+B;QAC/B,IAAI,MAAM,GAAG,EAAE,CAAC;QAChB,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE;YAC3B,IAAI,GAAG,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE;gBAC/B,MAAM,GAAG,SAAS,CAAC;aACtB;iBAAM,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;gBACnC,MAAM,GAAG,QAAQ,CAAC;aACrB;iBAAM;gBACH,sBAAsB;gBACtB,MAAM,GAAG,SAAS,CAAC;aACtB;SACJ;QAED,OAAO,MAAM,GAAG,GAAG,CAAC;IACxB,CAAC;IAED,SAAS,QAAQ,CAAC,IAAY;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IAChE,CAAC","sourcesContent":["import { adjustTrailingSpaceSelection } from '../../modelApi/selection/adjustTrailingSpaceSelection';\nimport { matchLink } from '../../modelApi/link/matchLink';\nimport {\n addLink,\n addSegment,\n ChangeSource,\n createContentModelDocument,\n createText,\n getSelectedSegments,\n mergeModel,\n} from 'roosterjs-content-model-dom';\nimport type { ContentModelLink, IEditor } from 'roosterjs-content-model-types';\n\n// Regex matching Uri scheme\nconst URI_REGEX = /^[a-zA-Z]+:/i;\n// Regex matching begin of email address\nconst MAILTO_REGEX = /^[\\w.%+-]+@/i;\n// Regex matching begin of ftp, i.e. ftp.microsoft.com\nconst FTP_REGEX = /^ftp\\./i;\n\n/**\n * Insert a hyperlink at cursor.\n * When there is a selection, hyperlink will be applied to the selection,\n * otherwise a hyperlink will be inserted to the cursor position.\n * @param editor Editor object\n * @param link Link address, can be http(s), mailto, notes, file, unc, ftp, news, telnet, gopher, wais.\n * When protocol is not specified, a best matched protocol will be predicted.\n * @param anchorTitle Optional alt text of the link, will be shown when hover on the link\n * @param displayText Optional display text for the link.\n * @param target Optional display target for the link (\"_blank\"|\"_self\"|\"_parent\"|\"_top\"|\"{framename}\")\n * If specified, the display text of link will be replaced with this text.\n * If not specified and there wasn't a link, the link url will be used as display text.\n */\nexport function insertLink(\n editor: IEditor,\n link: string,\n anchorTitle?: string,\n displayText?: string,\n target?: string\n) {\n editor.focus();\n\n const url = (checkXss(link) || '').trim();\n if (url) {\n const linkData = matchLink(url);\n const linkUrl = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);\n const links: ContentModelLink[] = [];\n let anchorNode: Node | undefined;\n\n editor.formatContentModel(\n (model, context) => {\n const segments = getSelectedSegments(\n model,\n false /*includingFormatHolder*/,\n true /*mutate*/\n );\n const originalText = segments\n .map(x => (x.segmentType == 'Text' ? x.text : ''))\n .join('');\n const text = displayText || originalText || '';\n\n if (\n segments.some(x => x.segmentType != 'SelectionMarker') &&\n originalText == text\n ) {\n segments.forEach(x => {\n const link = createLink(\n linkUrl,\n anchorTitle,\n target,\n x.segmentType == 'Text'\n );\n addLink(x, link);\n if (x.link) {\n links.push(x.link);\n }\n });\n } else if (\n segments.every(x => x.segmentType == 'SelectionMarker') ||\n (!!text && text != originalText)\n ) {\n const segment = createText(text || (linkData ? linkData.originalUrl : url), {\n ...segments[0]?.format,\n ...editor.getPendingFormat(),\n });\n const doc = createContentModelDocument();\n const link = createLink(linkUrl, anchorTitle, target);\n\n addLink(segment, link);\n addSegment(doc, segment);\n\n if (segment.link) {\n links.push(segment.link);\n }\n\n mergeModel(model, doc, context, {\n mergeFormat: 'mergeAll',\n });\n }\n\n adjustTrailingSpaceSelection(model);\n return segments.length > 0;\n },\n {\n changeSource: ChangeSource.CreateLink,\n onNodeCreated: (modelElement, node) => {\n if (!anchorNode && links.indexOf(modelElement as ContentModelLink) >= 0) {\n anchorNode = node;\n }\n },\n getChangeData: () => anchorNode,\n apiName: 'insertLink',\n }\n );\n }\n}\n\nconst createLink = (\n url: string,\n anchorTitle?: string,\n target?: string,\n underline: boolean = true\n): ContentModelLink => {\n return {\n dataset: {},\n format: {\n href: url,\n anchorTitle,\n target,\n underline: underline,\n },\n };\n};\n\nfunction applyLinkPrefix(url: string): string {\n if (!url) {\n return url;\n }\n\n // Add link prefix per rule:\n // (a) if the url always starts with a URI scheme, leave it as it is\n // (b) if the url is an email address, xxx@... add mailto: prefix\n // (c) if the url starts with ftp., add ftp:// prefix\n // (d) rest, add http:// prefix\n let prefix = '';\n if (url.search(URI_REGEX) < 0) {\n if (url.search(MAILTO_REGEX) == 0) {\n prefix = 'mailto:';\n } else if (url.search(FTP_REGEX) == 0) {\n prefix = 'ftp://';\n } else {\n // fallback to http://\n prefix = 'http://';\n }\n }\n\n return prefix + url;\n}\n\nfunction checkXss(link: string): string {\n return link.match(/s\\n*c\\n*r\\n*i\\n*p\\n*t\\n*:/i) ? '' : link;\n}\n"]}
1
+ {"version":3,"file":"insertLink.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/link/insertLink.ts"],"names":[],"mappings":";;;;IAaA,4BAA4B;IAC5B,IAAM,SAAS,GAAG,cAAc,CAAC;IACjC,wCAAwC;IACxC,IAAM,YAAY,GAAG,cAAc,CAAC;IACpC,sDAAsD;IACtD,IAAM,SAAS,GAAG,SAAS,CAAC;IAE5B;;;;;;;;;;;;OAYG;IACH,SAAgB,UAAU,C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O,MAAM,GAAG,GAAG,CAAC;IACxB,CAAC;IAED,SAAS,QAAQ,CAAC,IAAY;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IAChE,CAAC","sourcesContent":["import { adjustTrailingSpaceSelection } from '../../modelApi/selection/adjustTrailingSpaceSelection';\nimport { matchLink } from '../../modelApi/link/matchLink';\nimport {\n addLink,\n addSegment,\n ChangeSource,\n createContentModelDocument,\n createText,\n getSelectedSegments,\n mergeModel,\n} from 'roosterjs-content-model-dom';\nimport type { ContentModelLink, IEditor } from 'roosterjs-content-model-types';\n\n// Regex matching Uri scheme\nconst URI_REGEX = /^[a-zA-Z]+:/i;\n// Regex matching begin of email address\nconst MAILTO_REGEX = /^[\\w.%+-]+@/i;\n// Regex matching begin of ftp, i.e. ftp.microsoft.com\nconst FTP_REGEX = /^ftp\\./i;\n\n/**\n * Insert a hyperlink at cursor.\n * When there is a selection, hyperlink will be applied to the selection,\n * otherwise a hyperlink will be inserted to the cursor position.\n * @param editor Editor object\n * @param link Link address, can be http(s), mailto, notes, file, unc, ftp, news, telnet, gopher, wais.\n * When protocol is not specified, a best matched protocol will be predicted.\n * @param anchorTitle Optional alt text of the link, will be shown when hover on the link\n * @param displayText Optional display text for the link.\n * @param target Optional display target for the link (\"_blank\"|\"_self\"|\"_parent\"|\"_top\"|\"{framename}\")\n * If specified, the display text of link will be replaced with this text.\n * If not specified and there wasn't a link, the link url will be used as display text.\n */\nexport function insertLink(\n editor: IEditor,\n link: string,\n anchorTitle?: string,\n displayText?: string,\n target?: string\n) {\n editor.focus();\n\n const url = (checkXss(link) || '').trim();\n if (url) {\n const linkData = matchLink(url);\n const linkUrl = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);\n const links: ContentModelLink[] = [];\n let anchorNode: Node | undefined;\n\n editor.formatContentModel(\n (model, context) => {\n const segments = getSelectedSegments(\n model,\n false /*includingFormatHolder*/,\n true /*mutate*/\n );\n\n const originalText = segments\n .map(x => (x.segmentType == 'Text' ? x.text : ''))\n .join('');\n const text = displayText || originalText || '';\n\n if (\n (segments.some(x => x.segmentType != 'SelectionMarker') &&\n originalText == text) ||\n (segments.length == 1 && segments[0].segmentType == 'Image')\n ) {\n segments.forEach(x => {\n const link = createLink(\n linkUrl,\n anchorTitle,\n target,\n x.segmentType == 'Text'\n );\n addLink(x, link);\n if (x.link) {\n links.push(x.link);\n }\n });\n } else if (\n segments.every(x => x.segmentType == 'SelectionMarker') ||\n (!!text && text != originalText)\n ) {\n const segment = createText(text || (linkData ? linkData.originalUrl : url), {\n ...segments[0]?.format,\n ...editor.getPendingFormat(),\n });\n const doc = createContentModelDocument();\n const link = createLink(linkUrl, anchorTitle, target);\n\n addLink(segment, link);\n addSegment(doc, segment);\n\n if (segment.link) {\n links.push(segment.link);\n }\n\n mergeModel(model, doc, context, {\n mergeFormat: 'mergeAll',\n });\n }\n\n adjustTrailingSpaceSelection(model);\n return segments.length > 0;\n },\n {\n changeSource: ChangeSource.CreateLink,\n onNodeCreated: (modelElement, node) => {\n if (!anchorNode && links.indexOf(modelElement as ContentModelLink) >= 0) {\n anchorNode = node;\n }\n },\n getChangeData: () => anchorNode,\n apiName: 'insertLink',\n }\n );\n }\n}\n\nconst createLink = (\n url: string,\n anchorTitle?: string,\n target?: string,\n underline: boolean = true\n): ContentModelLink => {\n return {\n dataset: {},\n format: {\n href: url,\n anchorTitle,\n target,\n underline: underline,\n },\n };\n};\n\nfunction applyLinkPrefix(url: string): string {\n if (!url) {\n return url;\n }\n\n // Add link prefix per rule:\n // (a) if the url always starts with a URI scheme, leave it as it is\n // (b) if the url is an email address, xxx@... add mailto: prefix\n // (c) if the url starts with ftp., add ftp:// prefix\n // (d) rest, add http:// prefix\n let prefix = '';\n if (url.search(URI_REGEX) < 0) {\n if (url.search(MAILTO_REGEX) == 0) {\n prefix = 'mailto:';\n } else if (url.search(FTP_REGEX) == 0) {\n prefix = 'ftp://';\n } else {\n // fallback to http://\n prefix = 'http://';\n }\n }\n\n return prefix + url;\n}\n\nfunction checkXss(link: string): string {\n return link.match(/s\\n*c\\n*r\\n*i\\n*p\\n*t\\n*:/i) ? '' : link;\n}\n"]}
@@ -0,0 +1,8 @@
1
+ import type { ContentModelEntity, EditorContext, IEditor } from 'roosterjs-content-model-types';
2
+ /**
3
+ * Create an EditorContext for an entity
4
+ * @param editor The editor object
5
+ * @param entity The entity to create the context for
6
+ * @returns The generated EditorContext for the entity
7
+ */
8
+ export declare function createEditorContextForEntity(editor: IEditor, entity: ContentModelEntity): EditorContext;
@@ -0,0 +1,31 @@
1
+ define(["require", "exports", "tslib"], function (require, exports, tslib_1) {
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.createEditorContextForEntity = void 0;
5
+ /**
6
+ * Create an EditorContext for an entity
7
+ * @param editor The editor object
8
+ * @param entity The entity to create the context for
9
+ * @returns The generated EditorContext for the entity
10
+ */
11
+ function createEditorContextForEntity(editor, entity) {
12
+ var _a;
13
+ var domHelper = editor.getDOMHelper();
14
+ var context = {
15
+ isDarkMode: editor.isDarkMode(),
16
+ defaultFormat: (0, tslib_1.__assign)({}, entity.format),
17
+ darkColorHandler: editor.getColorManager(),
18
+ addDelimiterForEntity: false,
19
+ allowCacheElement: false,
20
+ domIndexer: undefined,
21
+ zoomScale: domHelper.calculateZoomScale(),
22
+ experimentalFeatures: [],
23
+ };
24
+ if (((_a = editor.getDocument().defaultView) === null || _a === void 0 ? void 0 : _a.getComputedStyle(entity.wrapper).direction) == 'rtl') {
25
+ context.isRootRtl = true;
26
+ }
27
+ return context;
28
+ }
29
+ exports.createEditorContextForEntity = createEditorContextForEntity;
30
+ });
31
+ //# sourceMappingURL=createEditorContextForEntity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createEditorContextForEntity.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/createEditorContextForEntity.ts"],"names":[],"mappings":";;;;IAEA;;;;;OAKG;IACH,SAAgB,4BAA4B,CACxC,MAAe,EACf,MAA0B;;QAE1B,IAAM,SAAS,GAAG,MAAM,CAAC,YAAY,EAAE,CAAC;QACxC,IAAM,OAAO,GAAkB;YAC3B,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE;YAC/B,aAAa,4BAAO,MAAM,CAAC,MAAM,CAAE;YACnC,gBAAgB,EAAE,MAAM,CAAC,eAAe,EAAE;YAC1C,qBAAqB,EAAE,KAAK;YAC5B,iBAAiB,EAAE,KAAK;YACxB,UAAU,EAAE,SAAS;YACrB,SAAS,EAAE,SAAS,CAAC,kBAAkB,EAAE;YACzC,oBAAoB,EAAE,EAAE;SAC3B,CAAC;QAEF,IAAI,CAAA,MAAA,MAAM,CAAC,WAAW,EAAE,CAAC,WAAW,0CAAE,gBAAgB,CAAC,MAAM,CAAC,OAAO,EAAE,SAAS,KAAI,KAAK,EAAE;YACvF,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;SAC5B;QAED,OAAO,OAAO,CAAC;IACnB,CAAC;IArBD,oEAqBC","sourcesContent":["import type { ContentModelEntity, EditorContext, IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Create an EditorContext for an entity\n * @param editor The editor object\n * @param entity The entity to create the context for\n * @returns The generated EditorContext for the entity\n */\nexport function createEditorContextForEntity(\n editor: IEditor,\n entity: ContentModelEntity\n): EditorContext {\n const domHelper = editor.getDOMHelper();\n const context: EditorContext = {\n isDarkMode: editor.isDarkMode(),\n defaultFormat: { ...entity.format },\n darkColorHandler: editor.getColorManager(),\n addDelimiterForEntity: false,\n allowCacheElement: false,\n domIndexer: undefined,\n zoomScale: domHelper.calculateZoomScale(),\n experimentalFeatures: [],\n };\n\n if (editor.getDocument().defaultView?.getComputedStyle(entity.wrapper).direction == 'rtl') {\n context.isRootRtl = true;\n }\n\n return context;\n}\n"]}
@@ -1,4 +1,4 @@
1
- define(["require", "exports", "tslib", "../../modelApi/selection/adjustWordSelection", "roosterjs-content-model-dom"], function (require, exports, tslib_1, adjustWordSelection_1, roosterjs_content_model_dom_1) {
1
+ define(["require", "exports", "tslib", "roosterjs-content-model-dom", "../../modelApi/selection/adjustWordSelection", "./createEditorContextForEntity"], function (require, exports, tslib_1, roosterjs_content_model_dom_1, adjustWordSelection_1, createEditorContextForEntity_1) {
2
2
  "use strict";
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.formatSegmentWithContentModel = void 0;
@@ -75,24 +75,6 @@ define(["require", "exports", "tslib", "../../modelApi/selection/adjustWordSelec
75
75
  });
76
76
  }
77
77
  exports.formatSegmentWithContentModel = formatSegmentWithContentModel;
78
- function createEditorContextForEntity(editor, entity) {
79
- var _a;
80
- var domHelper = editor.getDOMHelper();
81
- var context = {
82
- isDarkMode: editor.isDarkMode(),
83
- defaultFormat: (0, tslib_1.__assign)({}, entity.format),
84
- darkColorHandler: editor.getColorManager(),
85
- addDelimiterForEntity: false,
86
- allowCacheElement: false,
87
- domIndexer: undefined,
88
- zoomScale: domHelper.calculateZoomScale(),
89
- experimentalFeatures: [],
90
- };
91
- if (((_a = editor.getDocument().defaultView) === null || _a === void 0 ? void 0 : _a.getComputedStyle(entity.wrapper).direction) == 'rtl') {
92
- context.isRootRtl = true;
93
- }
94
- return context;
95
- }
96
78
  function expandEntitySelections(editor, entity, formatsAndSegments, modelsFromEntities) {
97
79
  var _a = entity.entityFormat, id = _a.id, type = _a.entityType, isReadonly = _a.isReadonly;
98
80
  if (id && type) {
@@ -105,7 +87,7 @@ define(["require", "exports", "tslib", "../../modelApi/selection/adjustWordSelec
105
87
  editor.triggerEvent('entityOperation', entityOperationEventData);
106
88
  formattableRoots.forEach(function (root) {
107
89
  if (entity.wrapper.contains(root.element)) {
108
- var editorContext = createEditorContextForEntity(editor, entity);
90
+ var editorContext = (0, createEditorContextForEntity_1.createEditorContextForEntity)(editor, entity);
109
91
  var context = (0, roosterjs_content_model_dom_1.createDomToModelContext)(editorContext, root.domToModelOptions);
110
92
  // Treat everything as selected since the parent entity is selected
111
93
  context.isInSelection = true;
@@ -122,7 +104,7 @@ define(["require", "exports", "tslib", "../../modelApi/selection/adjustWordSelec
122
104
  function writeBackEntities(editor, modelsFromEntities) {
123
105
  modelsFromEntities.forEach(function (_a) {
124
106
  var _b = (0, tslib_1.__read)(_a, 3), entity = _b[0], root = _b[1], model = _b[2];
125
- var editorContext = createEditorContextForEntity(editor, entity);
107
+ var editorContext = (0, createEditorContextForEntity_1.createEditorContextForEntity)(editor, entity);
126
108
  var modelToDomContext = (0, roosterjs_content_model_dom_1.createModelToDomContext)(editorContext, root.modelToDomOptions);
127
109
  (0, roosterjs_content_model_dom_1.contentModelToDom)(editor.getDocument(), root.element, model, modelToDomContext);
128
110
  });
@@ -1 +1 @@
1
- {"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";;;;IAsBA;;;;;;;;OAQG;IACH,SAAgB,6BAA6B,CACzC,MAAe,EACf,OAAe,EACf,mBAKS,EACT,uBAIY,EACZ,qBAA+B,EAC/B,mBAAmE;QAEnE,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;YACX,IAAI,oBAAoB,GAAG,IAAA,8DAAgC,EACvD,KAAK,EACL,CAAC,CAAC,qBAAqB,EACvB,IAAI,CAAC,mBAAmB,EACxB,IAAI,CAAC,UAAU,CAClB,CAAC;YACF,IAAI,oBAAoB,GACpB,oBAAoB,CAAC,MAAM,IAAI,CAAC;gBAChC,oBAAoB,CAAC,KAAK,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,IAAI,iBAAiB,EAArC,CAAqC,CAAC,CAAC;YAE3E,0DAA0D;YAC1D,IAAI,oBAAoB,EAAE;gBACtB,IAAM,MAAI,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,C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nE,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;oBAE7B,IAAM,KAAK,GAAG,IAAA,+CAAiB,EAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;oBACvD,IAAM,UAAU,GAAG,IAAA,8DAAgC,EAC/C,KAAK,EACL,KAAK,CAAC,yBAAyB,EAC/B,KAAK,CAAC,mBAAmB,EACzB,IAAI,CAAC,UAAU,CAClB,CAAC;oBAEF,UAAU,CAAC,OAAO,CAAC,UAAA,IAAI;wBACnB,kBAAkB,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;oBAChE,CAAC,CAAC,CAAC;oBAEH,kBAAkB,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;iBAClD;YACL,CAAC,CAAC,CAAC;SACN;IACL,CAAC;IAED,SAAS,iBAAiB,CACtB,MAAe,EACf,kBAAiF;QAEjF,kBAAkB,CAAC,OAAO,CAAC,UAAC,EAAqB;gBAArB,KAAA,0BAAqB,EAApB,MAAM,QAAA,EAAE,IAAI,QAAA,EAAE,KAAK,QAAA;YAC5C,IAAM,aAAa,GAAG,4BAA4B,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YACnE,IAAM,iBAAiB,GAAG,IAAA,qDAAuB,EAAC,aAAa,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;YAEzF,IAAA,+CAAiB,EAAC,MAAM,CAAC,WAAW,EAAE,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC;QACpF,CAAC,CAAC,CAAC;IACP,CAAC","sourcesContent":["import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport {\n contentModelToDom,\n createDomToModelContext,\n createModelToDomContext,\n domToContentModel,\n getSelectedSegmentsAndParagraphs,\n mergeTextSegments,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelDocument,\n ContentModelEntity,\n ContentModelSegmentFormat,\n EditorContext,\n FormattableRoot,\n IEditor,\n PluginEventData,\n ReadonlyContentModelDocument,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the selected segment using Content Model\n * @param editor The editor object\n * @param apiName Name of API this calling this function. This is mostly for logging.\n * @param toggleStyleCallback The callback to format the segment. It will be called with current selected table. If no table is selected, it will not be called.\n * @param segmentHasStyleCallback The callback used for checking if the given segment already has required format\n * @param includingFormatHolder True to also include format holder of list item when search selected segments\n * @param afterFormatCallback A callback to invoke after format is applied to all selected segments and before the change is applied to DOM tree\n */\nexport function formatSegmentWithContentModel(\n editor: IEditor,\n apiName: string,\n toggleStyleCallback: (\n format: ContentModelSegmentFormat,\n isTuringOn: boolean,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => void,\n segmentHasStyleCallback?: (\n format: ContentModelSegmentFormat,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => boolean,\n includingFormatHolder?: boolean,\n afterFormatCallback?: (model: ReadonlyContentModelDocument) => void\n) {\n editor.formatContentModel(\n (model, context) => {\n let segmentAndParagraphs = getSelectedSegmentsAndParagraphs(\n model,\n !!includingFormatHolder,\n true /*includingEntity*/,\n true /*mutate*/\n );\n let isCollapsedSelection =\n segmentAndParagraphs.length >= 1 &&\n segmentAndParagraphs.every(x => x[0].segmentType == 'SelectionMarker');\n\n // 1. adjust selection to a word if selection is collapsed\n if (isCollapsedSelection) {\n const para = segmentAndParagraphs[0][1];\n const path = segmentAndParagraphs[0][2];\n\n segmentAndParagraphs = adjustWordSelection(\n model,\n segmentAndParagraphs[0][0]\n ).map(x => [x, para, path]);\n\n if (segmentAndParagraphs.length > 1) {\n isCollapsedSelection = false;\n }\n }\n\n // 2. expand selection for entities if any\n const formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][] = [];\n const modelsFromEntities: [\n ContentModelEntity,\n FormattableRoot,\n ContentModelDocument\n ][] = [];\n\n segmentAndParagraphs.forEach(item => {\n if (item[0].segmentType == 'Entity') {\n expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities);\n } else {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n }\n });\n\n // 3. check if we should turn format on (when not all selection has the required format already)\n // or off (all selections already have the required format)\n const isTurningOff = segmentHasStyleCallback\n ? formatsAndSegments.every(([format, segment, paragraph]) =>\n segmentHasStyleCallback(format, segment, paragraph)\n )\n : false;\n\n // 4. invoke the callback function to apply the format\n formatsAndSegments.forEach(([format, segment, paragraph]) => {\n toggleStyleCallback(format, !isTurningOff, segment, paragraph);\n });\n\n // 5. after format is applied to all selections, invoke another callback to do some clean up before write the change back\n afterFormatCallback?.(model);\n\n // 6. finally merge segments if possible, to avoid fragmentation\n formatsAndSegments.forEach(([_, __, paragraph]) => {\n if (paragraph) {\n mergeTextSegments(paragraph);\n }\n });\n\n // 7. Write back models that we got from entities (if any)\n writeBackEntities(editor, modelsFromEntities);\n\n // 8. if the selection is still collapsed, it means we didn't actually applied format, set a pending format so it can be applied when user type\n // otherwise, write back to editor\n if (isCollapsedSelection) {\n context.newPendingFormat = segmentAndParagraphs[0][0].format;\n editor.focus();\n return false;\n } else {\n return formatsAndSegments.length > 0;\n }\n },\n {\n apiName,\n }\n );\n}\n\nfunction createEditorContextForEntity(editor: IEditor, entity: ContentModelEntity): EditorContext {\n const domHelper = editor.getDOMHelper();\n const context: EditorContext = {\n isDarkMode: editor.isDarkMode(),\n defaultFormat: { ...entity.format },\n darkColorHandler: editor.getColorManager(),\n addDelimiterForEntity: false,\n allowCacheElement: false,\n domIndexer: undefined,\n zoomScale: domHelper.calculateZoomScale(),\n experimentalFeatures: [],\n };\n\n if (editor.getDocument().defaultView?.getComputedStyle(entity.wrapper).direction == 'rtl') {\n context.isRootRtl = true;\n }\n\n return context;\n}\n\nfunction expandEntitySelections(\n editor: IEditor,\n entity: ContentModelEntity,\n formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][],\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n const { id, entityType: type, isReadonly } = entity.entityFormat;\n\n if (id && type) {\n const formattableRoots: FormattableRoot[] = [];\n const entityOperationEventData: PluginEventData<'entityOperation'> = {\n entity: { id, type, isReadonly: !!isReadonly, wrapper: entity.wrapper },\n operation: 'beforeFormat',\n formattableRoots,\n };\n\n editor.triggerEvent('entityOperation', entityOperationEventData);\n\n formattableRoots.forEach(root => {\n if (entity.wrapper.contains(root.element)) {\n const editorContext = createEditorContextForEntity(editor, entity);\n const context = createDomToModelContext(editorContext, root.domToModelOptions);\n\n // Treat everything as selected since the parent entity is selected\n context.isInSelection = true;\n\n const model = domToContentModel(root.element, context);\n const selections = getSelectedSegmentsAndParagraphs(\n model,\n false /*includingFormatHolder*/,\n false /*includingEntity*/,\n true /*mutate*/\n );\n\n selections.forEach(item => {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n });\n\n modelsFromEntities.push([entity, root, model]);\n }\n });\n }\n}\n\nfunction writeBackEntities(\n editor: IEditor,\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n modelsFromEntities.forEach(([entity, root, model]) => {\n const editorContext = createEditorContextForEntity(editor, entity);\n const modelToDomContext = createModelToDomContext(editorContext, root.modelToDomOptions);\n\n contentModelToDom(editor.getDocument(), root.element, model, modelToDomContext);\n });\n}\n"]}
1
+ {"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";;;;IAsBA;;;;;;;;OAQG;IACH,SAA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;oBAE7B,IAAM,KAAK,GAAG,IAAA,+CAAiB,EAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;oBACvD,IAAM,UAAU,GAAG,IAAA,8DAAgC,EAC/C,KAAK,EACL,KAAK,CAAC,yBAAyB,EAC/B,KAAK,CAAC,mBAAmB,EACzB,IAAI,CAAC,UAAU,CAClB,CAAC;oBAEF,UAAU,CAAC,OAAO,CAAC,UAAA,IAAI;wBACnB,kBAAkB,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;oBAChE,CAAC,CAAC,CAAC;oBAEH,kBAAkB,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;iBAClD;YACL,CAAC,CAAC,CAAC;SACN;IACL,CAAC;IAED,SAAS,iBAAiB,CACtB,MAAe,EACf,kBAAiF;QAEjF,kBAAkB,CAAC,OAAO,CAAC,UAAC,EAAqB;gBAArB,KAAA,0BAAqB,EAApB,MAAM,QAAA,EAAE,IAAI,QAAA,EAAE,KAAK,QAAA;YAC5C,IAAM,aAAa,GAAG,IAAA,2DAA4B,EAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YACnE,IAAM,iBAAiB,GAAG,IAAA,qDAAuB,EAAC,aAAa,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;YAEzF,IAAA,+CAAiB,EAAC,MAAM,CAAC,WAAW,EAAE,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC;QACpF,CAAC,CAAC,CAAC;IACP,CAAC","sourcesContent":["import {\n contentModelToDom,\n createDomToModelContext,\n createModelToDomContext,\n domToContentModel,\n getSelectedSegmentsAndParagraphs,\n mergeTextSegments,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelDocument,\n ContentModelEntity,\n ContentModelSegmentFormat,\n FormattableRoot,\n IEditor,\n PluginEventData,\n ReadonlyContentModelDocument,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\nimport { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport { createEditorContextForEntity } from './createEditorContextForEntity';\n\n/**\n * Invoke a callback to format the selected segment using Content Model\n * @param editor The editor object\n * @param apiName Name of API this calling this function. This is mostly for logging.\n * @param toggleStyleCallback The callback to format the segment. It will be called with current selected table. If no table is selected, it will not be called.\n * @param segmentHasStyleCallback The callback used for checking if the given segment already has required format\n * @param includingFormatHolder True to also include format holder of list item when search selected segments\n * @param afterFormatCallback A callback to invoke after format is applied to all selected segments and before the change is applied to DOM tree\n */\nexport function formatSegmentWithContentModel(\n editor: IEditor,\n apiName: string,\n toggleStyleCallback: (\n format: ContentModelSegmentFormat,\n isTuringOn: boolean,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => void,\n segmentHasStyleCallback?: (\n format: ContentModelSegmentFormat,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => boolean,\n includingFormatHolder?: boolean,\n afterFormatCallback?: (model: ReadonlyContentModelDocument) => void\n) {\n editor.formatContentModel(\n (model, context) => {\n let segmentAndParagraphs = getSelectedSegmentsAndParagraphs(\n model,\n !!includingFormatHolder,\n true /*includingEntity*/,\n true /*mutate*/\n );\n let isCollapsedSelection =\n segmentAndParagraphs.length >= 1 &&\n segmentAndParagraphs.every(x => x[0].segmentType == 'SelectionMarker');\n\n // 1. adjust selection to a word if selection is collapsed\n if (isCollapsedSelection) {\n const para = segmentAndParagraphs[0][1];\n const path = segmentAndParagraphs[0][2];\n\n segmentAndParagraphs = adjustWordSelection(\n model,\n segmentAndParagraphs[0][0]\n ).map(x => [x, para, path]);\n\n if (segmentAndParagraphs.length > 1) {\n isCollapsedSelection = false;\n }\n }\n\n // 2. expand selection for entities if any\n const formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][] = [];\n const modelsFromEntities: [\n ContentModelEntity,\n FormattableRoot,\n ContentModelDocument\n ][] = [];\n\n segmentAndParagraphs.forEach(item => {\n if (item[0].segmentType == 'Entity') {\n expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities);\n } else {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n }\n });\n\n // 3. check if we should turn format on (when not all selection has the required format already)\n // or off (all selections already have the required format)\n const isTurningOff = segmentHasStyleCallback\n ? formatsAndSegments.every(([format, segment, paragraph]) =>\n segmentHasStyleCallback(format, segment, paragraph)\n )\n : false;\n\n // 4. invoke the callback function to apply the format\n formatsAndSegments.forEach(([format, segment, paragraph]) => {\n toggleStyleCallback(format, !isTurningOff, segment, paragraph);\n });\n\n // 5. after format is applied to all selections, invoke another callback to do some clean up before write the change back\n afterFormatCallback?.(model);\n\n // 6. finally merge segments if possible, to avoid fragmentation\n formatsAndSegments.forEach(([_, __, paragraph]) => {\n if (paragraph) {\n mergeTextSegments(paragraph);\n }\n });\n\n // 7. Write back models that we got from entities (if any)\n writeBackEntities(editor, modelsFromEntities);\n\n // 8. if the selection is still collapsed, it means we didn't actually applied format, set a pending format so it can be applied when user type\n // otherwise, write back to editor\n if (isCollapsedSelection) {\n context.newPendingFormat = segmentAndParagraphs[0][0].format;\n editor.focus();\n return false;\n } else {\n return formatsAndSegments.length > 0;\n }\n },\n {\n apiName,\n }\n );\n}\n\nfunction expandEntitySelections(\n editor: IEditor,\n entity: ContentModelEntity,\n formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][],\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n const { id, entityType: type, isReadonly } = entity.entityFormat;\n\n if (id && type) {\n const formattableRoots: FormattableRoot[] = [];\n const entityOperationEventData: PluginEventData<'entityOperation'> = {\n entity: { id, type, isReadonly: !!isReadonly, wrapper: entity.wrapper },\n operation: 'beforeFormat',\n formattableRoots,\n };\n\n editor.triggerEvent('entityOperation', entityOperationEventData);\n\n formattableRoots.forEach(root => {\n if (entity.wrapper.contains(root.element)) {\n const editorContext = createEditorContextForEntity(editor, entity);\n const context = createDomToModelContext(editorContext, root.domToModelOptions);\n\n // Treat everything as selected since the parent entity is selected\n context.isInSelection = true;\n\n const model = domToContentModel(root.element, context);\n const selections = getSelectedSegmentsAndParagraphs(\n model,\n false /*includingFormatHolder*/,\n false /*includingEntity*/,\n true /*mutate*/\n );\n\n selections.forEach(item => {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n });\n\n modelsFromEntities.push([entity, root, model]);\n }\n });\n }\n}\n\nfunction writeBackEntities(\n editor: IEditor,\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n modelsFromEntities.forEach(([entity, root, model]) => {\n const editorContext = createEditorContextForEntity(editor, entity);\n const modelToDomContext = createModelToDomContext(editorContext, root.modelToDomOptions);\n\n contentModelToDom(editor.getDocument(), root.element, model, modelToDomContext);\n });\n}\n"]}
@@ -44,6 +44,7 @@ export { insertEntity } from './publicApi/entity/insertEntity';
44
44
  export { insertTableRow } from './modelApi/table/insertTableRow';
45
45
  export { insertTableColumn } from './modelApi/table/insertTableColumn';
46
46
  export { clearSelectedCells } from './modelApi/table/clearSelectedCells';
47
+ export { createEditorContextForEntity } from './publicApi/utils/createEditorContextForEntity';
47
48
  export { formatTableWithContentModel } from './publicApi/utils/formatTableWithContentModel';
48
49
  export { formatImageWithContentModel } from './publicApi/utils/formatImageWithContentModel';
49
50
  export { formatParagraphWithContentModel } from './publicApi/utils/formatParagraphWithContentModel';
package/lib-mjs/index.js CHANGED
@@ -44,6 +44,7 @@ export { insertEntity } from './publicApi/entity/insertEntity';
44
44
  export { insertTableRow } from './modelApi/table/insertTableRow';
45
45
  export { insertTableColumn } from './modelApi/table/insertTableColumn';
46
46
  export { clearSelectedCells } from './modelApi/table/clearSelectedCells';
47
+ export { createEditorContextForEntity } from './publicApi/utils/createEditorContextForEntity';
47
48
  export { formatTableWithContentModel } from './publicApi/utils/formatTableWithContentModel';
48
49
  export { formatImageWithContentModel } from './publicApi/utils/formatImageWithContentModel';
49
50
  export { formatParagraphWithContentModel } from './publicApi/utils/formatParagraphWithContentModel';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../packages/roosterjs-content-model-api/lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,0CAA0C,CAAC;AAClF,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,wCAAwC,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE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sC,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAEzE,OAAO,EAAE,2BAA2B,EAAE,MAAM,+CAA+C,CAAC;AAC5F,OAAO,EAAE,2BAA2B,EAAE,MAAM,+CAA+C,CAAC;AAC5F,OAAO,EAAE,+BAA+B,EAAE,MAAM,mDAAmD,CAAC;AACpG,OAAO,EAAE,6BAA6B,EAAE,MAAM,iDAAiD,CAAC;AAChG,OAAO,EAAE,sCAAsC,EAAE,MAAM,0DAA0D,CAAC;AAClH,OAAO,EAAE,iCAAiC,EAAE,MAAM,qDAAqD,CAAC;AAExG,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAE,uBAAuB,EAAE,MAAM,yCAAyC,CAAC;AAClF,OAAO,EAAE,yBAAyB,EAAE,MAAM,2CAA2C,CAAC;AACtF,OAAO,EAAE,mBAAmB,EAAE,MAAM,sCAAsC,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,2CAA2C,CAAC","sourcesContent":["export { insertTable } from './publicApi/table/insertTable';\nexport { formatTable } from './publicApi/table/formatTable';\nexport { setTableCellShade } from './publicApi/table/setTableCellShade';\nexport { editTable } from './publicApi/table/editTable';\nexport { applyTableBorderFormat } from './publicApi/table/applyTableBorderFormat';\nexport { toggleBullet } from './publicApi/list/toggleBullet';\nexport { toggleNumbering } from './publicApi/list/toggleNumbering';\nexport { toggleBold } from './publicApi/segment/toggleBold';\nexport { toggleItalic } from './publicApi/segment/toggleItalic';\nexport { toggleUnderline } from './publicApi/segment/toggleUnderline';\nexport { toggleStrikethrough } from './publicApi/segment/toggleStrikethrough';\nexport { toggleSubscript } from './publicApi/segment/toggleSubscript';\nexport { toggleSuperscript } from './publicApi/segment/toggleSuperscript';\nexport { setBackgroundColor } from './publicApi/segment/setBackgroundColor';\nexport { setFontName } from './publicApi/segment/setFontName';\nexport { setFontSize } from './publicApi/segment/setFontSize';\nexport { setTextColor } from './publicApi/segment/setTextColor';\nexport { changeFontSize } from './publicApi/segment/changeFontSize';\nexport { applySegmentFormat } from './publicApi/segment/applySegmentFormat';\nexport { changeCapitalization } from './publicApi/segment/changeCapitalization';\nexport { splitTextSegment } from './publicApi/segment/splitTextSegment';\nexport { insertImage } from './publicApi/image/insertImage';\nexport { setListStyle } from './publicApi/list/setListStyle';\nexport { setListStartNumber } from './publicApi/list/setListStartNumber';\nexport { setIndentation } from './publicApi/block/setIndentation';\nexport { setAlignment } from './publicApi/block/setAlignment';\nexport { setDirection } from './publicApi/block/setDirection';\nexport { setHeadingLevel } from './publicApi/block/setHeadingLevel';\nexport { toggleBlockQuote } from './publicApi/block/toggleBlockQuote';\nexport { setSpacing } from './publicApi/block/setSpacing';\nexport { setImageBorder } from './publicApi/image/setImageBorder';\nexport { setImageBoxShadow } from './publicApi/image/setImageBoxShadow';\nexport { changeImage } from './publicApi/image/changeImage';\nexport { getFormatState } from './publicApi/format/getFormatState';\nexport { clearFormat } from './publicApi/format/clearFormat';\nexport { insertLink } from './publicApi/link/insertLink';\nexport { removeLink } from './publicApi/link/removeLink';\nexport { adjustLinkSelection } from './publicApi/link/adjustLinkSelection';\nexport { setImageAltText } from './publicApi/image/setImageAltText';\nexport { adjustImageSelection } from './publicApi/image/adjustImageSelection';\nexport { setParagraphMargin } from './publicApi/block/setParagraphMargin';\nexport { toggleCode } from './publicApi/segment/toggleCode';\nexport { insertEntity } from './publicApi/entity/insertEntity';\nexport { insertTableRow } from './modelApi/table/insertTableRow';\nexport { insertTableColumn } from './modelApi/table/insertTableColumn';\nexport { clearSelectedCells } from './modelApi/table/clearSelectedCells';\n\nexport { formatTableWithContentModel } from './publicApi/utils/formatTableWithContentModel';\nexport { formatImageWithContentModel } from './publicApi/utils/formatImageWithContentModel';\nexport { formatParagraphWithContentModel } from './publicApi/utils/formatParagraphWithContentModel';\nexport { formatSegmentWithContentModel } from './publicApi/utils/formatSegmentWithContentModel';\nexport { formatTextSegmentBeforeSelectionMarker } from './publicApi/utils/formatTextSegmentBeforeSelectionMarker';\nexport { formatInsertPointWithContentModel } from './publicApi/utils/formatInsertPointWithContentModel';\n\nexport { setListType } from './modelApi/list/setListType';\nexport { setModelListStyle } from './modelApi/list/setModelListStyle';\nexport { setModelListStartNumber } from './modelApi/list/setModelListStartNumber';\nexport { findListItemsInSameThread } from './modelApi/list/findListItemsInSameThread';\nexport { setModelIndentation } from './modelApi/block/setModelIndentation';\nexport { matchLink } from './modelApi/link/matchLink';\nexport { promoteLink } from './modelApi/link/promoteLink';\nexport { getListAnnounceData } from './modelApi/list/getListAnnounceData';\nexport { queryContentModelBlocks } from './modelApi/common/queryContentModelBlocks';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../packages/roosterjs-content-model-api/lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,0CAA0C,CAAC;AAClF,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;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O,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAE,uBAAuB,EAAE,MAAM,yCAAyC,CAAC;AAClF,OAAO,EAAE,yBAAyB,EAAE,MAAM,2CAA2C,CAAC;AACtF,OAAO,EAAE,mBAAmB,EAAE,MAAM,sCAAsC,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,2CAA2C,CAAC","sourcesContent":["export { insertTable } from './publicApi/table/insertTable';\nexport { formatTable } from './publicApi/table/formatTable';\nexport { setTableCellShade } from './publicApi/table/setTableCellShade';\nexport { editTable } from './publicApi/table/editTable';\nexport { applyTableBorderFormat } from './publicApi/table/applyTableBorderFormat';\nexport { toggleBullet } from './publicApi/list/toggleBullet';\nexport { toggleNumbering } from './publicApi/list/toggleNumbering';\nexport { toggleBold } from './publicApi/segment/toggleBold';\nexport { toggleItalic } from './publicApi/segment/toggleItalic';\nexport { toggleUnderline } from './publicApi/segment/toggleUnderline';\nexport { toggleStrikethrough } from './publicApi/segment/toggleStrikethrough';\nexport { toggleSubscript } from './publicApi/segment/toggleSubscript';\nexport { toggleSuperscript } from './publicApi/segment/toggleSuperscript';\nexport { setBackgroundColor } from './publicApi/segment/setBackgroundColor';\nexport { setFontName } from './publicApi/segment/setFontName';\nexport { setFontSize } from './publicApi/segment/setFontSize';\nexport { setTextColor } from './publicApi/segment/setTextColor';\nexport { changeFontSize } from './publicApi/segment/changeFontSize';\nexport { applySegmentFormat } from './publicApi/segment/applySegmentFormat';\nexport { changeCapitalization } from './publicApi/segment/changeCapitalization';\nexport { splitTextSegment } from './publicApi/segment/splitTextSegment';\nexport { insertImage } from './publicApi/image/insertImage';\nexport { setListStyle } from './publicApi/list/setListStyle';\nexport { setListStartNumber } from './publicApi/list/setListStartNumber';\nexport { setIndentation } from './publicApi/block/setIndentation';\nexport { setAlignment } from './publicApi/block/setAlignment';\nexport { setDirection } from './publicApi/block/setDirection';\nexport { setHeadingLevel } from './publicApi/block/setHeadingLevel';\nexport { toggleBlockQuote } from './publicApi/block/toggleBlockQuote';\nexport { setSpacing } from './publicApi/block/setSpacing';\nexport { setImageBorder } from './publicApi/image/setImageBorder';\nexport { setImageBoxShadow } from './publicApi/image/setImageBoxShadow';\nexport { changeImage } from './publicApi/image/changeImage';\nexport { getFormatState } from './publicApi/format/getFormatState';\nexport { clearFormat } from './publicApi/format/clearFormat';\nexport { insertLink } from './publicApi/link/insertLink';\nexport { removeLink } from './publicApi/link/removeLink';\nexport { adjustLinkSelection } from './publicApi/link/adjustLinkSelection';\nexport { setImageAltText } from './publicApi/image/setImageAltText';\nexport { adjustImageSelection } from './publicApi/image/adjustImageSelection';\nexport { setParagraphMargin } from './publicApi/block/setParagraphMargin';\nexport { toggleCode } from './publicApi/segment/toggleCode';\nexport { insertEntity } from './publicApi/entity/insertEntity';\nexport { insertTableRow } from './modelApi/table/insertTableRow';\nexport { insertTableColumn } from './modelApi/table/insertTableColumn';\nexport { clearSelectedCells } from './modelApi/table/clearSelectedCells';\n\nexport { createEditorContextForEntity } from './publicApi/utils/createEditorContextForEntity';\nexport { formatTableWithContentModel } from './publicApi/utils/formatTableWithContentModel';\nexport { formatImageWithContentModel } from './publicApi/utils/formatImageWithContentModel';\nexport { formatParagraphWithContentModel } from './publicApi/utils/formatParagraphWithContentModel';\nexport { formatSegmentWithContentModel } from './publicApi/utils/formatSegmentWithContentModel';\nexport { formatTextSegmentBeforeSelectionMarker } from './publicApi/utils/formatTextSegmentBeforeSelectionMarker';\nexport { formatInsertPointWithContentModel } from './publicApi/utils/formatInsertPointWithContentModel';\n\nexport { setListType } from './modelApi/list/setListType';\nexport { setModelListStyle } from './modelApi/list/setModelListStyle';\nexport { setModelListStartNumber } from './modelApi/list/setModelListStartNumber';\nexport { findListItemsInSameThread } from './modelApi/list/findListItemsInSameThread';\nexport { setModelIndentation } from './modelApi/block/setModelIndentation';\nexport { matchLink } from './modelApi/link/matchLink';\nexport { promoteLink } from './modelApi/link/promoteLink';\nexport { getListAnnounceData } from './modelApi/list/getListAnnounceData';\nexport { queryContentModelBlocks } from './modelApi/common/queryContentModelBlocks';\n"]}
@@ -1,9 +1,10 @@
1
- import type { ReadonlyContentModelBlock, ReadonlyContentModelBlockBase, ReadonlyContentModelBlockGroup } from 'roosterjs-content-model-types';
1
+ import type { ContentModelEntity, EditorContext, ReadonlyContentModelBlock, ReadonlyContentModelBlockBase, ReadonlyContentModelBlockGroup } from 'roosterjs-content-model-types';
2
2
  /**
3
3
  * Query content model blocks
4
4
  * @param group The block group to query
5
5
  * @param type The type of block to query
6
6
  * @param filter Optional selector to filter the blocks
7
7
  * @param findFirstOnly True to return the first block only, false to return all blocks
8
+ * @param shouldExpandEntity Optional function to determine if an entity's children should be recursively queried, should return a EditorContext if the entity should be expanded, or null if not
8
9
  */
9
- export declare function queryContentModelBlocks<T extends ReadonlyContentModelBlock>(group: ReadonlyContentModelBlockGroup, type: T extends ReadonlyContentModelBlockBase<infer U> ? U : never, filter?: (element: T) => element is T, findFirstOnly?: boolean): T[];
10
+ export declare function queryContentModelBlocks<T extends ReadonlyContentModelBlock>(group: ReadonlyContentModelBlockGroup, type: T extends ReadonlyContentModelBlockBase<infer U> ? U : never, filter?: (element: T) => element is T, findFirstOnly?: boolean, shouldExpandEntity?: (entity: ContentModelEntity) => EditorContext | null): T[];
@@ -1,12 +1,14 @@
1
1
  import { __read, __spreadArray, __values } from "tslib";
2
+ import { createDomToModelContext, domToContentModel } from 'roosterjs-content-model-dom';
2
3
  /**
3
4
  * Query content model blocks
4
5
  * @param group The block group to query
5
6
  * @param type The type of block to query
6
7
  * @param filter Optional selector to filter the blocks
7
8
  * @param findFirstOnly True to return the first block only, false to return all blocks
9
+ * @param shouldExpandEntity Optional function to determine if an entity's children should be recursively queried, should return a EditorContext if the entity should be expanded, or null if not
8
10
  */
9
- export function queryContentModelBlocks(group, type, filter, findFirstOnly) {
11
+ export function queryContentModelBlocks(group, type, filter, findFirstOnly, shouldExpandEntity) {
10
12
  var e_1, _a, e_2, _b;
11
13
  var elements = [];
12
14
  for (var i = 0; i < group.blocks.length; i++) {
@@ -21,12 +23,21 @@ export function queryContentModelBlocks(group, type, filter, findFirstOnly) {
21
23
  if (isExpectedBlockType(block, type, filter)) {
22
24
  elements.push(block);
23
25
  }
26
+ if (block.blockType == 'Entity' && shouldExpandEntity) {
27
+ var editorContext = shouldExpandEntity(block);
28
+ if (editorContext) {
29
+ var context = createDomToModelContext(editorContext);
30
+ var model = domToContentModel(block.wrapper, context);
31
+ var results_1 = queryContentModelBlocks(model, type, filter, findFirstOnly, shouldExpandEntity);
32
+ elements.push.apply(elements, __spreadArray([], __read(results_1), false));
33
+ }
34
+ }
24
35
  break;
25
36
  case 'BlockGroup':
26
37
  if (isExpectedBlockType(block, type, filter)) {
27
38
  elements.push(block);
28
39
  }
29
- var results = queryContentModelBlocks(block, type, filter, findFirstOnly);
40
+ var results = queryContentModelBlocks(block, type, filter, findFirstOnly, shouldExpandEntity);
30
41
  elements.push.apply(elements, __spreadArray([], __read(results), false));
31
42
  break;
32
43
  case 'Table':
@@ -39,8 +50,8 @@ export function queryContentModelBlocks(group, type, filter, findFirstOnly) {
39
50
  try {
40
51
  for (var _e = (e_2 = void 0, __values(row.cells)), _f = _e.next(); !_f.done; _f = _e.next()) {
41
52
  var cell = _f.value;
42
- var results_1 = queryContentModelBlocks(cell, type, filter, findFirstOnly);
43
- elements.push.apply(elements, __spreadArray([], __read(results_1), false));
53
+ var results_2 = queryContentModelBlocks(cell, type, filter, findFirstOnly, shouldExpandEntity);
54
+ elements.push.apply(elements, __spreadArray([], __read(results_2), false));
44
55
  }
45
56
  }
46
57
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
@@ -1 +1 @@
1
- {"version":3,"file":"queryContentModelBlocks.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/common/queryContentModelBlocks.ts"],"names":[],"mappings":";AAOA;;;;;;GAMG;AACH,MAAM,UAAU,uBAAuB,CACnC,KAAqC,EACrC,IAAkE,EAClE,MAAqC,EACrC,aAAuB;;IAEvB,IAAM,QAAQ,GAAQ,EAAE,CAAC;IACzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC1C,IAAI,aAAa,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;YACtC,OAAO,QAAQ,CAAC;SACnB;QACD,IAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAE9B,QAAQ,KAAK,CAAC,SAAS,EAAE;YACrB,KAAK,WAAW,CAAC;YACjB,KAAK,SAAS,CAAC;YACf,KAAK,QAAQ;gBACT,IAAI,mBAAmB,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE;oBAC1C,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;iBACxB;gBACD,MAAM;YACV,KAAK,YAAY;gBACb,IAAI,mBAAmB,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE;oBAC1C,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;iBACxB;gBACD,IAAM,OAAO,GAAG,uBAAuB,CAAI,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC;gBAC/E,QAAQ,CAAC,IAAI,OAAb,QAAQ,2BAAS,OAAO,WAAE;gBAC1B,MAAM;YACV,KAAK,OAAO;gBACR,IAAI,mBAAmB,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE;oBAC1C,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;iBACxB;;oBACD,KAAkB,IAAA,oBAAA,SAAA,KAAK,CAAC,IAAI,CAAA,CAAA,gBAAA,4BAAE;wBAAzB,IAAM,GAAG,WAAA;;4BACV,KAAmB,IAAA,oBAAA,SAAA,GAAG,CAAC,KAAK,CAAA,CAAA,gBAAA,4BAAE;gCAAzB,IAAM,IAAI,WAAA;gCACX,IAAM,SAAO,GAAG,uBAAuB,CACnC,IAAI,EACJ,IAAI,EACJ,MAAM,EACN,aAAa,CAChB,CAAC;gCACF,QAAQ,CAAC,IAAI,OAAb,QAAQ,2BAAS,SAAO,WAAE;6BAC7B;;;;;;;;;qBACJ;;;;;;;;;gBACD,MAAM;SACb;KACJ;IACD,OAAO,QAAQ,CAAC;AACpB,CAAC;AAED,SAAS,mBAAmB,CACxB,KAAgC,EAChC,IAA2B,EAC3B,MAAqC;IAErC,OAAO,WAAW,CAAI,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AACrE,CAAC;AAED,SAAS,WAAW,CAChB,KAAgC,EAChC,IAA2B;IAE3B,OAAO,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC;AACnC,CAAC","sourcesContent":["import type {\n ContentModelBlockType,\n ReadonlyContentModelBlock,\n ReadonlyContentModelBlockBase,\n ReadonlyContentModelBlockGroup,\n} from 'roosterjs-content-model-types';\n\n/**\n * Query content model blocks\n * @param group The block group to query\n * @param type The type of block to query\n * @param filter Optional selector to filter the blocks\n * @param findFirstOnly True to return the first block only, false to return all blocks\n */\nexport function queryContentModelBlocks<T extends ReadonlyContentModelBlock>(\n group: ReadonlyContentModelBlockGroup,\n type: T extends ReadonlyContentModelBlockBase<infer U> ? U : never,\n filter?: (element: T) => element is T,\n findFirstOnly?: boolean\n): T[] {\n const elements: T[] = [];\n for (let i = 0; i < group.blocks.length; i++) {\n if (findFirstOnly && elements.length > 0) {\n return elements;\n }\n const block = group.blocks[i];\n\n switch (block.blockType) {\n case 'Paragraph':\n case 'Divider':\n case 'Entity':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n break;\n case 'BlockGroup':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n const results = queryContentModelBlocks<T>(block, type, filter, findFirstOnly);\n elements.push(...results);\n break;\n case 'Table':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n for (const row of block.rows) {\n for (const cell of row.cells) {\n const results = queryContentModelBlocks<T>(\n cell,\n type,\n filter,\n findFirstOnly\n );\n elements.push(...results);\n }\n }\n break;\n }\n }\n return elements;\n}\n\nfunction isExpectedBlockType<T extends ReadonlyContentModelBlock>(\n block: ReadonlyContentModelBlock,\n type: ContentModelBlockType,\n filter?: (element: T) => element is T\n): block is T {\n return isBlockType<T>(block, type) && (!filter || filter(block));\n}\n\nfunction isBlockType<T extends ReadonlyContentModelBlock>(\n block: ReadonlyContentModelBlock,\n type: ContentModelBlockType\n): block is T {\n return block.blockType == type;\n}\n"]}
1
+ {"version":3,"file":"queryContentModelBlocks.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/common/queryContentModelBlocks.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAUzF;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CACnC,KAAqC,EACrC,IAAkE,EAClE,MAAqC,EACrC,aAAuB,EACvB,kBAAyE;;IAEzE,IAAM,QAAQ,GAAQ,EAAE,CAAC;IACzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC1C,IAAI,aAAa,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;YACtC,OAAO,QAAQ,CAAC;SACnB;QACD,IAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC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AAA,CAAA,gBAAA,4BAAE;wBAAzB,IAAM,GAAG,WAAA;;4BACV,KAAmB,IAAA,oBAAA,SAAA,GAAG,CAAC,KAAK,CAAA,CAAA,gBAAA,4BAAE;gCAAzB,IAAM,IAAI,WAAA;gCACX,IAAM,SAAO,GAAG,uBAAuB,CACnC,IAAI,EACJ,IAAI,EACJ,MAAM,EACN,aAAa,EACb,kBAAkB,CACrB,CAAC;gCACF,QAAQ,CAAC,IAAI,OAAb,QAAQ,2BAAS,SAAO,WAAE;6BAC7B;;;;;;;;;qBACJ;;;;;;;;;gBACD,MAAM;SACb;KACJ;IACD,OAAO,QAAQ,CAAC;AACpB,CAAC;AAED,SAAS,mBAAmB,CACxB,KAAgC,EAChC,IAA2B,EAC3B,MAAqC;IAErC,OAAO,WAAW,CAAI,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AACrE,CAAC;AAED,SAAS,WAAW,CAChB,KAAgC,EAChC,IAA2B;IAE3B,OAAO,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC;AACnC,CAAC","sourcesContent":["import { createDomToModelContext, domToContentModel } from 'roosterjs-content-model-dom';\nimport type {\n ContentModelBlockType,\n ContentModelEntity,\n EditorContext,\n ReadonlyContentModelBlock,\n ReadonlyContentModelBlockBase,\n ReadonlyContentModelBlockGroup,\n} from 'roosterjs-content-model-types';\n\n/**\n * Query content model blocks\n * @param group The block group to query\n * @param type The type of block to query\n * @param filter Optional selector to filter the blocks\n * @param findFirstOnly True to return the first block only, false to return all blocks\n * @param shouldExpandEntity Optional function to determine if an entity's children should be recursively queried, should return a EditorContext if the entity should be expanded, or null if not\n */\nexport function queryContentModelBlocks<T extends ReadonlyContentModelBlock>(\n group: ReadonlyContentModelBlockGroup,\n type: T extends ReadonlyContentModelBlockBase<infer U> ? U : never,\n filter?: (element: T) => element is T,\n findFirstOnly?: boolean,\n shouldExpandEntity?: (entity: ContentModelEntity) => EditorContext | null\n): T[] {\n const elements: T[] = [];\n for (let i = 0; i < group.blocks.length; i++) {\n if (findFirstOnly && elements.length > 0) {\n return elements;\n }\n const block = group.blocks[i];\n\n switch (block.blockType) {\n case 'Paragraph':\n case 'Divider':\n case 'Entity':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n if (block.blockType == 'Entity' && shouldExpandEntity) {\n const editorContext = shouldExpandEntity(block);\n if (editorContext) {\n const context = createDomToModelContext(editorContext);\n const model = domToContentModel(block.wrapper, context);\n const results = queryContentModelBlocks<T>(\n model,\n type,\n filter,\n findFirstOnly,\n shouldExpandEntity\n );\n elements.push(...results);\n }\n }\n break;\n case 'BlockGroup':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n const results = queryContentModelBlocks<T>(\n block,\n type,\n filter,\n findFirstOnly,\n shouldExpandEntity\n );\n elements.push(...results);\n break;\n case 'Table':\n if (isExpectedBlockType(block, type, filter)) {\n elements.push(block);\n }\n for (const row of block.rows) {\n for (const cell of row.cells) {\n const results = queryContentModelBlocks<T>(\n cell,\n type,\n filter,\n findFirstOnly,\n shouldExpandEntity\n );\n elements.push(...results);\n }\n }\n break;\n }\n }\n return elements;\n}\n\nfunction isExpectedBlockType<T extends ReadonlyContentModelBlock>(\n block: ReadonlyContentModelBlock,\n type: ContentModelBlockType,\n filter?: (element: T) => element is T\n): block is T {\n return isBlockType<T>(block, type) && (!filter || filter(block));\n}\n\nfunction isBlockType<T extends ReadonlyContentModelBlock>(\n block: ReadonlyContentModelBlock,\n type: ContentModelBlockType\n): block is T {\n return block.blockType == type;\n}\n"]}
@@ -36,8 +36,9 @@ export function insertLink(editor, link, anchorTitle, displayText, target) {
36
36
  .map(function (x) { return (x.segmentType == 'Text' ? x.text : ''); })
37
37
  .join('');
38
38
  var text = displayText || originalText || '';
39
- if (segments.some(function (x) { return x.segmentType != 'SelectionMarker'; }) &&
40
- originalText == text) {
39
+ if ((segments.some(function (x) { return x.segmentType != 'SelectionMarker'; }) &&
40
+ originalText == text) ||
41
+ (segments.length == 1 && segments[0].segmentType == 'Image')) {
41
42
  segments.forEach(function (x) {
42
43
  var link = createLink(linkUrl_1, anchorTitle, target, x.segmentType == 'Text');
43
44
  addLink(x, link);
@@ -1 +1 @@
1
- {"version":3,"file":"insertLink.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/link/insertLink.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,4BAA4B,EAAE,MAAM,uDAAuD,CAAC;AACrG,OAAO,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC1D,OAAO,EACH,OAAO,EACP,UAAU,EACV,YAAY,EACZ,0BAA0B,EAC1B,UAAU,EACV,mBAAmB,EACnB,UAAU,GACb,MAAM,6BAA6B,CAAC;AAGrC,4BAA4B;AAC5B,IAAM,SAAS,GAAG,cAAc,CAAC;AACjC,wCAAwC;AACxC,IAAM,YAAY,GAAG,cAAc,CAAC;AACpC,sDAAsD;AACtD,IAAM,SAAS,GAAG,SAAS,CAAC;AAE5B;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,UAAU,CACtB,MAAe,EACf,IAAY,EACZ,WAAoB,EACpB,WAAoB,EACpB,MAAe;IAEf,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,IAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAC1C,IAAI,GAAG,EAAE;QACL,IAAM,UAAQ,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;QAChC,IAAM,SAAO,GAAG,UAAQ,CAAC,CAAC,CAAC,UAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;QACzE,IAAM,OAAK,GAAuB,EAAE,CAAC;QACrC,IAAI,YAA4B,CAAC;QAEjC,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;;YACX,IAAM,QAAQ,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,OAAO,GAAG,CAAC;KACd;IAED,4BAA4B;IAC5B,oEAAoE;IACpE,iEAAiE;IACjE,qDAAqD;IACrD,+BAA+B;IAC/B,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE;QAC3B,IAAI,GAAG,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE;YAC/B,MAAM,GAAG,SAAS,CAAC;SACtB;aAAM,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;YACnC,MAAM,GAAG,QAAQ,CAAC;SACrB;aAAM;YACH,sBAAsB;YACtB,MAAM,GAAG,SAAS,CAAC;SACtB;KACJ;IAED,OAAO,MAAM,GAAG,GAAG,CAAC;AACxB,CAAC;AAED,SAAS,QAAQ,CAAC,IAAY;IAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AAChE,CAAC","sourcesContent":["import { adjustTrailingSpaceSelection } from '../../modelApi/selection/adjustTrailingSpaceSelection';\nimport { matchLink } from '../../modelApi/link/matchLink';\nimport {\n addLink,\n addSegment,\n ChangeSource,\n createContentModelDocument,\n createText,\n getSelectedSegments,\n mergeModel,\n} from 'roosterjs-content-model-dom';\nimport type { ContentModelLink, IEditor } from 'roosterjs-content-model-types';\n\n// Regex matching Uri scheme\nconst URI_REGEX = /^[a-zA-Z]+:/i;\n// Regex matching begin of email address\nconst MAILTO_REGEX = /^[\\w.%+-]+@/i;\n// Regex matching begin of ftp, i.e. ftp.microsoft.com\nconst FTP_REGEX = /^ftp\\./i;\n\n/**\n * Insert a hyperlink at cursor.\n * When there is a selection, hyperlink will be applied to the selection,\n * otherwise a hyperlink will be inserted to the cursor position.\n * @param editor Editor object\n * @param link Link address, can be http(s), mailto, notes, file, unc, ftp, news, telnet, gopher, wais.\n * When protocol is not specified, a best matched protocol will be predicted.\n * @param anchorTitle Optional alt text of the link, will be shown when hover on the link\n * @param displayText Optional display text for the link.\n * @param target Optional display target for the link (\"_blank\"|\"_self\"|\"_parent\"|\"_top\"|\"{framename}\")\n * If specified, the display text of link will be replaced with this text.\n * If not specified and there wasn't a link, the link url will be used as display text.\n */\nexport function insertLink(\n editor: IEditor,\n link: string,\n anchorTitle?: string,\n displayText?: string,\n target?: string\n) {\n editor.focus();\n\n const url = (checkXss(link) || '').trim();\n if (url) {\n const linkData = matchLink(url);\n const linkUrl = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);\n const links: ContentModelLink[] = [];\n let anchorNode: Node | undefined;\n\n editor.formatContentModel(\n (model, context) => {\n const segments = getSelectedSegments(\n model,\n false /*includingFormatHolder*/,\n true /*mutate*/\n );\n const originalText = segments\n .map(x => (x.segmentType == 'Text' ? x.text : ''))\n .join('');\n const text = displayText || originalText || '';\n\n if (\n segments.some(x => x.segmentType != 'SelectionMarker') &&\n originalText == text\n ) {\n segments.forEach(x => {\n const link = createLink(\n linkUrl,\n anchorTitle,\n target,\n x.segmentType == 'Text'\n );\n addLink(x, link);\n if (x.link) {\n links.push(x.link);\n }\n });\n } else if (\n segments.every(x => x.segmentType == 'SelectionMarker') ||\n (!!text && text != originalText)\n ) {\n const segment = createText(text || (linkData ? linkData.originalUrl : url), {\n ...segments[0]?.format,\n ...editor.getPendingFormat(),\n });\n const doc = createContentModelDocument();\n const link = createLink(linkUrl, anchorTitle, target);\n\n addLink(segment, link);\n addSegment(doc, segment);\n\n if (segment.link) {\n links.push(segment.link);\n }\n\n mergeModel(model, doc, context, {\n mergeFormat: 'mergeAll',\n });\n }\n\n adjustTrailingSpaceSelection(model);\n return segments.length > 0;\n },\n {\n changeSource: ChangeSource.CreateLink,\n onNodeCreated: (modelElement, node) => {\n if (!anchorNode && links.indexOf(modelElement as ContentModelLink) >= 0) {\n anchorNode = node;\n }\n },\n getChangeData: () => anchorNode,\n apiName: 'insertLink',\n }\n );\n }\n}\n\nconst createLink = (\n url: string,\n anchorTitle?: string,\n target?: string,\n underline: boolean = true\n): ContentModelLink => {\n return {\n dataset: {},\n format: {\n href: url,\n anchorTitle,\n target,\n underline: underline,\n },\n };\n};\n\nfunction applyLinkPrefix(url: string): string {\n if (!url) {\n return url;\n }\n\n // Add link prefix per rule:\n // (a) if the url always starts with a URI scheme, leave it as it is\n // (b) if the url is an email address, xxx@... add mailto: prefix\n // (c) if the url starts with ftp., add ftp:// prefix\n // (d) rest, add http:// prefix\n let prefix = '';\n if (url.search(URI_REGEX) < 0) {\n if (url.search(MAILTO_REGEX) == 0) {\n prefix = 'mailto:';\n } else if (url.search(FTP_REGEX) == 0) {\n prefix = 'ftp://';\n } else {\n // fallback to http://\n prefix = 'http://';\n }\n }\n\n return prefix + url;\n}\n\nfunction checkXss(link: string): string {\n return link.match(/s\\n*c\\n*r\\n*i\\n*p\\n*t\\n*:/i) ? '' : link;\n}\n"]}
1
+ {"version":3,"file":"insertLink.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/link/insertLink.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,4BAA4B,EAAE,MAAM,uDAAuD,CAAC;AACrG,OAAO,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC1D,OAAO,EACH,OAAO,EACP,UAAU,EACV,YAAY,EACZ,0BAA0B,EAC1B,UAAU,EACV,mBAAmB,EACnB,UAAU,GACb,MAAM,6BAA6B,CAAC;AAGrC,4BAA4B;AAC5B,IAAM,SAAS,GAAG,cAAc,CAAC;AACjC,wCAAwC;AACxC,IAAM,YAAY,GAAG,cAAc,CAAC;AACpC,sDAAsD;AACtD,IAAM,SAAS,GAAG,SAAS,CAAC;AAE5B;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,UAAU,CACtB,MAAe,EACf,IAAY,EACZ,WAAoB,EACpB,WAAoB,EACpB,MAAe;IAEf,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,IAAM,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election } from '../../modelApi/selection/adjustTrailingSpaceSelection';\nimport { matchLink } from '../../modelApi/link/matchLink';\nimport {\n addLink,\n addSegment,\n ChangeSource,\n createContentModelDocument,\n createText,\n getSelectedSegments,\n mergeModel,\n} from 'roosterjs-content-model-dom';\nimport type { ContentModelLink, IEditor } from 'roosterjs-content-model-types';\n\n// Regex matching Uri scheme\nconst URI_REGEX = /^[a-zA-Z]+:/i;\n// Regex matching begin of email address\nconst MAILTO_REGEX = /^[\\w.%+-]+@/i;\n// Regex matching begin of ftp, i.e. ftp.microsoft.com\nconst FTP_REGEX = /^ftp\\./i;\n\n/**\n * Insert a hyperlink at cursor.\n * When there is a selection, hyperlink will be applied to the selection,\n * otherwise a hyperlink will be inserted to the cursor position.\n * @param editor Editor object\n * @param link Link address, can be http(s), mailto, notes, file, unc, ftp, news, telnet, gopher, wais.\n * When protocol is not specified, a best matched protocol will be predicted.\n * @param anchorTitle Optional alt text of the link, will be shown when hover on the link\n * @param displayText Optional display text for the link.\n * @param target Optional display target for the link (\"_blank\"|\"_self\"|\"_parent\"|\"_top\"|\"{framename}\")\n * If specified, the display text of link will be replaced with this text.\n * If not specified and there wasn't a link, the link url will be used as display text.\n */\nexport function insertLink(\n editor: IEditor,\n link: string,\n anchorTitle?: string,\n displayText?: string,\n target?: string\n) {\n editor.focus();\n\n const url = (checkXss(link) || '').trim();\n if (url) {\n const linkData = matchLink(url);\n const linkUrl = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);\n const links: ContentModelLink[] = [];\n let anchorNode: Node | undefined;\n\n editor.formatContentModel(\n (model, context) => {\n const segments = getSelectedSegments(\n model,\n false /*includingFormatHolder*/,\n true /*mutate*/\n );\n\n const originalText = segments\n .map(x => (x.segmentType == 'Text' ? x.text : ''))\n .join('');\n const text = displayText || originalText || '';\n\n if (\n (segments.some(x => x.segmentType != 'SelectionMarker') &&\n originalText == text) ||\n (segments.length == 1 && segments[0].segmentType == 'Image')\n ) {\n segments.forEach(x => {\n const link = createLink(\n linkUrl,\n anchorTitle,\n target,\n x.segmentType == 'Text'\n );\n addLink(x, link);\n if (x.link) {\n links.push(x.link);\n }\n });\n } else if (\n segments.every(x => x.segmentType == 'SelectionMarker') ||\n (!!text && text != originalText)\n ) {\n const segment = createText(text || (linkData ? linkData.originalUrl : url), {\n ...segments[0]?.format,\n ...editor.getPendingFormat(),\n });\n const doc = createContentModelDocument();\n const link = createLink(linkUrl, anchorTitle, target);\n\n addLink(segment, link);\n addSegment(doc, segment);\n\n if (segment.link) {\n links.push(segment.link);\n }\n\n mergeModel(model, doc, context, {\n mergeFormat: 'mergeAll',\n });\n }\n\n adjustTrailingSpaceSelection(model);\n return segments.length > 0;\n },\n {\n changeSource: ChangeSource.CreateLink,\n onNodeCreated: (modelElement, node) => {\n if (!anchorNode && links.indexOf(modelElement as ContentModelLink) >= 0) {\n anchorNode = node;\n }\n },\n getChangeData: () => anchorNode,\n apiName: 'insertLink',\n }\n );\n }\n}\n\nconst createLink = (\n url: string,\n anchorTitle?: string,\n target?: string,\n underline: boolean = true\n): ContentModelLink => {\n return {\n dataset: {},\n format: {\n href: url,\n anchorTitle,\n target,\n underline: underline,\n },\n };\n};\n\nfunction applyLinkPrefix(url: string): string {\n if (!url) {\n return url;\n }\n\n // Add link prefix per rule:\n // (a) if the url always starts with a URI scheme, leave it as it is\n // (b) if the url is an email address, xxx@... add mailto: prefix\n // (c) if the url starts with ftp., add ftp:// prefix\n // (d) rest, add http:// prefix\n let prefix = '';\n if (url.search(URI_REGEX) < 0) {\n if (url.search(MAILTO_REGEX) == 0) {\n prefix = 'mailto:';\n } else if (url.search(FTP_REGEX) == 0) {\n prefix = 'ftp://';\n } else {\n // fallback to http://\n prefix = 'http://';\n }\n }\n\n return prefix + url;\n}\n\nfunction checkXss(link: string): string {\n return link.match(/s\\n*c\\n*r\\n*i\\n*p\\n*t\\n*:/i) ? '' : link;\n}\n"]}
@@ -0,0 +1,8 @@
1
+ import type { ContentModelEntity, EditorContext, IEditor } from 'roosterjs-content-model-types';
2
+ /**
3
+ * Create an EditorContext for an entity
4
+ * @param editor The editor object
5
+ * @param entity The entity to create the context for
6
+ * @returns The generated EditorContext for the entity
7
+ */
8
+ export declare function createEditorContextForEntity(editor: IEditor, entity: ContentModelEntity): EditorContext;
@@ -0,0 +1,26 @@
1
+ import { __assign } from "tslib";
2
+ /**
3
+ * Create an EditorContext for an entity
4
+ * @param editor The editor object
5
+ * @param entity The entity to create the context for
6
+ * @returns The generated EditorContext for the entity
7
+ */
8
+ export function createEditorContextForEntity(editor, entity) {
9
+ var _a;
10
+ var domHelper = editor.getDOMHelper();
11
+ var context = {
12
+ isDarkMode: editor.isDarkMode(),
13
+ defaultFormat: __assign({}, entity.format),
14
+ darkColorHandler: editor.getColorManager(),
15
+ addDelimiterForEntity: false,
16
+ allowCacheElement: false,
17
+ domIndexer: undefined,
18
+ zoomScale: domHelper.calculateZoomScale(),
19
+ experimentalFeatures: [],
20
+ };
21
+ if (((_a = editor.getDocument().defaultView) === null || _a === void 0 ? void 0 : _a.getComputedStyle(entity.wrapper).direction) == 'rtl') {
22
+ context.isRootRtl = true;
23
+ }
24
+ return context;
25
+ }
26
+ //# sourceMappingURL=createEditorContextForEntity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createEditorContextForEntity.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/createEditorContextForEntity.ts"],"names":[],"mappings":";AAEA;;;;;GAKG;AACH,MAAM,UAAU,4BAA4B,CACxC,MAAe,EACf,MAA0B;;IAE1B,IAAM,SAAS,GAAG,MAAM,CAAC,YAAY,EAAE,CAAC;IACxC,IAAM,OAAO,GAAkB;QAC3B,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE;QAC/B,aAAa,eAAO,MAAM,CAAC,MAAM,CAAE;QACnC,gBAAgB,EAAE,MAAM,CAAC,eAAe,EAAE;QAC1C,qBAAqB,EAAE,KAAK;QAC5B,iBAAiB,EAAE,KAAK;QACxB,UAAU,EAAE,SAAS;QACrB,SAAS,EAAE,SAAS,CAAC,kBAAkB,EAAE;QACzC,oBAAoB,EAAE,EAAE;KAC3B,CAAC;IAEF,IAAI,CAAA,MAAA,MAAM,CAAC,WAAW,EAAE,CAAC,WAAW,0CAAE,gBAAgB,CAAC,MAAM,CAAC,OAAO,EAAE,SAAS,KAAI,KAAK,EAAE;QACvF,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;KAC5B;IAED,OAAO,OAAO,CAAC;AACnB,CAAC","sourcesContent":["import type { ContentModelEntity, EditorContext, IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Create an EditorContext for an entity\n * @param editor The editor object\n * @param entity The entity to create the context for\n * @returns The generated EditorContext for the entity\n */\nexport function createEditorContextForEntity(\n editor: IEditor,\n entity: ContentModelEntity\n): EditorContext {\n const domHelper = editor.getDOMHelper();\n const context: EditorContext = {\n isDarkMode: editor.isDarkMode(),\n defaultFormat: { ...entity.format },\n darkColorHandler: editor.getColorManager(),\n addDelimiterForEntity: false,\n allowCacheElement: false,\n domIndexer: undefined,\n zoomScale: domHelper.calculateZoomScale(),\n experimentalFeatures: [],\n };\n\n if (editor.getDocument().defaultView?.getComputedStyle(entity.wrapper).direction == 'rtl') {\n context.isRootRtl = true;\n }\n\n return context;\n}\n"]}
@@ -1,6 +1,7 @@
1
- import { __assign, __read } from "tslib";
2
- import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';
1
+ import { __read } from "tslib";
3
2
  import { contentModelToDom, createDomToModelContext, createModelToDomContext, domToContentModel, getSelectedSegmentsAndParagraphs, mergeTextSegments, } from 'roosterjs-content-model-dom';
3
+ import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';
4
+ import { createEditorContextForEntity } from './createEditorContextForEntity';
4
5
  /**
5
6
  * Invoke a callback to format the selected segment using Content Model
6
7
  * @param editor The editor object
@@ -73,24 +74,6 @@ export function formatSegmentWithContentModel(editor, apiName, toggleStyleCallba
73
74
  apiName: apiName,
74
75
  });
75
76
  }
76
- function createEditorContextForEntity(editor, entity) {
77
- var _a;
78
- var domHelper = editor.getDOMHelper();
79
- var context = {
80
- isDarkMode: editor.isDarkMode(),
81
- defaultFormat: __assign({}, entity.format),
82
- darkColorHandler: editor.getColorManager(),
83
- addDelimiterForEntity: false,
84
- allowCacheElement: false,
85
- domIndexer: undefined,
86
- zoomScale: domHelper.calculateZoomScale(),
87
- experimentalFeatures: [],
88
- };
89
- if (((_a = editor.getDocument().defaultView) === null || _a === void 0 ? void 0 : _a.getComputedStyle(entity.wrapper).direction) == 'rtl') {
90
- context.isRootRtl = true;
91
- }
92
- return context;
93
- }
94
77
  function expandEntitySelections(editor, entity, formatsAndSegments, modelsFromEntities) {
95
78
  var _a = entity.entityFormat, id = _a.id, type = _a.entityType, isReadonly = _a.isReadonly;
96
79
  if (id && type) {