roosterjs-content-model-api 9.54.0 → 9.56.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"]}
@@ -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,KAAK,EAAE,CAAC;QAEf,IAAA,6DAA6B,EAAC,MAAM,EAAE,sBAAsB,EAAE,UAAC,CAAC,EAAE,EAAE,EAAE,OAAO;YACzE,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,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;4BACvC,SAAS,CAAC,CAAC,CAAC;gCACR,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,QAAQ,CAAC;oCAClD,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;yBAC7B;wBAED,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;wBACnC,MAAM;oBAEV,KAAK,UAAU;wBACX,IAAM,gBAAgB,GAAG,aAAa,CAAC;wBACvC,2CAA2C;wBAC3C,2EAA2E;wBAC3E,2EAA2E;wBAC3E,uEAAuE;wBACvE,sDAAsD;wBACtD,IAAM,KAAK,GAAG,IAAI,MAAM,CAAC,WAAW,GAAG,gBAAgB,GAAG,SAAS,EAAE,GAAG,CAAC,CAAC;wBAE1E,OAAO,CAAC,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,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C;IACP,CAAC;IArDD,oDAqDC;IAED;;;;OAIG;IACH,SAAS,qBAAqB,CAC1B,SAAqD,EACrD,OAA0C;QAE1C,IAAI,CAAC,SAAS,EAAE;YACZ,OAAO,EAAE,CAAC;SACb;QAED,IAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAElD,KAAK,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;YACjC,IAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;YAEvC,IAAI,QAAQ,CAAC,WAAW,IAAI,iBAAiB,EAAE;gBAC3C,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"]}
@@ -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,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,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E1C,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"]}
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.54.0",
7
- "roosterjs-content-model-types": "^9.54.0"
6
+ "roosterjs-content-model-dom": "^9.56.0",
7
+ "roosterjs-content-model-types": "^9.56.0"
8
8
  },
9
- "version": "9.54.0",
9
+ "version": "9.56.0",
10
10
  "main": "./lib/index.js",
11
11
  "typings": "./lib/index.d.ts",
12
12
  "module": "./lib-mjs/index.js",