roosterjs-content-model-api 9.53.0 → 9.55.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.
@@ -12,7 +12,7 @@ var formatSegmentWithContentModel_1 = require("../utils/formatSegmentWithContent
12
12
  */
13
13
  function changeCapitalization(editor, capitalization, language) {
14
14
  editor.focus();
15
- (0, formatSegmentWithContentModel_1.formatSegmentWithContentModel)(editor, 'changeCapitalization', function (_, __, segment) {
15
+ (0, formatSegmentWithContentModel_1.formatSegmentWithContentModel)(editor, 'changeCapitalization', function (_, __, segment, paragraph) {
16
16
  if ((segment === null || segment === void 0 ? void 0 : segment.segmentType) == 'Text') {
17
17
  switch (capitalization) {
18
18
  case 'lowerCase':
@@ -23,7 +23,13 @@ function changeCapitalization(editor, capitalization, language) {
23
23
  break;
24
24
  case 'capitalize':
25
25
  var wordArray = segment.text.toLocaleLowerCase(language).split(' ');
26
- for (var i = 0; i < wordArray.length; i++) {
26
+ // When a collapsed selection is in the middle of a word, the word is split
27
+ // into multiple text segments around the selection marker. In that case the
28
+ // first segment of the word continues from a previous segment, so its first
29
+ // word must not be capitalized to avoid results like "HeLlo" for "he|llo".
30
+ var precedingChar = getPrecedingCharacter(paragraph, segment);
31
+ var startIndex = precedingChar && precedingChar != ' ' ? 1 : 0;
32
+ for (var i = startIndex; i < wordArray.length; i++) {
27
33
  wordArray[i] =
28
34
  wordArray[i].charAt(0).toLocaleUpperCase(language) +
29
35
  wordArray[i].slice(1);
@@ -47,4 +53,28 @@ function changeCapitalization(editor, capitalization, language) {
47
53
  });
48
54
  }
49
55
  exports.changeCapitalization = changeCapitalization;
56
+ /**
57
+ * Get the character immediately preceding the given segment within its paragraph, skipping
58
+ * selection markers (which carry no text). Returns an empty string if there is no preceding
59
+ * text character (e.g. the segment starts the paragraph or follows a non-text segment).
60
+ */
61
+ function getPrecedingCharacter(paragraph, segment) {
62
+ if (!paragraph) {
63
+ return '';
64
+ }
65
+ var index = paragraph.segments.indexOf(segment);
66
+ for (var i = index - 1; i >= 0; i--) {
67
+ var previous = paragraph.segments[i];
68
+ if (previous.segmentType == 'SelectionMarker') {
69
+ continue;
70
+ }
71
+ else if (previous.segmentType == 'Text' && previous.text.length > 0) {
72
+ return previous.text[previous.text.length - 1];
73
+ }
74
+ else {
75
+ break;
76
+ }
77
+ }
78
+ return '';
79
+ }
50
80
  //# sourceMappingURL=changeCapitalization.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"changeCapitalization.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/segment/changeCapitalization.ts"],"names":[],"mappings":";;;AAAA,wFAAuF;AAGvF;;;;;;;GAOG;AACH,SAAgB,oBAAoB,CAChC,MAAe,EACf,cAAqE,EACrE,QAAiB;IAEjB,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,IAAA,6DAA6B,EAAC,MAAM,EAAE,sBAAsB,EAAE,UAAC,CAAC,EAAE,EAAE,EAAE,OAAO;QACzE,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,WAAW,KAAI,MAAM,EAAE;YAChC,QAAQ,cAAc,EAAE;gBACpB,KAAK,WAAW;oBACZ,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;oBACxD,MAAM;gBAEV,KAAK,WAAW;oBACZ,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;oBACxD,MAAM;gBAEV,KAAK,YAAY;oBACb,IAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;oBAEtE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;wBACvC,SAAS,CAAC,CAAC,CAAC;4BACR,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,QAAQ,CAAC;gCAClD,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;qBAC7B;oBAED,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;oBACnC,MAAM;gBAEV,KAAK,UAAU;oBACX,IAAM,gBAAgB,GAAG,aAAa,CAAC;oBACvC,2CAA2C;oBAC3C,2EAA2E;oBAC3E,2EAA2E;oBAC3E,uEAAuE;oBACvE,sDAAsD;oBACtD,IAAM,KAAK,GAAG,IAAI,MAAM,CAAC,WAAW,GAAG,gBAAgB,GAAG,SAAS,EAAE,GAAG,CAAC,CAAC;oBAE1E,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI;yBACtB,iBAAiB,CAAC,QAAQ,CAAC;yBAC3B,OAAO,CAAC,KAAK,EAAE,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,iBAAiB,CAAC,QAAQ,CAAC,EAAjC,CAAiC,CAAC,CAAC;oBAChE,MAAM;aACb;SACJ;IACL,CAAC,CAAC,CAAC;AACP,CAAC;AA9CD,oDA8CC","sourcesContent":["import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Change the capitalization of text in the selection\n * @param editor The editor instance\n * @param capitalization The case option\n * @param language Optional parameter for language string that should comply to \"IETF BCP 47 Tags for\n * Identifying Languages\". For example: 'en' or 'en-US' for English, 'tr' for Turkish.\n * Default is the host environment’s current locale.\n */\nexport function changeCapitalization(\n editor: IEditor,\n capitalization: 'sentence' | 'lowerCase' | 'upperCase' | 'capitalize',\n language?: string\n) {\n editor.focus();\n\n formatSegmentWithContentModel(editor, 'changeCapitalization', (_, __, segment) => {\n if (segment?.segmentType == 'Text') {\n switch (capitalization) {\n case 'lowerCase':\n segment.text = segment.text.toLocaleLowerCase(language);\n break;\n\n case 'upperCase':\n segment.text = segment.text.toLocaleUpperCase(language);\n break;\n\n case 'capitalize':\n const wordArray = segment.text.toLocaleLowerCase(language).split(' ');\n\n for (let i = 0; i < wordArray.length; i++) {\n wordArray[i] =\n wordArray[i].charAt(0).toLocaleUpperCase(language) +\n wordArray[i].slice(1);\n }\n\n segment.text = wordArray.join(' ');\n break;\n\n case 'sentence':\n const punctuationMarks = '[\\\\.\\\\!\\\\?]';\n // Find a match of a word character either:\n // - At the beginning of a string with or without preceding whitespace, for\n // example: ' hello world' and 'hello world' strings would both match 'h'.\n // - Or preceded by a punctuation mark and at least one whitespace, for\n // example 'yes. hello world' would match 'y' and 'h'.\n const regex = new RegExp('^\\\\s*\\\\w|' + punctuationMarks + '\\\\s+\\\\w', 'g');\n\n segment.text = segment.text\n .toLocaleLowerCase(language)\n .replace(regex, match => match.toLocaleUpperCase(language));\n break;\n }\n }\n });\n}\n"]}
1
+ {"version":3,"file":"changeCapitalization.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/segment/changeCapitalization.ts"],"names":[],"mappings":";;;AAAA,wFAAuF;AAOvF;;;;;;;GAOG;AACH,SAAgB,oBAAoB,CAChC,MAAe,EACf,cAAqE,EACrE,QAAiB;IAEjB,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,IAAA,6DAA6B,EAAC,MAAM,EAAE,sBAAsB,EAAE,UAAC,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,SAAS;QACpF,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,WAAW,KAAI,MAAM,EAAE;YAChC,QAAQ,cAAc,EAAE;gBACpB,KAAK,WAAW;oBACZ,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;oBACxD,MAAM;gBAEV,KAAK,WAAW;oBACZ,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,iBAA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C,CAAiC,CAAC,CAAC;oBAChE,MAAM;aACb;SACJ;IACL,CAAC,CAAC,CAAC;AACP,CAAC;AArDD,oDAqDC;AAED;;;;GAIG;AACH,SAAS,qBAAqB,CAC1B,SAAqD,EACrD,OAA0C;IAE1C,IAAI,CAAC,SAAS,EAAE;QACZ,OAAO,EAAE,CAAC;KACb;IAED,IAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAElD,KAAK,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;QACjC,IAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QAEvC,IAAI,QAAQ,CAAC,WAAW,IAAI,iBAAiB,EAAE;YAC3C,SAAS;SACZ;aAAM,IAAI,QAAQ,CAAC,WAAW,IAAI,MAAM,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;YACnE,OAAO,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;SAClD;aAAM;YACH,MAAM;SACT;KACJ;IAED,OAAO,EAAE,CAAC;AACd,CAAC","sourcesContent":["import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';\nimport type {\n IEditor,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Change the capitalization of text in the selection\n * @param editor The editor instance\n * @param capitalization The case option\n * @param language Optional parameter for language string that should comply to \"IETF BCP 47 Tags for\n * Identifying Languages\". For example: 'en' or 'en-US' for English, 'tr' for Turkish.\n * Default is the host environment’s current locale.\n */\nexport function changeCapitalization(\n editor: IEditor,\n capitalization: 'sentence' | 'lowerCase' | 'upperCase' | 'capitalize',\n language?: string\n) {\n editor.focus();\n\n formatSegmentWithContentModel(editor, 'changeCapitalization', (_, __, segment, paragraph) => {\n if (segment?.segmentType == 'Text') {\n switch (capitalization) {\n case 'lowerCase':\n segment.text = segment.text.toLocaleLowerCase(language);\n break;\n\n case 'upperCase':\n segment.text = segment.text.toLocaleUpperCase(language);\n break;\n\n case 'capitalize':\n const wordArray = segment.text.toLocaleLowerCase(language).split(' ');\n\n // When a collapsed selection is in the middle of a word, the word is split\n // into multiple text segments around the selection marker. In that case the\n // first segment of the word continues from a previous segment, so its first\n // word must not be capitalized to avoid results like \"HeLlo\" for \"he|llo\".\n const precedingChar = getPrecedingCharacter(paragraph, segment);\n const startIndex = precedingChar && precedingChar != ' ' ? 1 : 0;\n\n for (let i = startIndex; i < wordArray.length; i++) {\n wordArray[i] =\n wordArray[i].charAt(0).toLocaleUpperCase(language) +\n wordArray[i].slice(1);\n }\n\n segment.text = wordArray.join(' ');\n break;\n\n case 'sentence':\n const punctuationMarks = '[\\\\.\\\\!\\\\?]';\n // Find a match of a word character either:\n // - At the beginning of a string with or without preceding whitespace, for\n // example: ' hello world' and 'hello world' strings would both match 'h'.\n // - Or preceded by a punctuation mark and at least one whitespace, for\n // example 'yes. hello world' would match 'y' and 'h'.\n const regex = new RegExp('^\\\\s*\\\\w|' + punctuationMarks + '\\\\s+\\\\w', 'g');\n\n segment.text = segment.text\n .toLocaleLowerCase(language)\n .replace(regex, match => match.toLocaleUpperCase(language));\n break;\n }\n }\n });\n}\n\n/**\n * Get the character immediately preceding the given segment within its paragraph, skipping\n * selection markers (which carry no text). Returns an empty string if there is no preceding\n * text character (e.g. the segment starts the paragraph or follows a non-text segment).\n */\nfunction getPrecedingCharacter(\n paragraph: ShallowMutableContentModelParagraph | null,\n segment: ShallowMutableContentModelSegment\n): string {\n if (!paragraph) {\n return '';\n }\n\n const index = paragraph.segments.indexOf(segment);\n\n for (let i = index - 1; i >= 0; i--) {\n const previous = paragraph.segments[i];\n\n if (previous.segmentType == 'SelectionMarker') {\n continue;\n } else if (previous.segmentType == 'Text' && previous.text.length > 0) {\n return previous.text[previous.text.length - 1];\n } else {\n break;\n }\n }\n\n return '';\n}\n"]}
@@ -30,6 +30,15 @@ function formatInsertPointWithContentModel(editor, insertPoint, callback, option
30
30
  textWithSelection: getShadowTextProcessor(bundle),
31
31
  },
32
32
  tryGetFromCache: false,
33
+ // When an element carries "container level" styles such as margin or padding, we first
34
+ // wrap it in a FormatContainer. After all its child nodes are processed, we decide whether
35
+ // to keep the FormatContainer or fall back to a plain paragraph when it only wraps a single
36
+ // paragraph. However, formatInsertPointWithContentModel persists the Content Model group path
37
+ // during processing so the later formatting callback can still use it (see the
38
+ // DomToModelContextWithPath interface below). If the FormatContainer falls back to a paragraph,
39
+ // it is removed from the model and the persisted path becomes invalid. To keep the path valid,
40
+ // we skip the fallback check here and always keep the FormatContainer when one is needed.
41
+ skipFormatContainerFallbackCheck: true,
33
42
  });
34
43
  }
35
44
  exports.formatInsertPointWithContentModel = formatInsertPointWithContentModel;
@@ -1 +1 @@
1
- {"version":3,"file":"formatInsertPointWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatInsertPointWithContentModel.ts"],"names":[],"mappings":";;;;AAAA,2EAOqC;AAarC;;;;;;GAMG;AACH,SAAgB,iCAAiC,CAC7C,MAAe,EACf,WAA2B,EAC3B,QAIS,EACT,OAAmC;IAEnC,IAAM,MAAM,GAAsB;QAC9B,KAAK,EAAE,WAAW;KACrB,CAAC;IAEF,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;QAExC,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,EAAE;YACV,IAAA,KAAwB,MAAM,CAAC,MAAM,EAAnC,SAAS,eAAA,EAAE,MAAM,YAAkB,CAAC;YAC5C,IAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAEjD,I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kBAAkB,CACvB,KAA6B,EAC7B,OAAkC,EAClC,SAAgB,EAChB,MAAe,EACf,MAA0B;IAE1B,IAAM,MAAM,GAAG,IAAA,kDAAoB,EAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;IAEvE,MAAM,CAAC,UAAU,GAAG,CAAC,MAAM,CAAC;IAE5B,IAAM,IAAI,GAAG,IAAA,wCAAU,EAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAE3E,IAAI,MAAM,IAAI,OAAO,CAAC,IAAI,EAAE;QACxB,MAAM,CAAC,MAAM,GAAG;YACZ,IAAI,qDAAM,OAAO,CAAC,IAAI,SAAC;YACvB,SAAS,EAAE,IAAI;YACf,MAAM,QAAA;SACT,CAAC;KACL;AACL,CAAC;AAED,SAAS,yBAAyB,CAC9B,OAA0B,EAC1B,MAAyB,EACzB,gBAAsB;IAEhB,IAAA,KAAA,oBAAe,IAAA,wDAA0B,EAAC,OAAO,EAAE,gBAAgB,CAAC,IAAA,EAAnE,KAAK,QAAA,EAAE,GAAG,QAAyD,CAAC;IAC3E,IAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,IAAI,gBAAgB,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAEhF,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;AAChC,CAAC","sourcesContent":["import {\n addSegment,\n addTextSegment,\n buildSelectionMarker,\n getRegularSelectionOffsets,\n mutateBlock,\n processChildNode,\n} from 'roosterjs-content-model-dom';\nimport type {\n ElementProcessor,\n DOMInsertPoint,\n FormatContentModelOptions,\n IEditor,\n InsertPoint,\n DomToModelContext,\n ContentModelBlockGroup,\n FormatContentModelContext,\n ShallowMutableContentModelDocument,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the content in a specific position using Content Model\n * @param editor The editor object\n * @param insertPoint The insert position.\n * @param callback The callback to insert the format.\n * @param options More options, @see FormatContentModelOptions\n */\nexport function formatInsertPointWithContentModel(\n editor: IEditor,\n insertPoint: DOMInsertPoint,\n callback: (\n model: ShallowMutableContentModelDocument,\n context: FormatContentModelContext,\n insertPoint?: InsertPoint\n ) => void,\n options?: FormatContentModelOptions\n) {\n const bundle: InsertPointBundle = {\n input: insertPoint,\n };\n\n editor.formatContentModel(\n (model, context) => {\n callback(model, context, bundle.result);\n\n if (bundle?.result) {\n const { paragraph, marker } = bundle.result;\n const index = paragraph.segments.indexOf(marker);\n\n if (index >= 0) {\n mutateBlock(paragraph).segments.splice(index, 1);\n }\n }\n return true;\n },\n options,\n {\n processorOverride: {\n child: getShadowChildProcessor(bundle),\n textWithSelection: getShadowTextProcessor(bundle),\n },\n tryGetFromCache: false,\n }\n );\n}\n\ninterface InsertPointBundle {\n input: DOMInsertPoint;\n result?: InsertPoint;\n}\n\ninterface DomToModelContextWithPath extends DomToModelContext {\n /**\n * Block group path of this insert point, from direct parent group to the root group\n */\n path?: ContentModelBlockGroup[];\n}\n\nfunction getShadowChildProcessor(bundle: InsertPointBundle): ElementProcessor<ParentNode> {\n return (group, parent, context) => {\n const contextWithPath = context as DomToModelContextWithPath;\n\n contextWithPath.path = contextWithPath.path || [];\n\n let shouldShiftPath = false;\n if (contextWithPath.path[0] != group) {\n contextWithPath.path.unshift(group);\n shouldShiftPath = true;\n }\n\n const offsets = getShadowSelectionOffsets(context, bundle, parent);\n let index = 0;\n\n for (let child = parent.firstChild; child; child = child.nextSibling) {\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n processChildNode(group, child, context);\n\n index++;\n }\n\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n if (shouldShiftPath) {\n contextWithPath.path.shift();\n }\n };\n}\n\nfunction handleElementShadowSelection(\n bundle: InsertPointBundle,\n index: number,\n context: DomToModelContext,\n group: ContentModelBlockGroup,\n offsets: [number, number, number],\n container?: Node\n) {\n if (\n index == offsets[2] &&\n (index <= offsets[0] || offsets[0] < 0) &&\n (index < offsets[1] || offsets[1] < 0)\n ) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[0]) {\n context.isInSelection = true;\n addSelectionMarker(group, context, container, index);\n }\n\n if (index == offsets[2] && (index < offsets[1] || offsets[1] < 0)) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[1]) {\n addSelectionMarker(group, context, container, index);\n context.isInSelection = false;\n }\n\n if (index == offsets[2]) {\n addSelectionMarker(group, context, container, index, bundle);\n }\n}\n\nconst getShadowTextProcessor = (bundle: InsertPointBundle): ElementProcessor<Text> => (\n group,\n textNode,\n context\n) => {\n let txt = textNode.nodeValue || '';\n const offsets = getShadowSelectionOffsets(context, bundle, textNode);\n const [start, end, shadow] = offsets;\n\n const handleTextSelection = (\n subtract: number,\n originalOffset: number,\n bundle?: InsertPointBundle\n ) => {\n addTextSegment(group, txt.substring(0, subtract), context);\n addSelectionMarker(group, context, textNode, originalOffset, bundle);\n\n offsets[0] -= subtract;\n offsets[1] -= subtract;\n offsets[2] = bundle ? -1 : offsets[2] - subtract;\n\n txt = txt.substring(subtract);\n };\n\n if (\n offsets[2] >= 0 &&\n (offsets[2] <= offsets[0] || offsets[0] < 0) &&\n (offsets[2] < offsets[1] || offsets[1] < 0)\n ) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[0] >= 0) {\n handleTextSelection(offsets[0], start);\n\n context.isInSelection = true;\n }\n\n if (offsets[2] >= 0 && offsets[2] > offsets[0] && (offsets[2] < offsets[1] || offsets[1] < 0)) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[1] >= 0) {\n handleTextSelection(offsets[1], end);\n\n context.isInSelection = false;\n }\n\n if (offsets[2] >= 0 && offsets[2] >= offsets[1]) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n addTextSegment(group, txt, context);\n};\n\nfunction addSelectionMarker(\n group: ContentModelBlockGroup,\n context: DomToModelContextWithPath,\n container?: Node,\n offset?: number,\n bundle?: InsertPointBundle\n) {\n const marker = buildSelectionMarker(group, context, container, offset);\n\n marker.isSelected = !bundle;\n\n const para = addSegment(group, marker, context.blockFormat, marker.format);\n\n if (bundle && context.path) {\n bundle.result = {\n path: [...context.path],\n paragraph: para,\n marker,\n };\n }\n}\n\nfunction getShadowSelectionOffsets(\n context: DomToModelContext,\n bundle: InsertPointBundle,\n currentContainer: Node\n): [number, number, number] {\n const [start, end] = getRegularSelectionOffsets(context, currentContainer);\n const shadow = bundle.input.node == currentContainer ? bundle.input.offset : -1;\n\n return [start, end, shadow];\n}\n"]}
1
+ {"version":3,"file":"formatInsertPointWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatInsertPointWithContentModel.ts"],"names":[],"mappings":";;;;AAAA,2EAOqC;AAarC;;;;;;GAMG;AACH,SAAgB,iCAAiC,CAC7C,MAAe,EACf,WAA2B,EAC3B,QAIS,EACT,OAAmC;IAEnC,IAAM,MAAM,GAAsB;QAC9B,KAAK,EAAE,WAAW;KACrB,CAAC;IAEF,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;QAExC,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,EAAE;YACV,IAAA,KAAwB,MAAM,CAAC,MAAM,EAAnC,SAAS,eAAA,EAAE,MAAM,YAAkB,CAAC;YAC5C,IAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAEjD,IAAI,KAAK,IAAI,CAAC,EAAE;gBACZ,IAAA,yCAAW,EAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,EA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xC,CAAC,EArDqF,CAqDrF,CAAC;AAEF,SAAS,kBAAkB,CACvB,KAA6B,EAC7B,OAAkC,EAClC,SAAgB,EAChB,MAAe,EACf,MAA0B;IAE1B,IAAM,MAAM,GAAG,IAAA,kDAAoB,EAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;IAEvE,MAAM,CAAC,UAAU,GAAG,CAAC,MAAM,CAAC;IAE5B,IAAM,IAAI,GAAG,IAAA,wCAAU,EAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAE3E,IAAI,MAAM,IAAI,OAAO,CAAC,IAAI,EAAE;QACxB,MAAM,CAAC,MAAM,GAAG;YACZ,IAAI,qDAAM,OAAO,CAAC,IAAI,SAAC;YACvB,SAAS,EAAE,IAAI;YACf,MAAM,QAAA;SACT,CAAC;KACL;AACL,CAAC;AAED,SAAS,yBAAyB,CAC9B,OAA0B,EAC1B,MAAyB,EACzB,gBAAsB;IAEhB,IAAA,KAAA,oBAAe,IAAA,wDAA0B,EAAC,OAAO,EAAE,gBAAgB,CAAC,IAAA,EAAnE,KAAK,QAAA,EAAE,GAAG,QAAyD,CAAC;IAC3E,IAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,IAAI,gBAAgB,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAEhF,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;AAChC,CAAC","sourcesContent":["import {\n addSegment,\n addTextSegment,\n buildSelectionMarker,\n getRegularSelectionOffsets,\n mutateBlock,\n processChildNode,\n} from 'roosterjs-content-model-dom';\nimport type {\n ElementProcessor,\n DOMInsertPoint,\n FormatContentModelOptions,\n IEditor,\n InsertPoint,\n DomToModelContext,\n ContentModelBlockGroup,\n FormatContentModelContext,\n ShallowMutableContentModelDocument,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the content in a specific position using Content Model\n * @param editor The editor object\n * @param insertPoint The insert position.\n * @param callback The callback to insert the format.\n * @param options More options, @see FormatContentModelOptions\n */\nexport function formatInsertPointWithContentModel(\n editor: IEditor,\n insertPoint: DOMInsertPoint,\n callback: (\n model: ShallowMutableContentModelDocument,\n context: FormatContentModelContext,\n insertPoint?: InsertPoint\n ) => void,\n options?: FormatContentModelOptions\n) {\n const bundle: InsertPointBundle = {\n input: insertPoint,\n };\n\n editor.formatContentModel(\n (model, context) => {\n callback(model, context, bundle.result);\n\n if (bundle?.result) {\n const { paragraph, marker } = bundle.result;\n const index = paragraph.segments.indexOf(marker);\n\n if (index >= 0) {\n mutateBlock(paragraph).segments.splice(index, 1);\n }\n }\n return true;\n },\n options,\n {\n processorOverride: {\n child: getShadowChildProcessor(bundle),\n textWithSelection: getShadowTextProcessor(bundle),\n },\n tryGetFromCache: false,\n // When an element carries \"container level\" styles such as margin or padding, we first\n // wrap it in a FormatContainer. After all its child nodes are processed, we decide whether\n // to keep the FormatContainer or fall back to a plain paragraph when it only wraps a single\n // paragraph. However, formatInsertPointWithContentModel persists the Content Model group path\n // during processing so the later formatting callback can still use it (see the\n // DomToModelContextWithPath interface below). If the FormatContainer falls back to a paragraph,\n // it is removed from the model and the persisted path becomes invalid. To keep the path valid,\n // we skip the fallback check here and always keep the FormatContainer when one is needed.\n skipFormatContainerFallbackCheck: true,\n }\n );\n}\n\ninterface InsertPointBundle {\n input: DOMInsertPoint;\n result?: InsertPoint;\n}\n\ninterface DomToModelContextWithPath extends DomToModelContext {\n /**\n * Block group path of this insert point, from direct parent group to the root group\n */\n path?: ContentModelBlockGroup[];\n}\n\nfunction getShadowChildProcessor(bundle: InsertPointBundle): ElementProcessor<ParentNode> {\n return (group, parent, context) => {\n const contextWithPath = context as DomToModelContextWithPath;\n\n contextWithPath.path = contextWithPath.path || [];\n\n let shouldShiftPath = false;\n if (contextWithPath.path[0] != group) {\n contextWithPath.path.unshift(group);\n shouldShiftPath = true;\n }\n\n const offsets = getShadowSelectionOffsets(context, bundle, parent);\n let index = 0;\n\n for (let child = parent.firstChild; child; child = child.nextSibling) {\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n processChildNode(group, child, context);\n\n index++;\n }\n\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n if (shouldShiftPath) {\n contextWithPath.path.shift();\n }\n };\n}\n\nfunction handleElementShadowSelection(\n bundle: InsertPointBundle,\n index: number,\n context: DomToModelContext,\n group: ContentModelBlockGroup,\n offsets: [number, number, number],\n container?: Node\n) {\n if (\n index == offsets[2] &&\n (index <= offsets[0] || offsets[0] < 0) &&\n (index < offsets[1] || offsets[1] < 0)\n ) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[0]) {\n context.isInSelection = true;\n addSelectionMarker(group, context, container, index);\n }\n\n if (index == offsets[2] && (index < offsets[1] || offsets[1] < 0)) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[1]) {\n addSelectionMarker(group, context, container, index);\n context.isInSelection = false;\n }\n\n if (index == offsets[2]) {\n addSelectionMarker(group, context, container, index, bundle);\n }\n}\n\nconst getShadowTextProcessor = (bundle: InsertPointBundle): ElementProcessor<Text> => (\n group,\n textNode,\n context\n) => {\n let txt = textNode.nodeValue || '';\n const offsets = getShadowSelectionOffsets(context, bundle, textNode);\n const [start, end, shadow] = offsets;\n\n const handleTextSelection = (\n subtract: number,\n originalOffset: number,\n bundle?: InsertPointBundle\n ) => {\n addTextSegment(group, txt.substring(0, subtract), context);\n addSelectionMarker(group, context, textNode, originalOffset, bundle);\n\n offsets[0] -= subtract;\n offsets[1] -= subtract;\n offsets[2] = bundle ? -1 : offsets[2] - subtract;\n\n txt = txt.substring(subtract);\n };\n\n if (\n offsets[2] >= 0 &&\n (offsets[2] <= offsets[0] || offsets[0] < 0) &&\n (offsets[2] < offsets[1] || offsets[1] < 0)\n ) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[0] >= 0) {\n handleTextSelection(offsets[0], start);\n\n context.isInSelection = true;\n }\n\n if (offsets[2] >= 0 && offsets[2] > offsets[0] && (offsets[2] < offsets[1] || offsets[1] < 0)) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[1] >= 0) {\n handleTextSelection(offsets[1], end);\n\n context.isInSelection = false;\n }\n\n if (offsets[2] >= 0 && offsets[2] >= offsets[1]) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n addTextSegment(group, txt, context);\n};\n\nfunction addSelectionMarker(\n group: ContentModelBlockGroup,\n context: DomToModelContextWithPath,\n container?: Node,\n offset?: number,\n bundle?: InsertPointBundle\n) {\n const marker = buildSelectionMarker(group, context, container, offset);\n\n marker.isSelected = !bundle;\n\n const para = addSegment(group, marker, context.blockFormat, marker.format);\n\n if (bundle && context.path) {\n bundle.result = {\n path: [...context.path],\n paragraph: para,\n marker,\n };\n }\n}\n\nfunction getShadowSelectionOffsets(\n context: DomToModelContext,\n bundle: InsertPointBundle,\n currentContainer: Node\n): [number, number, number] {\n const [start, end] = getRegularSelectionOffsets(context, currentContainer);\n const shadow = bundle.input.node == currentContainer ? bundle.input.offset : -1;\n\n return [start, end, shadow];\n}\n"]}
@@ -12,7 +12,7 @@ define(["require", "exports", "../utils/formatSegmentWithContentModel"], functio
12
12
  */
13
13
  function changeCapitalization(editor, capitalization, language) {
14
14
  editor.focus();
15
- (0, formatSegmentWithContentModel_1.formatSegmentWithContentModel)(editor, 'changeCapitalization', function (_, __, segment) {
15
+ (0, formatSegmentWithContentModel_1.formatSegmentWithContentModel)(editor, 'changeCapitalization', function (_, __, segment, paragraph) {
16
16
  if ((segment === null || segment === void 0 ? void 0 : segment.segmentType) == 'Text') {
17
17
  switch (capitalization) {
18
18
  case 'lowerCase':
@@ -23,7 +23,13 @@ define(["require", "exports", "../utils/formatSegmentWithContentModel"], functio
23
23
  break;
24
24
  case 'capitalize':
25
25
  var wordArray = segment.text.toLocaleLowerCase(language).split(' ');
26
- for (var i = 0; i < wordArray.length; i++) {
26
+ // When a collapsed selection is in the middle of a word, the word is split
27
+ // into multiple text segments around the selection marker. In that case the
28
+ // first segment of the word continues from a previous segment, so its first
29
+ // word must not be capitalized to avoid results like "HeLlo" for "he|llo".
30
+ var precedingChar = getPrecedingCharacter(paragraph, segment);
31
+ var startIndex = precedingChar && precedingChar != ' ' ? 1 : 0;
32
+ for (var i = startIndex; i < wordArray.length; i++) {
27
33
  wordArray[i] =
28
34
  wordArray[i].charAt(0).toLocaleUpperCase(language) +
29
35
  wordArray[i].slice(1);
@@ -47,5 +53,29 @@ define(["require", "exports", "../utils/formatSegmentWithContentModel"], functio
47
53
  });
48
54
  }
49
55
  exports.changeCapitalization = changeCapitalization;
56
+ /**
57
+ * Get the character immediately preceding the given segment within its paragraph, skipping
58
+ * selection markers (which carry no text). Returns an empty string if there is no preceding
59
+ * text character (e.g. the segment starts the paragraph or follows a non-text segment).
60
+ */
61
+ function getPrecedingCharacter(paragraph, segment) {
62
+ if (!paragraph) {
63
+ return '';
64
+ }
65
+ var index = paragraph.segments.indexOf(segment);
66
+ for (var i = index - 1; i >= 0; i--) {
67
+ var previous = paragraph.segments[i];
68
+ if (previous.segmentType == 'SelectionMarker') {
69
+ continue;
70
+ }
71
+ else if (previous.segmentType == 'Text' && previous.text.length > 0) {
72
+ return previous.text[previous.text.length - 1];
73
+ }
74
+ else {
75
+ break;
76
+ }
77
+ }
78
+ return '';
79
+ }
50
80
  });
51
81
  //# sourceMappingURL=changeCapitalization.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"changeCapitalization.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/segment/changeCapitalization.ts"],"names":[],"mappings":";;;;IAGA;;;;;;;OAOG;IACH,SAAgB,oBAAoB,CAChC,MAAe,EACf,cAAqE,EACrE,QAAiB;QAEjB,MAAM,CAAC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,IAAI,GAAG,OAAO,CAAC,IAAI;6BACtB,iBAAiB,CAAC,QAAQ,CAAC;6BAC3B,OAAO,CAAC,KAAK,EAAE,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,iBAAiB,CAAC,QAAQ,CAAC,EAAjC,CAAiC,CAAC,CAAC;wBAChE,MAAM;iBACb;aACJ;QACL,CAAC,CAAC,CAAC;IACP,CAAC;IA9CD,oDA8CC","sourcesContent":["import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Change the capitalization of text in the selection\n * @param editor The editor instance\n * @param capitalization The case option\n * @param language Optional parameter for language string that should comply to \"IETF BCP 47 Tags for\n * Identifying Languages\". For example: 'en' or 'en-US' for English, 'tr' for Turkish.\n * Default is the host environment’s current locale.\n */\nexport function changeCapitalization(\n editor: IEditor,\n capitalization: 'sentence' | 'lowerCase' | 'upperCase' | 'capitalize',\n language?: string\n) {\n editor.focus();\n\n formatSegmentWithContentModel(editor, 'changeCapitalization', (_, __, segment) => {\n if (segment?.segmentType == 'Text') {\n switch (capitalization) {\n case 'lowerCase':\n segment.text = segment.text.toLocaleLowerCase(language);\n break;\n\n case 'upperCase':\n segment.text = segment.text.toLocaleUpperCase(language);\n break;\n\n case 'capitalize':\n const wordArray = segment.text.toLocaleLowerCase(language).split(' ');\n\n for (let i = 0; i < wordArray.length; i++) {\n wordArray[i] =\n wordArray[i].charAt(0).toLocaleUpperCase(language) +\n wordArray[i].slice(1);\n }\n\n segment.text = wordArray.join(' ');\n break;\n\n case 'sentence':\n const punctuationMarks = '[\\\\.\\\\!\\\\?]';\n // Find a match of a word character either:\n // - At the beginning of a string with or without preceding whitespace, for\n // example: ' hello world' and 'hello world' strings would both match 'h'.\n // - Or preceded by a punctuation mark and at least one whitespace, for\n // example 'yes. hello world' would match 'y' and 'h'.\n const regex = new RegExp('^\\\\s*\\\\w|' + punctuationMarks + '\\\\s+\\\\w', 'g');\n\n segment.text = segment.text\n .toLocaleLowerCase(language)\n .replace(regex, match => match.toLocaleUpperCase(language));\n break;\n }\n }\n });\n}\n"]}
1
+ {"version":3,"file":"changeCapitalization.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/segment/changeCapitalization.ts"],"names":[],"mappings":";;;;IAOA;;;;;;;OAOG;IACH,SAAgB,oBAAoB,CAChC,MAAe,EACf,cAAqE,EACrE,QAAiB;QAEjB,MAAM,CAAC,KAAK,EAAE,CAAC;QAEf,IAAA,6DAA6B,EAAC,MAAM,EAAE,sBAAsB,EAAE,UAAC,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,SAAS;YACpF,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,WAAW,KAAI,MAAM,EAAE;gBAChC,QAAQ,cAAc,EAAE;oBACpB,KAAK,WAAW;wBACZ,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;wBACxD,MAAM;oBAEV,KAAK,WAAW;wBACZ,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;wBACxD,MAAM;oBAEV,KAAK,YAAY;wBACb,IAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;wBAEtE,2EAA2E;wBAC3E,4EAA4E;wBAC5E,4EAA4E;wBAC5E,2EAA2E;wBAC3E,IAAM,aAAa,GAAG,qBAAqB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;wBAChE,IAAM,UAAU,GAAG,aAAa,IAAI,aAAa,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;wBAEjE,KAAK,IAAI,CAAC,GAAG,UAAU,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,EA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C3C,SAAS;aACZ;iBAAM,IAAI,QAAQ,CAAC,WAAW,IAAI,MAAM,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;gBACnE,OAAO,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;aAClD;iBAAM;gBACH,MAAM;aACT;SACJ;QAED,OAAO,EAAE,CAAC;IACd,CAAC","sourcesContent":["import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';\nimport type {\n IEditor,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Change the capitalization of text in the selection\n * @param editor The editor instance\n * @param capitalization The case option\n * @param language Optional parameter for language string that should comply to \"IETF BCP 47 Tags for\n * Identifying Languages\". For example: 'en' or 'en-US' for English, 'tr' for Turkish.\n * Default is the host environment’s current locale.\n */\nexport function changeCapitalization(\n editor: IEditor,\n capitalization: 'sentence' | 'lowerCase' | 'upperCase' | 'capitalize',\n language?: string\n) {\n editor.focus();\n\n formatSegmentWithContentModel(editor, 'changeCapitalization', (_, __, segment, paragraph) => {\n if (segment?.segmentType == 'Text') {\n switch (capitalization) {\n case 'lowerCase':\n segment.text = segment.text.toLocaleLowerCase(language);\n break;\n\n case 'upperCase':\n segment.text = segment.text.toLocaleUpperCase(language);\n break;\n\n case 'capitalize':\n const wordArray = segment.text.toLocaleLowerCase(language).split(' ');\n\n // When a collapsed selection is in the middle of a word, the word is split\n // into multiple text segments around the selection marker. In that case the\n // first segment of the word continues from a previous segment, so its first\n // word must not be capitalized to avoid results like \"HeLlo\" for \"he|llo\".\n const precedingChar = getPrecedingCharacter(paragraph, segment);\n const startIndex = precedingChar && precedingChar != ' ' ? 1 : 0;\n\n for (let i = startIndex; i < wordArray.length; i++) {\n wordArray[i] =\n wordArray[i].charAt(0).toLocaleUpperCase(language) +\n wordArray[i].slice(1);\n }\n\n segment.text = wordArray.join(' ');\n break;\n\n case 'sentence':\n const punctuationMarks = '[\\\\.\\\\!\\\\?]';\n // Find a match of a word character either:\n // - At the beginning of a string with or without preceding whitespace, for\n // example: ' hello world' and 'hello world' strings would both match 'h'.\n // - Or preceded by a punctuation mark and at least one whitespace, for\n // example 'yes. hello world' would match 'y' and 'h'.\n const regex = new RegExp('^\\\\s*\\\\w|' + punctuationMarks + '\\\\s+\\\\w', 'g');\n\n segment.text = segment.text\n .toLocaleLowerCase(language)\n .replace(regex, match => match.toLocaleUpperCase(language));\n break;\n }\n }\n });\n}\n\n/**\n * Get the character immediately preceding the given segment within its paragraph, skipping\n * selection markers (which carry no text). Returns an empty string if there is no preceding\n * text character (e.g. the segment starts the paragraph or follows a non-text segment).\n */\nfunction getPrecedingCharacter(\n paragraph: ShallowMutableContentModelParagraph | null,\n segment: ShallowMutableContentModelSegment\n): string {\n if (!paragraph) {\n return '';\n }\n\n const index = paragraph.segments.indexOf(segment);\n\n for (let i = index - 1; i >= 0; i--) {\n const previous = paragraph.segments[i];\n\n if (previous.segmentType == 'SelectionMarker') {\n continue;\n } else if (previous.segmentType == 'Text' && previous.text.length > 0) {\n return previous.text[previous.text.length - 1];\n } else {\n break;\n }\n }\n\n return '';\n}\n"]}
@@ -29,6 +29,15 @@ define(["require", "exports", "tslib", "roosterjs-content-model-dom"], function
29
29
  textWithSelection: getShadowTextProcessor(bundle),
30
30
  },
31
31
  tryGetFromCache: false,
32
+ // When an element carries "container level" styles such as margin or padding, we first
33
+ // wrap it in a FormatContainer. After all its child nodes are processed, we decide whether
34
+ // to keep the FormatContainer or fall back to a plain paragraph when it only wraps a single
35
+ // paragraph. However, formatInsertPointWithContentModel persists the Content Model group path
36
+ // during processing so the later formatting callback can still use it (see the
37
+ // DomToModelContextWithPath interface below). If the FormatContainer falls back to a paragraph,
38
+ // it is removed from the model and the persisted path becomes invalid. To keep the path valid,
39
+ // we skip the fallback check here and always keep the FormatContainer when one is needed.
40
+ skipFormatContainerFallbackCheck: true,
32
41
  });
33
42
  }
34
43
  exports.formatInsertPointWithContentModel = formatInsertPointWithContentModel;
@@ -1 +1 @@
1
- {"version":3,"file":"formatInsertPointWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatInsertPointWithContentModel.ts"],"names":[],"mappings":";;;;IAoBA;;;;;;OAMG;IACH,SAAgB,iCAAiC,CAC7C,MAAe,EACf,WAA2B,EAC3B,QAIS,EACT,OAAmC;QAEnC,IAAM,MAAM,GAAsB;YAC9B,KAAK,EAAE,WAAW;SACrB,CAAC;QAEF,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;YACX,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;YAExC,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,EAAE;gBACV,IAAA,KAAwB,MAAM,CAAC,MAAM,EAAnC,SAAS,eAAA,EAAE,MAAM,YAAkB,CAAC;gBAC5C,IAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAEjD,IAAI,KAAK,IAAI,CAAC,EAAE;oBACZ,IAAA,yCAAW,EAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;iBACpD;aACJ;YACD,OAAO,IAAI,CAAC;QAChB,CAAC,EACD,OAAO,EACP;YACI,iBAAiB,EAAE;gBACf,KAAK,EAAE,uBAAuB,CAAC,MAAM,CAAC;gBACtC,iBAAiB,EAAE,sBAAsB,CAAC,MAAM,CAAC;aACpD;YACD,eAAe,EAAE,KAAK;SACzB,CACJ,CAAC;IACN,CAAC;IArCD,8EAqCC;IAcD,SAAS,uBAAuB,CAAC,MAAyB;QACtD,OAAO,UAAC,KAAK,EAAE,MAAM,EAAE,OAAO;YAC1B,IAAM,eAAe,GAAG,OAAoC,CAAC;YAE7D,eAAe,CAAC,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,QAAA;aACT,CAAC;SACL;IACL,CAAC;IAED,SAAS,yBAAyB,CAC9B,OAA0B,EAC1B,MAAyB,EACzB,gBAAsB;QAEhB,IAAA,KAAA,oBAAe,IAAA,wDAA0B,EAAC,OAAO,EAAE,gBAAgB,CAAC,IAAA,EAAnE,KAAK,QAAA,EAAE,GAAG,QAAyD,CAAC;QAC3E,IAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,IAAI,gBAAgB,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAEhF,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;IAChC,CAAC","sourcesContent":["import {\n addSegment,\n addTextSegment,\n buildSelectionMarker,\n getRegularSelectionOffsets,\n mutateBlock,\n processChildNode,\n} from 'roosterjs-content-model-dom';\nimport type {\n ElementProcessor,\n DOMInsertPoint,\n FormatContentModelOptions,\n IEditor,\n InsertPoint,\n DomToModelContext,\n ContentModelBlockGroup,\n FormatContentModelContext,\n ShallowMutableContentModelDocument,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the content in a specific position using Content Model\n * @param editor The editor object\n * @param insertPoint The insert position.\n * @param callback The callback to insert the format.\n * @param options More options, @see FormatContentModelOptions\n */\nexport function formatInsertPointWithContentModel(\n editor: IEditor,\n insertPoint: DOMInsertPoint,\n callback: (\n model: ShallowMutableContentModelDocument,\n context: FormatContentModelContext,\n insertPoint?: InsertPoint\n ) => void,\n options?: FormatContentModelOptions\n) {\n const bundle: InsertPointBundle = {\n input: insertPoint,\n };\n\n editor.formatContentModel(\n (model, context) => {\n callback(model, context, bundle.result);\n\n if (bundle?.result) {\n const { paragraph, marker } = bundle.result;\n const index = paragraph.segments.indexOf(marker);\n\n if (index >= 0) {\n mutateBlock(paragraph).segments.splice(index, 1);\n }\n }\n return true;\n },\n options,\n {\n processorOverride: {\n child: getShadowChildProcessor(bundle),\n textWithSelection: getShadowTextProcessor(bundle),\n },\n tryGetFromCache: false,\n }\n );\n}\n\ninterface InsertPointBundle {\n input: DOMInsertPoint;\n result?: InsertPoint;\n}\n\ninterface DomToModelContextWithPath extends DomToModelContext {\n /**\n * Block group path of this insert point, from direct parent group to the root group\n */\n path?: ContentModelBlockGroup[];\n}\n\nfunction getShadowChildProcessor(bundle: InsertPointBundle): ElementProcessor<ParentNode> {\n return (group, parent, context) => {\n const contextWithPath = context as DomToModelContextWithPath;\n\n contextWithPath.path = contextWithPath.path || [];\n\n let shouldShiftPath = false;\n if (contextWithPath.path[0] != group) {\n contextWithPath.path.unshift(group);\n shouldShiftPath = true;\n }\n\n const offsets = getShadowSelectionOffsets(context, bundle, parent);\n let index = 0;\n\n for (let child = parent.firstChild; child; child = child.nextSibling) {\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n processChildNode(group, child, context);\n\n index++;\n }\n\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n if (shouldShiftPath) {\n contextWithPath.path.shift();\n }\n };\n}\n\nfunction handleElementShadowSelection(\n bundle: InsertPointBundle,\n index: number,\n context: DomToModelContext,\n group: ContentModelBlockGroup,\n offsets: [number, number, number],\n container?: Node\n) {\n if (\n index == offsets[2] &&\n (index <= offsets[0] || offsets[0] < 0) &&\n (index < offsets[1] || offsets[1] < 0)\n ) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[0]) {\n context.isInSelection = true;\n addSelectionMarker(group, context, container, index);\n }\n\n if (index == offsets[2] && (index < offsets[1] || offsets[1] < 0)) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[1]) {\n addSelectionMarker(group, context, container, index);\n context.isInSelection = false;\n }\n\n if (index == offsets[2]) {\n addSelectionMarker(group, context, container, index, bundle);\n }\n}\n\nconst getShadowTextProcessor = (bundle: InsertPointBundle): ElementProcessor<Text> => (\n group,\n textNode,\n context\n) => {\n let txt = textNode.nodeValue || '';\n const offsets = getShadowSelectionOffsets(context, bundle, textNode);\n const [start, end, shadow] = offsets;\n\n const handleTextSelection = (\n subtract: number,\n originalOffset: number,\n bundle?: InsertPointBundle\n ) => {\n addTextSegment(group, txt.substring(0, subtract), context);\n addSelectionMarker(group, context, textNode, originalOffset, bundle);\n\n offsets[0] -= subtract;\n offsets[1] -= subtract;\n offsets[2] = bundle ? -1 : offsets[2] - subtract;\n\n txt = txt.substring(subtract);\n };\n\n if (\n offsets[2] >= 0 &&\n (offsets[2] <= offsets[0] || offsets[0] < 0) &&\n (offsets[2] < offsets[1] || offsets[1] < 0)\n ) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[0] >= 0) {\n handleTextSelection(offsets[0], start);\n\n context.isInSelection = true;\n }\n\n if (offsets[2] >= 0 && offsets[2] > offsets[0] && (offsets[2] < offsets[1] || offsets[1] < 0)) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[1] >= 0) {\n handleTextSelection(offsets[1], end);\n\n context.isInSelection = false;\n }\n\n if (offsets[2] >= 0 && offsets[2] >= offsets[1]) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n addTextSegment(group, txt, context);\n};\n\nfunction addSelectionMarker(\n group: ContentModelBlockGroup,\n context: DomToModelContextWithPath,\n container?: Node,\n offset?: number,\n bundle?: InsertPointBundle\n) {\n const marker = buildSelectionMarker(group, context, container, offset);\n\n marker.isSelected = !bundle;\n\n const para = addSegment(group, marker, context.blockFormat, marker.format);\n\n if (bundle && context.path) {\n bundle.result = {\n path: [...context.path],\n paragraph: para,\n marker,\n };\n }\n}\n\nfunction getShadowSelectionOffsets(\n context: DomToModelContext,\n bundle: InsertPointBundle,\n currentContainer: Node\n): [number, number, number] {\n const [start, end] = getRegularSelectionOffsets(context, currentContainer);\n const shadow = bundle.input.node == currentContainer ? bundle.input.offset : -1;\n\n return [start, end, shadow];\n}\n"]}
1
+ {"version":3,"file":"formatInsertPointWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatInsertPointWithContentModel.ts"],"names":[],"mappings":";;;;IAoBA;;;;;;OAMG;IACH,SAAgB,iCAAiC,CAC7C,MAAe,EACf,WAA2B,EAC3B,QAIS,EACT,OAAmC;QAEnC,IAAM,MAAM,GAAsB;YAC9B,KAAK,EAAE,WAAW;SACrB,CAAC;QAEF,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;YACX,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;YAExC,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,EAAE;gBACV,IAAA,KAAwB,MAAM,CAAC,MAAM,EAAnC,SAAS,eAAA,EAAE,MAAM,YAAkB,CAAC;gBAC5C,IAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAEjD,IAAI,KAAK,IAAI,CAAC,EAAE;oBACZ,IAAA,yCAAW,EAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;iBACpD;aACJ;YACD,OAAO,IAAI,CAAC;QAChB,CAAC,EACD,OAAO,EACP;YACI,iBAAiB,EAAE;gBACf,KAAK,EAAE,uBAAuB,CAAC,MAAM,CAAC;gBACtC,iBAAiB,EAAE,sBAAsB,CAAC,MAAM,CAAC;aACpD;YACD,eAAe,EAAE,KAAK;YACtB,uFAAuF;YACvF,2FAA2F;YAC3F,4FAA4F;YAC5F,8FAA8F;YAC9F,+EAA+E;YAC/E,gGAAgG;YAChG,+FAA+F;YAC/F,0FAA0F;YAC1F,gCAAgC,EAAE,IAAI;SACzC,CACJ,CAAC;IACN,CAAC;IA9CD,8EA8CC;IAcD,SAAS,uBAAuB,CAAC,MAAyB;QACtD,OAAO,UAAC,KAAK,EAAE,MAAM,EAAE,OAAO;YAC1B,IA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IAAI,SAAC;gBACvB,SAAS,EAAE,IAAI;gBACf,MAAM,QAAA;aACT,CAAC;SACL;IACL,CAAC;IAED,SAAS,yBAAyB,CAC9B,OAA0B,EAC1B,MAAyB,EACzB,gBAAsB;QAEhB,IAAA,KAAA,oBAAe,IAAA,wDAA0B,EAAC,OAAO,EAAE,gBAAgB,CAAC,IAAA,EAAnE,KAAK,QAAA,EAAE,GAAG,QAAyD,CAAC;QAC3E,IAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,IAAI,gBAAgB,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAEhF,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;IAChC,CAAC","sourcesContent":["import {\n addSegment,\n addTextSegment,\n buildSelectionMarker,\n getRegularSelectionOffsets,\n mutateBlock,\n processChildNode,\n} from 'roosterjs-content-model-dom';\nimport type {\n ElementProcessor,\n DOMInsertPoint,\n FormatContentModelOptions,\n IEditor,\n InsertPoint,\n DomToModelContext,\n ContentModelBlockGroup,\n FormatContentModelContext,\n ShallowMutableContentModelDocument,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the content in a specific position using Content Model\n * @param editor The editor object\n * @param insertPoint The insert position.\n * @param callback The callback to insert the format.\n * @param options More options, @see FormatContentModelOptions\n */\nexport function formatInsertPointWithContentModel(\n editor: IEditor,\n insertPoint: DOMInsertPoint,\n callback: (\n model: ShallowMutableContentModelDocument,\n context: FormatContentModelContext,\n insertPoint?: InsertPoint\n ) => void,\n options?: FormatContentModelOptions\n) {\n const bundle: InsertPointBundle = {\n input: insertPoint,\n };\n\n editor.formatContentModel(\n (model, context) => {\n callback(model, context, bundle.result);\n\n if (bundle?.result) {\n const { paragraph, marker } = bundle.result;\n const index = paragraph.segments.indexOf(marker);\n\n if (index >= 0) {\n mutateBlock(paragraph).segments.splice(index, 1);\n }\n }\n return true;\n },\n options,\n {\n processorOverride: {\n child: getShadowChildProcessor(bundle),\n textWithSelection: getShadowTextProcessor(bundle),\n },\n tryGetFromCache: false,\n // When an element carries \"container level\" styles such as margin or padding, we first\n // wrap it in a FormatContainer. After all its child nodes are processed, we decide whether\n // to keep the FormatContainer or fall back to a plain paragraph when it only wraps a single\n // paragraph. However, formatInsertPointWithContentModel persists the Content Model group path\n // during processing so the later formatting callback can still use it (see the\n // DomToModelContextWithPath interface below). If the FormatContainer falls back to a paragraph,\n // it is removed from the model and the persisted path becomes invalid. To keep the path valid,\n // we skip the fallback check here and always keep the FormatContainer when one is needed.\n skipFormatContainerFallbackCheck: true,\n }\n );\n}\n\ninterface InsertPointBundle {\n input: DOMInsertPoint;\n result?: InsertPoint;\n}\n\ninterface DomToModelContextWithPath extends DomToModelContext {\n /**\n * Block group path of this insert point, from direct parent group to the root group\n */\n path?: ContentModelBlockGroup[];\n}\n\nfunction getShadowChildProcessor(bundle: InsertPointBundle): ElementProcessor<ParentNode> {\n return (group, parent, context) => {\n const contextWithPath = context as DomToModelContextWithPath;\n\n contextWithPath.path = contextWithPath.path || [];\n\n let shouldShiftPath = false;\n if (contextWithPath.path[0] != group) {\n contextWithPath.path.unshift(group);\n shouldShiftPath = true;\n }\n\n const offsets = getShadowSelectionOffsets(context, bundle, parent);\n let index = 0;\n\n for (let child = parent.firstChild; child; child = child.nextSibling) {\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n processChildNode(group, child, context);\n\n index++;\n }\n\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n if (shouldShiftPath) {\n contextWithPath.path.shift();\n }\n };\n}\n\nfunction handleElementShadowSelection(\n bundle: InsertPointBundle,\n index: number,\n context: DomToModelContext,\n group: ContentModelBlockGroup,\n offsets: [number, number, number],\n container?: Node\n) {\n if (\n index == offsets[2] &&\n (index <= offsets[0] || offsets[0] < 0) &&\n (index < offsets[1] || offsets[1] < 0)\n ) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[0]) {\n context.isInSelection = true;\n addSelectionMarker(group, context, container, index);\n }\n\n if (index == offsets[2] && (index < offsets[1] || offsets[1] < 0)) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[1]) {\n addSelectionMarker(group, context, container, index);\n context.isInSelection = false;\n }\n\n if (index == offsets[2]) {\n addSelectionMarker(group, context, container, index, bundle);\n }\n}\n\nconst getShadowTextProcessor = (bundle: InsertPointBundle): ElementProcessor<Text> => (\n group,\n textNode,\n context\n) => {\n let txt = textNode.nodeValue || '';\n const offsets = getShadowSelectionOffsets(context, bundle, textNode);\n const [start, end, shadow] = offsets;\n\n const handleTextSelection = (\n subtract: number,\n originalOffset: number,\n bundle?: InsertPointBundle\n ) => {\n addTextSegment(group, txt.substring(0, subtract), context);\n addSelectionMarker(group, context, textNode, originalOffset, bundle);\n\n offsets[0] -= subtract;\n offsets[1] -= subtract;\n offsets[2] = bundle ? -1 : offsets[2] - subtract;\n\n txt = txt.substring(subtract);\n };\n\n if (\n offsets[2] >= 0 &&\n (offsets[2] <= offsets[0] || offsets[0] < 0) &&\n (offsets[2] < offsets[1] || offsets[1] < 0)\n ) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[0] >= 0) {\n handleTextSelection(offsets[0], start);\n\n context.isInSelection = true;\n }\n\n if (offsets[2] >= 0 && offsets[2] > offsets[0] && (offsets[2] < offsets[1] || offsets[1] < 0)) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[1] >= 0) {\n handleTextSelection(offsets[1], end);\n\n context.isInSelection = false;\n }\n\n if (offsets[2] >= 0 && offsets[2] >= offsets[1]) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n addTextSegment(group, txt, context);\n};\n\nfunction addSelectionMarker(\n group: ContentModelBlockGroup,\n context: DomToModelContextWithPath,\n container?: Node,\n offset?: number,\n bundle?: InsertPointBundle\n) {\n const marker = buildSelectionMarker(group, context, container, offset);\n\n marker.isSelected = !bundle;\n\n const para = addSegment(group, marker, context.blockFormat, marker.format);\n\n if (bundle && context.path) {\n bundle.result = {\n path: [...context.path],\n paragraph: para,\n marker,\n };\n }\n}\n\nfunction getShadowSelectionOffsets(\n context: DomToModelContext,\n bundle: InsertPointBundle,\n currentContainer: Node\n): [number, number, number] {\n const [start, end] = getRegularSelectionOffsets(context, currentContainer);\n const shadow = bundle.input.node == currentContainer ? bundle.input.offset : -1;\n\n return [start, end, shadow];\n}\n"]}
@@ -9,7 +9,7 @@ import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContent
9
9
  */
10
10
  export function changeCapitalization(editor, capitalization, language) {
11
11
  editor.focus();
12
- formatSegmentWithContentModel(editor, 'changeCapitalization', function (_, __, segment) {
12
+ formatSegmentWithContentModel(editor, 'changeCapitalization', function (_, __, segment, paragraph) {
13
13
  if ((segment === null || segment === void 0 ? void 0 : segment.segmentType) == 'Text') {
14
14
  switch (capitalization) {
15
15
  case 'lowerCase':
@@ -20,7 +20,13 @@ export function changeCapitalization(editor, capitalization, language) {
20
20
  break;
21
21
  case 'capitalize':
22
22
  var wordArray = segment.text.toLocaleLowerCase(language).split(' ');
23
- for (var i = 0; i < wordArray.length; i++) {
23
+ // When a collapsed selection is in the middle of a word, the word is split
24
+ // into multiple text segments around the selection marker. In that case the
25
+ // first segment of the word continues from a previous segment, so its first
26
+ // word must not be capitalized to avoid results like "HeLlo" for "he|llo".
27
+ var precedingChar = getPrecedingCharacter(paragraph, segment);
28
+ var startIndex = precedingChar && precedingChar != ' ' ? 1 : 0;
29
+ for (var i = startIndex; i < wordArray.length; i++) {
24
30
  wordArray[i] =
25
31
  wordArray[i].charAt(0).toLocaleUpperCase(language) +
26
32
  wordArray[i].slice(1);
@@ -43,4 +49,28 @@ export function changeCapitalization(editor, capitalization, language) {
43
49
  }
44
50
  });
45
51
  }
52
+ /**
53
+ * Get the character immediately preceding the given segment within its paragraph, skipping
54
+ * selection markers (which carry no text). Returns an empty string if there is no preceding
55
+ * text character (e.g. the segment starts the paragraph or follows a non-text segment).
56
+ */
57
+ function getPrecedingCharacter(paragraph, segment) {
58
+ if (!paragraph) {
59
+ return '';
60
+ }
61
+ var index = paragraph.segments.indexOf(segment);
62
+ for (var i = index - 1; i >= 0; i--) {
63
+ var previous = paragraph.segments[i];
64
+ if (previous.segmentType == 'SelectionMarker') {
65
+ continue;
66
+ }
67
+ else if (previous.segmentType == 'Text' && previous.text.length > 0) {
68
+ return previous.text[previous.text.length - 1];
69
+ }
70
+ else {
71
+ break;
72
+ }
73
+ }
74
+ return '';
75
+ }
46
76
  //# sourceMappingURL=changeCapitalization.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"changeCapitalization.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/segment/changeCapitalization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAGvF;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAChC,MAAe,EACf,cAAqE,EACrE,QAAiB;IAEjB,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,6BAA6B,CAAC,MAAM,EAAE,sBAAsB,EAAE,UAAC,CAAC,EAAE,EAAE,EAAE,OAAO;QACzE,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,WAAW,KAAI,MAAM,EAAE;YAChC,QAAQ,cAAc,EAAE;gBACpB,KAAK,WAAW;oBACZ,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;oBACxD,MAAM;gBAEV,KAAK,WAAW;oBACZ,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;oBACxD,MAAM;gBAEV,KAAK,YAAY;oBACb,IAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;oBAEtE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;wBACvC,SAAS,CAAC,CAAC,CAAC;4BACR,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,QAAQ,CAAC;gCAClD,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;qBAC7B;oBAED,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;oBACnC,MAAM;gBAEV,KAAK,UAAU;oBACX,IAAM,gBAAgB,GAAG,aAAa,CAAC;oBACvC,2CAA2C;oBAC3C,2EAA2E;oBAC3E,2EAA2E;oBAC3E,uEAAuE;oBACvE,sDAAsD;oBACtD,IAAM,KAAK,GAAG,IAAI,MAAM,CAAC,WAAW,GAAG,gBAAgB,GAAG,SAAS,EAAE,GAAG,CAAC,CAAC;oBAE1E,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI;yBACtB,iBAAiB,CAAC,QAAQ,CAAC;yBAC3B,OAAO,CAAC,KAAK,EAAE,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,iBAAiB,CAAC,QAAQ,CAAC,EAAjC,CAAiC,CAAC,CAAC;oBAChE,MAAM;aACb;SACJ;IACL,CAAC,CAAC,CAAC;AACP,CAAC","sourcesContent":["import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Change the capitalization of text in the selection\n * @param editor The editor instance\n * @param capitalization The case option\n * @param language Optional parameter for language string that should comply to \"IETF BCP 47 Tags for\n * Identifying Languages\". For example: 'en' or 'en-US' for English, 'tr' for Turkish.\n * Default is the host environment’s current locale.\n */\nexport function changeCapitalization(\n editor: IEditor,\n capitalization: 'sentence' | 'lowerCase' | 'upperCase' | 'capitalize',\n language?: string\n) {\n editor.focus();\n\n formatSegmentWithContentModel(editor, 'changeCapitalization', (_, __, segment) => {\n if (segment?.segmentType == 'Text') {\n switch (capitalization) {\n case 'lowerCase':\n segment.text = segment.text.toLocaleLowerCase(language);\n break;\n\n case 'upperCase':\n segment.text = segment.text.toLocaleUpperCase(language);\n break;\n\n case 'capitalize':\n const wordArray = segment.text.toLocaleLowerCase(language).split(' ');\n\n for (let i = 0; i < wordArray.length; i++) {\n wordArray[i] =\n wordArray[i].charAt(0).toLocaleUpperCase(language) +\n wordArray[i].slice(1);\n }\n\n segment.text = wordArray.join(' ');\n break;\n\n case 'sentence':\n const punctuationMarks = '[\\\\.\\\\!\\\\?]';\n // Find a match of a word character either:\n // - At the beginning of a string with or without preceding whitespace, for\n // example: ' hello world' and 'hello world' strings would both match 'h'.\n // - Or preceded by a punctuation mark and at least one whitespace, for\n // example 'yes. hello world' would match 'y' and 'h'.\n const regex = new RegExp('^\\\\s*\\\\w|' + punctuationMarks + '\\\\s+\\\\w', 'g');\n\n segment.text = segment.text\n .toLocaleLowerCase(language)\n .replace(regex, match => match.toLocaleUpperCase(language));\n break;\n }\n }\n });\n}\n"]}
1
+ {"version":3,"file":"changeCapitalization.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/segment/changeCapitalization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAOvF;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAChC,MAAe,EACf,cAAqE,EACrE,QAAiB;IAEjB,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,6BAA6B,CAAC,MAAM,EAAE,sBAAsB,EAAE,UAAC,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,SAAS;QACpF,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,WAAW,KAAI,MAAM,EAAE;YAChC,QAAQ,cAAc,EAA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AAG,CAAC,CAAC;oBAE1E,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI;yBACtB,iBAAiB,CAAC,QAAQ,CAAC;yBAC3B,OAAO,CAAC,KAAK,EAAE,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,iBAAiB,CAAC,QAAQ,CAAC,EAAjC,CAAiC,CAAC,CAAC;oBAChE,MAAM;aACb;SACJ;IACL,CAAC,CAAC,CAAC;AACP,CAAC;AAED;;;;GAIG;AACH,SAAS,qBAAqB,CAC1B,SAAqD,EACrD,OAA0C;IAE1C,IAAI,CAAC,SAAS,EAAE;QACZ,OAAO,EAAE,CAAC;KACb;IAED,IAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAElD,KAAK,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;QACjC,IAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QAEvC,IAAI,QAAQ,CAAC,WAAW,IAAI,iBAAiB,EAAE;YAC3C,SAAS;SACZ;aAAM,IAAI,QAAQ,CAAC,WAAW,IAAI,MAAM,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;YACnE,OAAO,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;SAClD;aAAM;YACH,MAAM;SACT;KACJ;IAED,OAAO,EAAE,CAAC;AACd,CAAC","sourcesContent":["import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';\nimport type {\n IEditor,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Change the capitalization of text in the selection\n * @param editor The editor instance\n * @param capitalization The case option\n * @param language Optional parameter for language string that should comply to \"IETF BCP 47 Tags for\n * Identifying Languages\". For example: 'en' or 'en-US' for English, 'tr' for Turkish.\n * Default is the host environment’s current locale.\n */\nexport function changeCapitalization(\n editor: IEditor,\n capitalization: 'sentence' | 'lowerCase' | 'upperCase' | 'capitalize',\n language?: string\n) {\n editor.focus();\n\n formatSegmentWithContentModel(editor, 'changeCapitalization', (_, __, segment, paragraph) => {\n if (segment?.segmentType == 'Text') {\n switch (capitalization) {\n case 'lowerCase':\n segment.text = segment.text.toLocaleLowerCase(language);\n break;\n\n case 'upperCase':\n segment.text = segment.text.toLocaleUpperCase(language);\n break;\n\n case 'capitalize':\n const wordArray = segment.text.toLocaleLowerCase(language).split(' ');\n\n // When a collapsed selection is in the middle of a word, the word is split\n // into multiple text segments around the selection marker. In that case the\n // first segment of the word continues from a previous segment, so its first\n // word must not be capitalized to avoid results like \"HeLlo\" for \"he|llo\".\n const precedingChar = getPrecedingCharacter(paragraph, segment);\n const startIndex = precedingChar && precedingChar != ' ' ? 1 : 0;\n\n for (let i = startIndex; i < wordArray.length; i++) {\n wordArray[i] =\n wordArray[i].charAt(0).toLocaleUpperCase(language) +\n wordArray[i].slice(1);\n }\n\n segment.text = wordArray.join(' ');\n break;\n\n case 'sentence':\n const punctuationMarks = '[\\\\.\\\\!\\\\?]';\n // Find a match of a word character either:\n // - At the beginning of a string with or without preceding whitespace, for\n // example: ' hello world' and 'hello world' strings would both match 'h'.\n // - Or preceded by a punctuation mark and at least one whitespace, for\n // example 'yes. hello world' would match 'y' and 'h'.\n const regex = new RegExp('^\\\\s*\\\\w|' + punctuationMarks + '\\\\s+\\\\w', 'g');\n\n segment.text = segment.text\n .toLocaleLowerCase(language)\n .replace(regex, match => match.toLocaleUpperCase(language));\n break;\n }\n }\n });\n}\n\n/**\n * Get the character immediately preceding the given segment within its paragraph, skipping\n * selection markers (which carry no text). Returns an empty string if there is no preceding\n * text character (e.g. the segment starts the paragraph or follows a non-text segment).\n */\nfunction getPrecedingCharacter(\n paragraph: ShallowMutableContentModelParagraph | null,\n segment: ShallowMutableContentModelSegment\n): string {\n if (!paragraph) {\n return '';\n }\n\n const index = paragraph.segments.indexOf(segment);\n\n for (let i = index - 1; i >= 0; i--) {\n const previous = paragraph.segments[i];\n\n if (previous.segmentType == 'SelectionMarker') {\n continue;\n } else if (previous.segmentType == 'Text' && previous.text.length > 0) {\n return previous.text[previous.text.length - 1];\n } else {\n break;\n }\n }\n\n return '';\n}\n"]}
@@ -27,6 +27,15 @@ export function formatInsertPointWithContentModel(editor, insertPoint, callback,
27
27
  textWithSelection: getShadowTextProcessor(bundle),
28
28
  },
29
29
  tryGetFromCache: false,
30
+ // When an element carries "container level" styles such as margin or padding, we first
31
+ // wrap it in a FormatContainer. After all its child nodes are processed, we decide whether
32
+ // to keep the FormatContainer or fall back to a plain paragraph when it only wraps a single
33
+ // paragraph. However, formatInsertPointWithContentModel persists the Content Model group path
34
+ // during processing so the later formatting callback can still use it (see the
35
+ // DomToModelContextWithPath interface below). If the FormatContainer falls back to a paragraph,
36
+ // it is removed from the model and the persisted path becomes invalid. To keep the path valid,
37
+ // we skip the fallback check here and always keep the FormatContainer when one is needed.
38
+ skipFormatContainerFallbackCheck: true,
30
39
  });
31
40
  }
32
41
  function getShadowChildProcessor(bundle) {
@@ -1 +1 @@
1
- {"version":3,"file":"formatInsertPointWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatInsertPointWithContentModel.ts"],"names":[],"mappings":";AAAA,OAAO,EACH,UAAU,EACV,cAAc,EACd,oBAAoB,EACpB,0BAA0B,EAC1B,WAAW,EACX,gBAAgB,GACnB,MAAM,6BAA6B,CAAC;AAarC;;;;;;GAMG;AACH,MAAM,UAAU,iCAAiC,CAC7C,MAAe,EACf,WAA2B,EAC3B,QAIS,EACT,OAAmC;IAEnC,IAAM,MAAM,GAAsB;QAC9B,KAAK,EAAE,WAAW;KACrB,CAAC;IAEF,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,QAAQ,CAAC,KAAK,EAAE,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,\n processChildNode,\n} from 'roosterjs-content-model-dom';\nimport type {\n ElementProcessor,\n DOMInsertPoint,\n FormatContentModelOptions,\n IEditor,\n InsertPoint,\n DomToModelContext,\n ContentModelBlockGroup,\n FormatContentModelContext,\n ShallowMutableContentModelDocument,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the content in a specific position using Content Model\n * @param editor The editor object\n * @param insertPoint The insert position.\n * @param callback The callback to insert the format.\n * @param options More options, @see FormatContentModelOptions\n */\nexport function formatInsertPointWithContentModel(\n editor: IEditor,\n insertPoint: DOMInsertPoint,\n callback: (\n model: ShallowMutableContentModelDocument,\n context: FormatContentModelContext,\n insertPoint?: InsertPoint\n ) => void,\n options?: FormatContentModelOptions\n) {\n const bundle: InsertPointBundle = {\n input: insertPoint,\n };\n\n editor.formatContentModel(\n (model, context) => {\n callback(model, context, bundle.result);\n\n if (bundle?.result) {\n const { paragraph, marker } = bundle.result;\n const index = paragraph.segments.indexOf(marker);\n\n if (index >= 0) {\n mutateBlock(paragraph).segments.splice(index, 1);\n }\n }\n return true;\n },\n options,\n {\n processorOverride: {\n child: getShadowChildProcessor(bundle),\n textWithSelection: getShadowTextProcessor(bundle),\n },\n tryGetFromCache: false,\n }\n );\n}\n\ninterface InsertPointBundle {\n input: DOMInsertPoint;\n result?: InsertPoint;\n}\n\ninterface DomToModelContextWithPath extends DomToModelContext {\n /**\n * Block group path of this insert point, from direct parent group to the root group\n */\n path?: ContentModelBlockGroup[];\n}\n\nfunction getShadowChildProcessor(bundle: InsertPointBundle): ElementProcessor<ParentNode> {\n return (group, parent, context) => {\n const contextWithPath = context as DomToModelContextWithPath;\n\n contextWithPath.path = contextWithPath.path || [];\n\n let shouldShiftPath = false;\n if (contextWithPath.path[0] != group) {\n contextWithPath.path.unshift(group);\n shouldShiftPath = true;\n }\n\n const offsets = getShadowSelectionOffsets(context, bundle, parent);\n let index = 0;\n\n for (let child = parent.firstChild; child; child = child.nextSibling) {\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n processChildNode(group, child, context);\n\n index++;\n }\n\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n if (shouldShiftPath) {\n contextWithPath.path.shift();\n }\n };\n}\n\nfunction handleElementShadowSelection(\n bundle: InsertPointBundle,\n index: number,\n context: DomToModelContext,\n group: ContentModelBlockGroup,\n offsets: [number, number, number],\n container?: Node\n) {\n if (\n index == offsets[2] &&\n (index <= offsets[0] || offsets[0] < 0) &&\n (index < offsets[1] || offsets[1] < 0)\n ) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[0]) {\n context.isInSelection = true;\n addSelectionMarker(group, context, container, index);\n }\n\n if (index == offsets[2] && (index < offsets[1] || offsets[1] < 0)) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[1]) {\n addSelectionMarker(group, context, container, index);\n context.isInSelection = false;\n }\n\n if (index == offsets[2]) {\n addSelectionMarker(group, context, container, index, bundle);\n }\n}\n\nconst getShadowTextProcessor = (bundle: InsertPointBundle): ElementProcessor<Text> => (\n group,\n textNode,\n context\n) => {\n let txt = textNode.nodeValue || '';\n const offsets = getShadowSelectionOffsets(context, bundle, textNode);\n const [start, end, shadow] = offsets;\n\n const handleTextSelection = (\n subtract: number,\n originalOffset: number,\n bundle?: InsertPointBundle\n ) => {\n addTextSegment(group, txt.substring(0, subtract), context);\n addSelectionMarker(group, context, textNode, originalOffset, bundle);\n\n offsets[0] -= subtract;\n offsets[1] -= subtract;\n offsets[2] = bundle ? -1 : offsets[2] - subtract;\n\n txt = txt.substring(subtract);\n };\n\n if (\n offsets[2] >= 0 &&\n (offsets[2] <= offsets[0] || offsets[0] < 0) &&\n (offsets[2] < offsets[1] || offsets[1] < 0)\n ) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[0] >= 0) {\n handleTextSelection(offsets[0], start);\n\n context.isInSelection = true;\n }\n\n if (offsets[2] >= 0 && offsets[2] > offsets[0] && (offsets[2] < offsets[1] || offsets[1] < 0)) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[1] >= 0) {\n handleTextSelection(offsets[1], end);\n\n context.isInSelection = false;\n }\n\n if (offsets[2] >= 0 && offsets[2] >= offsets[1]) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n addTextSegment(group, txt, context);\n};\n\nfunction addSelectionMarker(\n group: ContentModelBlockGroup,\n context: DomToModelContextWithPath,\n container?: Node,\n offset?: number,\n bundle?: InsertPointBundle\n) {\n const marker = buildSelectionMarker(group, context, container, offset);\n\n marker.isSelected = !bundle;\n\n const para = addSegment(group, marker, context.blockFormat, marker.format);\n\n if (bundle && context.path) {\n bundle.result = {\n path: [...context.path],\n paragraph: para,\n marker,\n };\n }\n}\n\nfunction getShadowSelectionOffsets(\n context: DomToModelContext,\n bundle: InsertPointBundle,\n currentContainer: Node\n): [number, number, number] {\n const [start, end] = getRegularSelectionOffsets(context, currentContainer);\n const shadow = bundle.input.node == currentContainer ? bundle.input.offset : -1;\n\n return [start, end, shadow];\n}\n"]}
1
+ {"version":3,"file":"formatInsertPointWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatInsertPointWithContentModel.ts"],"names":[],"mappings":";AAAA,OAAO,EACH,UAAU,EACV,cAAc,EACd,oBAAoB,EACpB,0BAA0B,EAC1B,WAAW,EACX,gBAAgB,GACnB,MAAM,6BAA6B,CAAC;AAarC;;;;;;GAMG;AACH,MAAM,UAAU,iCAAiC,CAC7C,MAAe,EACf,WAA2B,EAC3B,QAIS,EACT,OAAmC;IAEnC,IAAM,MAAM,GAAsB;QAC9B,KAAK,EAAE,WAAW;KACrB,CAAC;IAEF,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;QAExC,IAAI,MAAM,aAAN,MAA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CAAC","sourcesContent":["import {\n addSegment,\n addTextSegment,\n buildSelectionMarker,\n getRegularSelectionOffsets,\n mutateBlock,\n processChildNode,\n} from 'roosterjs-content-model-dom';\nimport type {\n ElementProcessor,\n DOMInsertPoint,\n FormatContentModelOptions,\n IEditor,\n InsertPoint,\n DomToModelContext,\n ContentModelBlockGroup,\n FormatContentModelContext,\n ShallowMutableContentModelDocument,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the content in a specific position using Content Model\n * @param editor The editor object\n * @param insertPoint The insert position.\n * @param callback The callback to insert the format.\n * @param options More options, @see FormatContentModelOptions\n */\nexport function formatInsertPointWithContentModel(\n editor: IEditor,\n insertPoint: DOMInsertPoint,\n callback: (\n model: ShallowMutableContentModelDocument,\n context: FormatContentModelContext,\n insertPoint?: InsertPoint\n ) => void,\n options?: FormatContentModelOptions\n) {\n const bundle: InsertPointBundle = {\n input: insertPoint,\n };\n\n editor.formatContentModel(\n (model, context) => {\n callback(model, context, bundle.result);\n\n if (bundle?.result) {\n const { paragraph, marker } = bundle.result;\n const index = paragraph.segments.indexOf(marker);\n\n if (index >= 0) {\n mutateBlock(paragraph).segments.splice(index, 1);\n }\n }\n return true;\n },\n options,\n {\n processorOverride: {\n child: getShadowChildProcessor(bundle),\n textWithSelection: getShadowTextProcessor(bundle),\n },\n tryGetFromCache: false,\n // When an element carries \"container level\" styles such as margin or padding, we first\n // wrap it in a FormatContainer. After all its child nodes are processed, we decide whether\n // to keep the FormatContainer or fall back to a plain paragraph when it only wraps a single\n // paragraph. However, formatInsertPointWithContentModel persists the Content Model group path\n // during processing so the later formatting callback can still use it (see the\n // DomToModelContextWithPath interface below). If the FormatContainer falls back to a paragraph,\n // it is removed from the model and the persisted path becomes invalid. To keep the path valid,\n // we skip the fallback check here and always keep the FormatContainer when one is needed.\n skipFormatContainerFallbackCheck: true,\n }\n );\n}\n\ninterface InsertPointBundle {\n input: DOMInsertPoint;\n result?: InsertPoint;\n}\n\ninterface DomToModelContextWithPath extends DomToModelContext {\n /**\n * Block group path of this insert point, from direct parent group to the root group\n */\n path?: ContentModelBlockGroup[];\n}\n\nfunction getShadowChildProcessor(bundle: InsertPointBundle): ElementProcessor<ParentNode> {\n return (group, parent, context) => {\n const contextWithPath = context as DomToModelContextWithPath;\n\n contextWithPath.path = contextWithPath.path || [];\n\n let shouldShiftPath = false;\n if (contextWithPath.path[0] != group) {\n contextWithPath.path.unshift(group);\n shouldShiftPath = true;\n }\n\n const offsets = getShadowSelectionOffsets(context, bundle, parent);\n let index = 0;\n\n for (let child = parent.firstChild; child; child = child.nextSibling) {\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n processChildNode(group, child, context);\n\n index++;\n }\n\n handleElementShadowSelection(bundle, index, context, group, offsets, parent);\n\n if (shouldShiftPath) {\n contextWithPath.path.shift();\n }\n };\n}\n\nfunction handleElementShadowSelection(\n bundle: InsertPointBundle,\n index: number,\n context: DomToModelContext,\n group: ContentModelBlockGroup,\n offsets: [number, number, number],\n container?: Node\n) {\n if (\n index == offsets[2] &&\n (index <= offsets[0] || offsets[0] < 0) &&\n (index < offsets[1] || offsets[1] < 0)\n ) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[0]) {\n context.isInSelection = true;\n addSelectionMarker(group, context, container, index);\n }\n\n if (index == offsets[2] && (index < offsets[1] || offsets[1] < 0)) {\n addSelectionMarker(group, context, container, index, bundle);\n offsets[2] = -1;\n }\n\n if (index == offsets[1]) {\n addSelectionMarker(group, context, container, index);\n context.isInSelection = false;\n }\n\n if (index == offsets[2]) {\n addSelectionMarker(group, context, container, index, bundle);\n }\n}\n\nconst getShadowTextProcessor = (bundle: InsertPointBundle): ElementProcessor<Text> => (\n group,\n textNode,\n context\n) => {\n let txt = textNode.nodeValue || '';\n const offsets = getShadowSelectionOffsets(context, bundle, textNode);\n const [start, end, shadow] = offsets;\n\n const handleTextSelection = (\n subtract: number,\n originalOffset: number,\n bundle?: InsertPointBundle\n ) => {\n addTextSegment(group, txt.substring(0, subtract), context);\n addSelectionMarker(group, context, textNode, originalOffset, bundle);\n\n offsets[0] -= subtract;\n offsets[1] -= subtract;\n offsets[2] = bundle ? -1 : offsets[2] - subtract;\n\n txt = txt.substring(subtract);\n };\n\n if (\n offsets[2] >= 0 &&\n (offsets[2] <= offsets[0] || offsets[0] < 0) &&\n (offsets[2] < offsets[1] || offsets[1] < 0)\n ) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[0] >= 0) {\n handleTextSelection(offsets[0], start);\n\n context.isInSelection = true;\n }\n\n if (offsets[2] >= 0 && offsets[2] > offsets[0] && (offsets[2] < offsets[1] || offsets[1] < 0)) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n if (offsets[1] >= 0) {\n handleTextSelection(offsets[1], end);\n\n context.isInSelection = false;\n }\n\n if (offsets[2] >= 0 && offsets[2] >= offsets[1]) {\n handleTextSelection(offsets[2], shadow, bundle);\n }\n\n addTextSegment(group, txt, context);\n};\n\nfunction addSelectionMarker(\n group: ContentModelBlockGroup,\n context: DomToModelContextWithPath,\n container?: Node,\n offset?: number,\n bundle?: InsertPointBundle\n) {\n const marker = buildSelectionMarker(group, context, container, offset);\n\n marker.isSelected = !bundle;\n\n const para = addSegment(group, marker, context.blockFormat, marker.format);\n\n if (bundle && context.path) {\n bundle.result = {\n path: [...context.path],\n paragraph: para,\n marker,\n };\n }\n}\n\nfunction getShadowSelectionOffsets(\n context: DomToModelContext,\n bundle: InsertPointBundle,\n currentContainer: Node\n): [number, number, number] {\n const [start, end] = getRegularSelectionOffsets(context, currentContainer);\n const shadow = bundle.input.node == currentContainer ? bundle.input.offset : -1;\n\n return [start, end, shadow];\n}\n"]}
package/package.json CHANGED
@@ -3,10 +3,10 @@
3
3
  "description": "Format API for roosterjs",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.1",
6
- "roosterjs-content-model-dom": "^9.53.0",
7
- "roosterjs-content-model-types": "^9.53.0"
6
+ "roosterjs-content-model-dom": "^9.55.0",
7
+ "roosterjs-content-model-types": "^9.55.0"
8
8
  },
9
- "version": "9.53.0",
9
+ "version": "9.55.0",
10
10
  "main": "./lib/index.js",
11
11
  "typings": "./lib/index.d.ts",
12
12
  "module": "./lib-mjs/index.js",