roosterjs-content-model-api 0.23.0 → 0.25.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.
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/modelApi/block/setModelDirection.js +0 -1
- package/lib/modelApi/block/setModelDirection.js.map +1 -1
- package/lib/modelApi/block/toggleModelBlockQuote.d.ts +1 -1
- package/lib/modelApi/block/toggleModelBlockQuote.js +6 -4
- package/lib/modelApi/block/toggleModelBlockQuote.js.map +1 -1
- package/lib/modelApi/common/retrieveModelFormatState.js +0 -2
- package/lib/modelApi/common/retrieveModelFormatState.js.map +1 -1
- package/lib/modelApi/common/wrapBlock.d.ts +2 -2
- package/lib/modelApi/common/wrapBlock.js +8 -4
- package/lib/modelApi/common/wrapBlock.js.map +1 -1
- package/lib/modelApi/link/matchLink.d.ts +29 -0
- package/lib/modelApi/link/matchLink.js +95 -0
- package/lib/modelApi/link/matchLink.js.map +1 -0
- package/lib/modelApi/list/setListType.d.ts +3 -1
- package/lib/modelApi/list/setListType.js +6 -9
- package/lib/modelApi/list/setListType.js.map +1 -1
- package/lib/publicApi/block/toggleBlockQuote.d.ts +1 -1
- package/lib/publicApi/block/toggleBlockQuote.js +10 -6
- package/lib/publicApi/block/toggleBlockQuote.js.map +1 -1
- package/lib/publicApi/entity/insertEntity.js +6 -10
- package/lib/publicApi/entity/insertEntity.js.map +1 -1
- package/lib/publicApi/format/getFormatState.d.ts +1 -21
- package/lib/publicApi/format/getFormatState.js +1 -69
- package/lib/publicApi/format/getFormatState.js.map +1 -1
- package/lib/publicApi/image/changeImage.js +1 -1
- package/lib/publicApi/image/changeImage.js.map +1 -1
- package/lib/publicApi/link/insertLink.js +3 -12
- package/lib/publicApi/link/insertLink.js.map +1 -1
- package/lib/publicApi/segment/changeCapitalization.js +0 -1
- package/lib/publicApi/segment/changeCapitalization.js.map +1 -1
- package/lib/publicApi/utils/formatSegmentWithContentModel.js +2 -7
- package/lib/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
- package/lib-amd/index.d.ts +1 -0
- package/lib-amd/index.js +3 -2
- package/lib-amd/index.js.map +1 -1
- package/lib-amd/modelApi/block/setModelDirection.js +0 -1
- package/lib-amd/modelApi/block/setModelDirection.js.map +1 -1
- package/lib-amd/modelApi/block/toggleModelBlockQuote.d.ts +1 -1
- package/lib-amd/modelApi/block/toggleModelBlockQuote.js +6 -4
- package/lib-amd/modelApi/block/toggleModelBlockQuote.js.map +1 -1
- package/lib-amd/modelApi/common/retrieveModelFormatState.js +0 -2
- package/lib-amd/modelApi/common/retrieveModelFormatState.js.map +1 -1
- package/lib-amd/modelApi/common/wrapBlock.d.ts +2 -2
- package/lib-amd/modelApi/common/wrapBlock.js +8 -4
- package/lib-amd/modelApi/common/wrapBlock.js.map +1 -1
- package/lib-amd/modelApi/link/matchLink.d.ts +29 -0
- package/lib-amd/modelApi/link/matchLink.js +95 -0
- package/lib-amd/modelApi/link/matchLink.js.map +1 -0
- package/lib-amd/modelApi/list/setListType.d.ts +3 -1
- package/lib-amd/modelApi/list/setListType.js +6 -9
- package/lib-amd/modelApi/list/setListType.js.map +1 -1
- package/lib-amd/publicApi/block/toggleBlockQuote.d.ts +1 -1
- package/lib-amd/publicApi/block/toggleBlockQuote.js +10 -6
- package/lib-amd/publicApi/block/toggleBlockQuote.js.map +1 -1
- package/lib-amd/publicApi/entity/insertEntity.js +6 -10
- package/lib-amd/publicApi/entity/insertEntity.js.map +1 -1
- package/lib-amd/publicApi/format/getFormatState.d.ts +1 -21
- package/lib-amd/publicApi/format/getFormatState.js +2 -67
- package/lib-amd/publicApi/format/getFormatState.js.map +1 -1
- package/lib-amd/publicApi/image/changeImage.js +1 -1
- package/lib-amd/publicApi/image/changeImage.js.map +1 -1
- package/lib-amd/publicApi/link/insertLink.js +3 -12
- package/lib-amd/publicApi/link/insertLink.js.map +1 -1
- package/lib-amd/publicApi/segment/changeCapitalization.js +0 -1
- package/lib-amd/publicApi/segment/changeCapitalization.js.map +1 -1
- package/lib-amd/publicApi/utils/formatSegmentWithContentModel.js +2 -7
- package/lib-amd/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
- package/lib-mjs/index.d.ts +1 -0
- package/lib-mjs/index.js +1 -0
- package/lib-mjs/index.js.map +1 -1
- package/lib-mjs/modelApi/block/setModelDirection.js +0 -1
- package/lib-mjs/modelApi/block/setModelDirection.js.map +1 -1
- package/lib-mjs/modelApi/block/toggleModelBlockQuote.d.ts +1 -1
- package/lib-mjs/modelApi/block/toggleModelBlockQuote.js +6 -4
- package/lib-mjs/modelApi/block/toggleModelBlockQuote.js.map +1 -1
- package/lib-mjs/modelApi/common/retrieveModelFormatState.js +0 -2
- package/lib-mjs/modelApi/common/retrieveModelFormatState.js.map +1 -1
- package/lib-mjs/modelApi/common/wrapBlock.d.ts +2 -2
- package/lib-mjs/modelApi/common/wrapBlock.js +8 -4
- package/lib-mjs/modelApi/common/wrapBlock.js.map +1 -1
- package/lib-mjs/modelApi/link/matchLink.d.ts +29 -0
- package/lib-mjs/modelApi/link/matchLink.js +91 -0
- package/lib-mjs/modelApi/link/matchLink.js.map +1 -0
- package/lib-mjs/modelApi/list/setListType.d.ts +3 -1
- package/lib-mjs/modelApi/list/setListType.js +6 -9
- package/lib-mjs/modelApi/list/setListType.js.map +1 -1
- package/lib-mjs/publicApi/block/toggleBlockQuote.d.ts +1 -1
- package/lib-mjs/publicApi/block/toggleBlockQuote.js +10 -6
- package/lib-mjs/publicApi/block/toggleBlockQuote.js.map +1 -1
- package/lib-mjs/publicApi/entity/insertEntity.js +6 -10
- package/lib-mjs/publicApi/entity/insertEntity.js.map +1 -1
- package/lib-mjs/publicApi/format/getFormatState.d.ts +1 -21
- package/lib-mjs/publicApi/format/getFormatState.js +1 -67
- package/lib-mjs/publicApi/format/getFormatState.js.map +1 -1
- package/lib-mjs/publicApi/image/changeImage.js +1 -1
- package/lib-mjs/publicApi/image/changeImage.js.map +1 -1
- package/lib-mjs/publicApi/link/insertLink.js +2 -11
- package/lib-mjs/publicApi/link/insertLink.js.map +1 -1
- package/lib-mjs/publicApi/segment/changeCapitalization.js +0 -1
- package/lib-mjs/publicApi/segment/changeCapitalization.js.map +1 -1
- package/lib-mjs/publicApi/utils/formatSegmentWithContentModel.js +2 -7
- package/lib-mjs/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
- package/package.json +4 -6
|
@@ -1,84 +1,18 @@
|
|
|
1
|
-
import { __read } from "tslib";
|
|
2
|
-
import { getSelectionRootNode } from 'roosterjs-content-model-core';
|
|
3
1
|
import { retrieveModelFormatState } from '../../modelApi/common/retrieveModelFormatState';
|
|
4
|
-
import { getRegularSelectionOffsets, handleRegularSelection, isNodeOfType, processChildNode, } from 'roosterjs-content-model-dom';
|
|
5
2
|
/**
|
|
6
3
|
* Get current format state
|
|
7
4
|
* @param editor The editor to get format from
|
|
8
5
|
*/
|
|
9
6
|
export default function getFormatState(editor) {
|
|
10
7
|
var pendingFormat = editor.getPendingFormat();
|
|
11
|
-
var model = editor.
|
|
12
|
-
processorOverride: {
|
|
13
|
-
child: reducedModelChildProcessor,
|
|
14
|
-
},
|
|
15
|
-
});
|
|
8
|
+
var model = editor.getContentModelCopy('reduced');
|
|
16
9
|
var manager = editor.getSnapshotsManager();
|
|
17
10
|
var result = {
|
|
18
11
|
canUndo: manager.hasNewContent || manager.canMove(-1),
|
|
19
12
|
canRedo: manager.canMove(1),
|
|
20
13
|
isDarkMode: editor.isDarkMode(),
|
|
21
|
-
zoomScale: editor.getZoomScale(),
|
|
22
14
|
};
|
|
23
15
|
retrieveModelFormatState(model, pendingFormat, result);
|
|
24
16
|
return result;
|
|
25
17
|
}
|
|
26
|
-
/**
|
|
27
|
-
* @internal
|
|
28
|
-
* Export for test only
|
|
29
|
-
* In order to get format, we can still use the regular child processor. However, to improve performance, we don't need to create
|
|
30
|
-
* content model for the whole doc, instead we only need to traverse the tree path that can arrive current selected node.
|
|
31
|
-
* This "reduced" child processor will first create a node stack that stores DOM node from root to current common ancestor node of selection,
|
|
32
|
-
* then use this stack as a faked DOM tree to create a reduced content model which we can use to retrieve format state
|
|
33
|
-
*/
|
|
34
|
-
export function reducedModelChildProcessor(group, parent, context) {
|
|
35
|
-
if (!context.nodeStack) {
|
|
36
|
-
var selectionRootNode = getSelectionRootNode(context.selection);
|
|
37
|
-
context.nodeStack = selectionRootNode ? createNodeStack(parent, selectionRootNode) : [];
|
|
38
|
-
}
|
|
39
|
-
var stackChild = context.nodeStack.pop();
|
|
40
|
-
if (stackChild) {
|
|
41
|
-
var _a = __read(getRegularSelectionOffsets(context, parent), 2), nodeStartOffset = _a[0], nodeEndOffset = _a[1];
|
|
42
|
-
// If selection is not on this node, skip getting node index to save some time since we don't need it here
|
|
43
|
-
var index = nodeStartOffset >= 0 || nodeEndOffset >= 0 ? getChildIndex(parent, stackChild) : -1;
|
|
44
|
-
if (index >= 0) {
|
|
45
|
-
handleRegularSelection(index, context, group, nodeStartOffset, nodeEndOffset);
|
|
46
|
-
}
|
|
47
|
-
processChildNode(group, stackChild, context);
|
|
48
|
-
if (index >= 0) {
|
|
49
|
-
handleRegularSelection(index + 1, context, group, nodeStartOffset, nodeEndOffset);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
// No child node from node stack, that means we have reached the deepest node of selection.
|
|
54
|
-
// Now we can use default child processor to perform full sub tree scanning for content model,
|
|
55
|
-
// So that all selected node will be included.
|
|
56
|
-
context.defaultElementProcessors.child(group, parent, context);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
function createNodeStack(root, startNode) {
|
|
60
|
-
var result = [];
|
|
61
|
-
var node = startNode;
|
|
62
|
-
while (node && root != node && root.contains(node)) {
|
|
63
|
-
if (isNodeOfType(node, 'ELEMENT_NODE') && node.tagName == 'TABLE') {
|
|
64
|
-
// For table, we can't do a reduced model creation since we need to handle their cells and indexes,
|
|
65
|
-
// so clean up whatever we already have, and just put table into the stack
|
|
66
|
-
result.splice(0, result.length, node);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
result.push(node);
|
|
70
|
-
}
|
|
71
|
-
node = node.parentNode;
|
|
72
|
-
}
|
|
73
|
-
return result;
|
|
74
|
-
}
|
|
75
|
-
function getChildIndex(parent, stackChild) {
|
|
76
|
-
var index = 0;
|
|
77
|
-
var child = parent.firstChild;
|
|
78
|
-
while (child && child != stackChild) {
|
|
79
|
-
index++;
|
|
80
|
-
child = child.nextSibling;
|
|
81
|
-
}
|
|
82
|
-
return index;
|
|
83
|
-
}
|
|
84
18
|
//# sourceMappingURL=getFormatState.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getFormatState.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/format/getFormatState.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"getFormatState.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/format/getFormatState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gDAAgD,CAAC;AAG1F;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,MAAyB;IAC5D,IAAM,aAAa,GAAG,MAAM,CAAC,gBAAgB,EAAE,CAAC;IAChD,IAAM,KAAK,GAAG,MAAM,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC;IACpD,IAAM,OAAO,GAAG,MAAM,CAAC,mBAAmB,EAAE,CAAC;IAC7C,IAAM,MAAM,GAA4B;QACpC,OAAO,EAAE,OAAO,CAAC,aAAa,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACrD,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;QAC3B,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE;KAClC,CAAC;IAEF,wBAAwB,CAAC,KAAK,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;IAEvD,OAAO,MAAM,CAAC;AAClB,CAAC","sourcesContent":["import { retrieveModelFormatState } from '../../modelApi/common/retrieveModelFormatState';\nimport type { IStandaloneEditor, ContentModelFormatState } from 'roosterjs-content-model-types';\n\n/**\n * Get current format state\n * @param editor The editor to get format from\n */\nexport default function getFormatState(editor: IStandaloneEditor): ContentModelFormatState {\n const pendingFormat = editor.getPendingFormat();\n const model = editor.getContentModelCopy('reduced');\n const manager = editor.getSnapshotsManager();\n const result: ContentModelFormatState = {\n canUndo: manager.hasNewContent || manager.canMove(-1),\n canRedo: manager.canMove(1),\n isDarkMode: editor.isDarkMode(),\n };\n\n retrieveModelFormatState(model, pendingFormat, result);\n\n return result;\n}\n"]}
|
|
@@ -19,7 +19,7 @@ export default function changeImage(editor, file) {
|
|
|
19
19
|
image.format.width = '';
|
|
20
20
|
image.format.height = '';
|
|
21
21
|
image.alt = '';
|
|
22
|
-
editor.
|
|
22
|
+
editor.triggerEvent('editImage', {
|
|
23
23
|
image: selection.image,
|
|
24
24
|
previousSrc: previousSrc,
|
|
25
25
|
newSrc: dataUrl,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"changeImage.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/image/changeImage.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,MAAM,sCAAsC,CAAC;
|
|
1
|
+
{"version":3,"file":"changeImage.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/image/changeImage.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,MAAM,sCAAsC,CAAC;AAC/E,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAG7E;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,MAAyB,EAAE,IAAU;IACrE,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,IAAM,SAAS,GAAG,MAAM,CAAC,eAAe,EAAE,CAAC;IAC3C,QAAQ,CAAC,IAAI,EAAE,UAAA,OAAO;QAClB,IAAI,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,IAAI,CAAA,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,IAAI,MAAK,OAAO,EAAE;YAChE,2BAA2B,CAAC,MAAM,EAAE,aAAa,EAAE,UAAC,KAAwB;;gBACxE,IAAM,WAAW,GAAG,MAAA,MAAA,mBAAmB,CAAC,KAAK,CAAC,0CAAE,GAAG,mCAAI,EAAE,CAAC;gBAC1D,IAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC;gBAE9B,KAAK,CAAC,GAAG,GAAG,OAAO,CAAC;gBACpB,KAAK,CAAC,OAAO,GAAG,EAAE,CAAC;gBACnB,KAAK,CAAC,MAAM,CAAC,KAAK,GAAG,EAAE,CAAC;gBACxB,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,EAAE,CAAC;gBACzB,KAAK,CAAC,GAAG,GAAG,EAAE,CAAC;gBAEf,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE;oBAC7B,KAAK,EAAE,SAAS,CAAC,KAAK;oBACtB,WAAW,aAAA;oBACX,MAAM,EAAE,OAAO;oBACf,WAAW,aAAA;iBACd,CAAC,CAAC;YACP,CAAC,CAAC,CAAC;SACN;IACL,CAAC,CAAC,CAAC;AACP,CAAC","sourcesContent":["import formatImageWithContentModel from '../utils/formatImageWithContentModel';\nimport { readFile, updateImageMetadata } from 'roosterjs-content-model-core';\nimport type { ContentModelImage, IStandaloneEditor } from 'roosterjs-content-model-types';\n\n/**\n * Change the selected image src\n * @param editor The editor instance\n * @param file The image file\n */\nexport default function changeImage(editor: IStandaloneEditor, file: File) {\n editor.focus();\n\n const selection = editor.getDOMSelection();\n readFile(file, dataUrl => {\n if (dataUrl && !editor.isDisposed() && selection?.type === 'image') {\n formatImageWithContentModel(editor, 'changeImage', (image: ContentModelImage) => {\n const originalSrc = updateImageMetadata(image)?.src ?? '';\n const previousSrc = image.src;\n\n image.src = dataUrl;\n image.dataset = {};\n image.format.width = '';\n image.format.height = '';\n image.alt = '';\n\n editor.triggerEvent('editImage', {\n image: selection.image,\n previousSrc,\n newSrc: dataUrl,\n originalSrc,\n });\n });\n }\n });\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __assign } from "tslib";
|
|
2
2
|
import { adjustTrailingSpaceSelection } from '../../modelApi/selection/adjustTrailingSpaceSelection';
|
|
3
3
|
import { ChangeSource, getSelectedSegments, mergeModel } from 'roosterjs-content-model-core';
|
|
4
|
-
import {
|
|
4
|
+
import { matchLink } from '../../modelApi/link/matchLink';
|
|
5
5
|
import { addLink, addSegment, createContentModelDocument, createText, } from 'roosterjs-content-model-dom';
|
|
6
6
|
// Regex matching Uri scheme
|
|
7
7
|
var URI_REGEX = /^[a-zA-Z]+:/i;
|
|
@@ -87,7 +87,6 @@ var createLink = function (url, anchorTitle, target, underline) {
|
|
|
87
87
|
},
|
|
88
88
|
};
|
|
89
89
|
};
|
|
90
|
-
// TODO: This is copied from original code. We may need to integrate this logic into matchLink() later.
|
|
91
90
|
function applyLinkPrefix(url) {
|
|
92
91
|
if (!url) {
|
|
93
92
|
return url;
|
|
@@ -112,15 +111,7 @@ function applyLinkPrefix(url) {
|
|
|
112
111
|
}
|
|
113
112
|
return prefix + url;
|
|
114
113
|
}
|
|
115
|
-
// TODO: This is copied from original code. However, ContentModel should be able to filter out malicious
|
|
116
|
-
// attributes later, so no need to use HtmlSanitizer here
|
|
117
114
|
function checkXss(link) {
|
|
118
|
-
|
|
119
|
-
var a = document.createElement('a');
|
|
120
|
-
a.href = link || '';
|
|
121
|
-
sanitizer.sanitize(a);
|
|
122
|
-
// We use getAttribute because some browsers will try to make the href property a valid link.
|
|
123
|
-
// This has unintended side effects when the link lacks a protocol.
|
|
124
|
-
return a.getAttribute('href') || '';
|
|
115
|
+
return link.match(/s\n*c\n*r\n*i\n*p\n*t\n*:/i) ? '' : link;
|
|
125
116
|
}
|
|
126
117
|
//# sourceMappingURL=insertLink.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insertLink.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/link/insertLink.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,4BAA4B,EAAE,MAAM,uDAAuD,CAAC;AACrG,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC7F,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEhE,OAAO,EACH,OAAO,EACP,UAAU,EACV,0BAA0B,EAC1B,UAAU,GACb,MAAM,6BAA6B,CAAC;AAErC,4BAA4B;AAC5B,IAAM,SAAS,GAAG,cAAc,CAAC;AACjC,wCAAwC;AACxC,IAAM,YAAY,GAAG,cAAc,CAAC;AACpC,sDAAsD;AACtD,IAAM,SAAS,GAAG,SAAS,CAAC;AAE5B;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAC9B,MAAyB,EACzB,IAAY,EACZ,WAAoB,EACpB,WAAoB,EACpB,MAAe;IAEf,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,IAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAC1C,IAAI,GAAG,EAAE;QACL,IAAM,UAAQ,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;QAChC,IAAM,SAAO,GAAG,UAAQ,CAAC,CAAC,CAAC,UAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;QACzE,IAAM,OAAK,GAAuB,EAAE,CAAC;QACrC,IAAI,YAA4B,CAAC;QAEjC,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;;YACX,IAAM,QAAQ,GAAG,mBAAmB,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d;IAED,4BAA4B;IAC5B,oEAAoE;IACpE,iEAAiE;IACjE,qDAAqD;IACrD,+BAA+B;IAC/B,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE;QAC3B,IAAI,GAAG,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE;YAC/B,MAAM,GAAG,SAAS,CAAC;SACtB;aAAM,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;YACnC,MAAM,GAAG,QAAQ,CAAC;SACrB;aAAM;YACH,sBAAsB;YACtB,MAAM,GAAG,SAAS,CAAC;SACtB;KACJ;IAED,OAAO,MAAM,GAAG,GAAG,CAAC;AACxB,CAAC;AAED,wGAAwG;AACxG,yDAAyD;AACzD,SAAS,QAAQ,CAAC,IAAY;IAC1B,IAAM,SAAS,GAAG,IAAI,aAAa,EAAE,CAAC;IACtC,IAAM,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IAEtC,CAAC,CAAC,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;IAEpB,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACtB,6FAA6F;IAC7F,mEAAmE;IACnE,OAAO,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;AACxC,CAAC","sourcesContent":["import { adjustTrailingSpaceSelection } from '../../modelApi/selection/adjustTrailingSpaceSelection';\nimport { ChangeSource, getSelectedSegments, mergeModel } from 'roosterjs-content-model-core';\nimport { HtmlSanitizer, matchLink } from 'roosterjs-editor-dom';\nimport type { ContentModelLink, IStandaloneEditor } from 'roosterjs-content-model-types';\nimport {\n addLink,\n addSegment,\n createContentModelDocument,\n createText,\n} from 'roosterjs-content-model-dom';\n\n// Regex matching Uri scheme\nconst URI_REGEX = /^[a-zA-Z]+:/i;\n// Regex matching begin of email address\nconst MAILTO_REGEX = /^[\\w.%+-]+@/i;\n// Regex matching begin of ftp, i.e. ftp.microsoft.com\nconst FTP_REGEX = /^ftp\\./i;\n\n/**\n * Insert a hyperlink at cursor.\n * When there is a selection, hyperlink will be applied to the selection,\n * otherwise a hyperlink will be inserted to the cursor position.\n * @param editor Editor object\n * @param link Link address, can be http(s), mailto, notes, file, unc, ftp, news, telnet, gopher, wais.\n * When protocol is not specified, a best matched protocol will be predicted.\n * @param anchorTitle Optional alt text of the link, will be shown when hover on the link\n * @param displayText Optional display text for the link.\n * @param target Optional display target for the link (\"_blank\"|\"_self\"|\"_parent\"|\"_top\"|\"{framename}\")\n * If specified, the display text of link will be replaced with this text.\n * If not specified and there wasn't a link, the link url will be used as display text.\n */\nexport default function insertLink(\n editor: IStandaloneEditor,\n link: string,\n anchorTitle?: string,\n displayText?: string,\n target?: string\n) {\n editor.focus();\n\n const url = (checkXss(link) || '').trim();\n if (url) {\n const linkData = matchLink(url);\n const linkUrl = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);\n const links: ContentModelLink[] = [];\n let anchorNode: Node | undefined;\n\n editor.formatContentModel(\n (model, context) => {\n const segments = getSelectedSegments(model, false /*includingFormatHolder*/);\n const originalText = segments\n .map(x => (x.segmentType == 'Text' ? x.text : ''))\n .join('');\n const text = displayText || originalText || '';\n\n if (\n segments.some(x => x.segmentType != 'SelectionMarker') &&\n originalText == text\n ) {\n segments.forEach(x => {\n const link = createLink(\n linkUrl,\n anchorTitle,\n target,\n x.segmentType == 'Text'\n );\n addLink(x, link);\n if (x.link) {\n links.push(x.link);\n }\n });\n } else if (\n segments.every(x => x.segmentType == 'SelectionMarker') ||\n (!!text && text != originalText)\n ) {\n const segment = createText(text || (linkData ? linkData.originalUrl : url), {\n ...segments[0]?.format,\n ...editor.getPendingFormat(),\n });\n const doc = createContentModelDocument();\n const link = createLink(linkUrl, anchorTitle, target);\n\n addLink(segment, link);\n addSegment(doc, segment);\n\n if (segment.link) {\n links.push(segment.link);\n }\n\n mergeModel(model, doc, context, {\n mergeFormat: 'mergeAll',\n });\n }\n\n adjustTrailingSpaceSelection(model);\n return segments.length > 0;\n },\n {\n changeSource: ChangeSource.CreateLink,\n onNodeCreated: (modelElement, node) => {\n if (!anchorNode && links.indexOf(modelElement as ContentModelLink) >= 0) {\n anchorNode = node;\n }\n },\n getChangeData: () => anchorNode,\n apiName: 'insertLink',\n }\n );\n }\n}\n\nconst createLink = (\n url: string,\n anchorTitle?: string,\n target?: string,\n underline: boolean = true\n): ContentModelLink => {\n return {\n dataset: {},\n format: {\n href: url,\n anchorTitle,\n target,\n underline: underline,\n },\n };\n};\n\n// TODO: This is copied from original code. We may need to integrate this logic into matchLink() later.\nfunction applyLinkPrefix(url: string): string {\n if (!url) {\n return url;\n }\n\n // Add link prefix per rule:\n // (a) if the url always starts with a URI scheme, leave it as it is\n // (b) if the url is an email address, xxx@... add mailto: prefix\n // (c) if the url starts with ftp., add ftp:// prefix\n // (d) rest, add http:// prefix\n let prefix = '';\n if (url.search(URI_REGEX) < 0) {\n if (url.search(MAILTO_REGEX) == 0) {\n prefix = 'mailto:';\n } else if (url.search(FTP_REGEX) == 0) {\n prefix = 'ftp://';\n } else {\n // fallback to http://\n prefix = 'http://';\n }\n }\n\n return prefix + url;\n}\n\n// TODO: This is copied from original code. However, ContentModel should be able to filter out malicious\n// attributes later, so no need to use HtmlSanitizer here\nfunction checkXss(link: string): string {\n const sanitizer = new HtmlSanitizer();\n const a = document.createElement('a');\n\n a.href = link || '';\n\n sanitizer.sanitize(a);\n // We use getAttribute because some browsers will try to make the href property a valid link.\n // This has unintended side effects when the link lacks a protocol.\n return a.getAttribute('href') || '';\n}\n"]}
|
|
1
|
+
{"version":3,"file":"insertLink.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/link/insertLink.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,4BAA4B,EAAE,MAAM,uDAAuD,CAAC;AACrG,OAAO,EAAE,YAAY,EAAE,mBAA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AC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;YACnC,MAAM,GAAG,QAAQ,CAAC;SACrB;aAAM;YACH,sBAAsB;YACtB,MAAM,GAAG,SAAS,CAAC;SACtB;KACJ;IAED,OAAO,MAAM,GAAG,GAAG,CAAC;AACxB,CAAC;AAED,SAAS,QAAQ,CAAC,IAAY;IAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AAChE,CAAC","sourcesContent":["import { adjustTrailingSpaceSelection } from '../../modelApi/selection/adjustTrailingSpaceSelection';\nimport { ChangeSource, getSelectedSegments, mergeModel } from 'roosterjs-content-model-core';\nimport { matchLink } from '../../modelApi/link/matchLink';\nimport type { ContentModelLink, IStandaloneEditor } from 'roosterjs-content-model-types';\nimport {\n addLink,\n addSegment,\n createContentModelDocument,\n createText,\n} from 'roosterjs-content-model-dom';\n\n// Regex matching Uri scheme\nconst URI_REGEX = /^[a-zA-Z]+:/i;\n// Regex matching begin of email address\nconst MAILTO_REGEX = /^[\\w.%+-]+@/i;\n// Regex matching begin of ftp, i.e. ftp.microsoft.com\nconst FTP_REGEX = /^ftp\\./i;\n\n/**\n * Insert a hyperlink at cursor.\n * When there is a selection, hyperlink will be applied to the selection,\n * otherwise a hyperlink will be inserted to the cursor position.\n * @param editor Editor object\n * @param link Link address, can be http(s), mailto, notes, file, unc, ftp, news, telnet, gopher, wais.\n * When protocol is not specified, a best matched protocol will be predicted.\n * @param anchorTitle Optional alt text of the link, will be shown when hover on the link\n * @param displayText Optional display text for the link.\n * @param target Optional display target for the link (\"_blank\"|\"_self\"|\"_parent\"|\"_top\"|\"{framename}\")\n * If specified, the display text of link will be replaced with this text.\n * If not specified and there wasn't a link, the link url will be used as display text.\n */\nexport default function insertLink(\n editor: IStandaloneEditor,\n link: string,\n anchorTitle?: string,\n displayText?: string,\n target?: string\n) {\n editor.focus();\n\n const url = (checkXss(link) || '').trim();\n if (url) {\n const linkData = matchLink(url);\n const linkUrl = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);\n const links: ContentModelLink[] = [];\n let anchorNode: Node | undefined;\n\n editor.formatContentModel(\n (model, context) => {\n const segments = getSelectedSegments(model, false /*includingFormatHolder*/);\n const originalText = segments\n .map(x => (x.segmentType == 'Text' ? x.text : ''))\n .join('');\n const text = displayText || originalText || '';\n\n if (\n segments.some(x => x.segmentType != 'SelectionMarker') &&\n originalText == text\n ) {\n segments.forEach(x => {\n const link = createLink(\n linkUrl,\n anchorTitle,\n target,\n x.segmentType == 'Text'\n );\n addLink(x, link);\n if (x.link) {\n links.push(x.link);\n }\n });\n } else if (\n segments.every(x => x.segmentType == 'SelectionMarker') ||\n (!!text && text != originalText)\n ) {\n const segment = createText(text || (linkData ? linkData.originalUrl : url), {\n ...segments[0]?.format,\n ...editor.getPendingFormat(),\n });\n const doc = createContentModelDocument();\n const link = createLink(linkUrl, anchorTitle, target);\n\n addLink(segment, link);\n addSegment(doc, segment);\n\n if (segment.link) {\n links.push(segment.link);\n }\n\n mergeModel(model, doc, context, {\n mergeFormat: 'mergeAll',\n });\n }\n\n adjustTrailingSpaceSelection(model);\n return segments.length > 0;\n },\n {\n changeSource: ChangeSource.CreateLink,\n onNodeCreated: (modelElement, node) => {\n if (!anchorNode && links.indexOf(modelElement as ContentModelLink) >= 0) {\n anchorNode = node;\n }\n },\n getChangeData: () => anchorNode,\n apiName: 'insertLink',\n }\n );\n }\n}\n\nconst createLink = (\n url: string,\n anchorTitle?: string,\n target?: string,\n underline: boolean = true\n): ContentModelLink => {\n return {\n dataset: {},\n format: {\n href: url,\n anchorTitle,\n target,\n underline: underline,\n },\n };\n};\n\nfunction applyLinkPrefix(url: string): string {\n if (!url) {\n return url;\n }\n\n // Add link prefix per rule:\n // (a) if the url always starts with a URI scheme, leave it as it is\n // (b) if the url is an email address, xxx@... add mailto: prefix\n // (c) if the url starts with ftp., add ftp:// prefix\n // (d) rest, add http:// prefix\n let prefix = '';\n if (url.search(URI_REGEX) < 0) {\n if (url.search(MAILTO_REGEX) == 0) {\n prefix = 'mailto:';\n } else if (url.search(FTP_REGEX) == 0) {\n prefix = 'ftp://';\n } else {\n // fallback to http://\n prefix = 'http://';\n }\n }\n\n return prefix + url;\n}\n\nfunction checkXss(link: string): string {\n return link.match(/s\\n*c\\n*r\\n*i\\n*p\\n*t\\n*:/i) ? '' : link;\n}\n"]}
|
|
@@ -28,7 +28,6 @@ export default function changeCapitalization(editor, capitalization, language) {
|
|
|
28
28
|
segment.text = wordArray.join(' ');
|
|
29
29
|
break;
|
|
30
30
|
case 'sentence':
|
|
31
|
-
// TODO: Add rules on punctuation for internationalization - TASK 104769
|
|
32
31
|
var punctuationMarks = '[\\.\\!\\?]';
|
|
33
32
|
// Find a match of a word character either:
|
|
34
33
|
// - At the beginning of a string with or without preceding whitespace, for
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"changeCapitalization.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/segment/changeCapitalization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAGvF;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CACxC,MAAyB,EACzB,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,
|
|
1
|
+
{"version":3,"file":"changeCapitalization.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/segment/changeCapitalization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAGvF;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CACxC,MAAyB,EACzB,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 { IStandaloneEditor } 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 default function changeCapitalization(\n editor: IStandaloneEditor,\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"]}
|
|
@@ -7,7 +7,6 @@ import { getSelectedSegmentsAndParagraphs } from 'roosterjs-content-model-core';
|
|
|
7
7
|
export function formatSegmentWithContentModel(editor, apiName, toggleStyleCallback, segmentHasStyleCallback, includingFormatHolder, afterFormatCallback) {
|
|
8
8
|
editor.formatContentModel(function (model, context) {
|
|
9
9
|
var segmentAndParagraphs = getSelectedSegmentsAndParagraphs(model, !!includingFormatHolder);
|
|
10
|
-
var pendingFormat = editor.getPendingFormat();
|
|
11
10
|
var isCollapsedSelection = segmentAndParagraphs.length == 1 &&
|
|
12
11
|
segmentAndParagraphs[0][0].segmentType == 'SelectionMarker';
|
|
13
12
|
if (isCollapsedSelection) {
|
|
@@ -17,9 +16,7 @@ export function formatSegmentWithContentModel(editor, apiName, toggleStyleCallba
|
|
|
17
16
|
isCollapsedSelection = false;
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
|
-
var formatsAndSegments =
|
|
21
|
-
? [[pendingFormat, null, null]]
|
|
22
|
-
: segmentAndParagraphs.map(function (item) { return [item[0].format, item[0], item[1]]; });
|
|
19
|
+
var formatsAndSegments = segmentAndParagraphs.map(function (item) { return [item[0].format, item[0], item[1]]; });
|
|
23
20
|
var isTurningOff = segmentHasStyleCallback
|
|
24
21
|
? formatsAndSegments.every(function (_a) {
|
|
25
22
|
var _b = __read(_a, 3), format = _b[0], segment = _b[1], paragraph = _b[2];
|
|
@@ -31,10 +28,8 @@ export function formatSegmentWithContentModel(editor, apiName, toggleStyleCallba
|
|
|
31
28
|
return toggleStyleCallback(format, !isTurningOff, segment, paragraph);
|
|
32
29
|
});
|
|
33
30
|
afterFormatCallback === null || afterFormatCallback === void 0 ? void 0 : afterFormatCallback(model);
|
|
34
|
-
if (!pendingFormat && isCollapsedSelection) {
|
|
35
|
-
context.newPendingFormat = segmentAndParagraphs[0][0].format;
|
|
36
|
-
}
|
|
37
31
|
if (isCollapsedSelection) {
|
|
32
|
+
context.newPendingFormat = segmentAndParagraphs[0][0].format;
|
|
38
33
|
editor.focus();
|
|
39
34
|
return false;
|
|
40
35
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAC;AACnF,OAAO,EAAE,gCAAgC,EAAE,MAAM,8BAA8B,CAAC;AAQhF;;GAEG;AACH,MAAM,UAAU,6BAA6B,CACzC,MAAyB,EACzB,OAAe,EACf,mBAKS,EACT,uBAIY,EACZ,qBAA+B,EAC/B,mBAA2D;IAE3D,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,IAAI,oBAAoB,GAAG,gCAAgC,CACvD,KAAK,EACL,CAAC,CAAC,qBAAqB,CAC1B,CAAC;QACF,
|
|
1
|
+
{"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages-content-model/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAC;AACnF,OAAO,EAAE,gCAAgC,EAAE,MAAM,8BAA8B,CAAC;AAQhF;;GAEG;AACH,MAAM,UAAU,6BAA6B,CACzC,MAAyB,EACzB,OAAe,EACf,mBAKS,EACT,uBAIY,EACZ,qBAA+B,EAC/B,mBAA2D;IAE3D,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,IAAI,oBAAoB,GAAG,gCAAgC,CACvD,KAAK,EACL,CAAC,CAAC,qBAAqB,CAC1B,CAAC;QACF,IAAI,oBAAoB,GACpB,oBAAoB,CAAC,MAAM,IAAI,CAAC;YAChC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,IAAI,iBAAiB,CAAC;QAEhE,IAAI,oBAAoB,EAAE;YACtB,IAAM,MAAI,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAExC,oBAAoB,GAAG,mBAAmB,CACtC,KAAK,EACL,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAC7B,CAAC,GAAG,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,EAAE,MAAI,CAAC,E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ent":["import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport { getSelectedSegmentsAndParagraphs } from 'roosterjs-content-model-core';\nimport type {\n ContentModelDocument,\n ContentModelParagraph,\n ContentModelSegment,\n ContentModelSegmentFormat,\n IStandaloneEditor,\n} from 'roosterjs-content-model-types';\n/**\n * @internal\n */\nexport function formatSegmentWithContentModel(\n editor: IStandaloneEditor,\n apiName: string,\n toggleStyleCallback: (\n format: ContentModelSegmentFormat,\n isTuringOn: boolean,\n segment: ContentModelSegment | null,\n paragraph: ContentModelParagraph | null\n ) => void,\n segmentHasStyleCallback?: (\n format: ContentModelSegmentFormat,\n segment: ContentModelSegment | null,\n paragraph: ContentModelParagraph | null\n ) => boolean,\n includingFormatHolder?: boolean,\n afterFormatCallback?: (model: ContentModelDocument) => void\n) {\n editor.formatContentModel(\n (model, context) => {\n let segmentAndParagraphs = getSelectedSegmentsAndParagraphs(\n model,\n !!includingFormatHolder\n );\n let isCollapsedSelection =\n segmentAndParagraphs.length == 1 &&\n segmentAndParagraphs[0][0].segmentType == 'SelectionMarker';\n\n if (isCollapsedSelection) {\n const para = segmentAndParagraphs[0][1];\n\n segmentAndParagraphs = adjustWordSelection(\n model,\n segmentAndParagraphs[0][0]\n ).map(x => [x, para]);\n\n if (segmentAndParagraphs.length > 1) {\n isCollapsedSelection = false;\n }\n }\n\n const formatsAndSegments: [\n ContentModelSegmentFormat,\n ContentModelSegment | null,\n ContentModelParagraph | null\n ][] = segmentAndParagraphs.map(item => [item[0].format, item[0], item[1]]);\n\n const isTurningOff = segmentHasStyleCallback\n ? formatsAndSegments.every(([format, segment, paragraph]) =>\n segmentHasStyleCallback(format, segment, paragraph)\n )\n : false;\n\n formatsAndSegments.forEach(([format, segment, paragraph]) =>\n toggleStyleCallback(format, !isTurningOff, segment, paragraph)\n );\n\n afterFormatCallback?.(model);\n\n if (isCollapsedSelection) {\n context.newPendingFormat = segmentAndParagraphs[0][0].format;\n editor.focus();\n return false;\n } else {\n return formatsAndSegments.length > 0;\n }\n },\n {\n apiName,\n }\n );\n}\n"]}
|
package/package.json
CHANGED
|
@@ -3,13 +3,11 @@
|
|
|
3
3
|
"description": "Content Model for roosterjs (Under development)",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"tslib": "^2.3.1",
|
|
6
|
-
"roosterjs-
|
|
7
|
-
"roosterjs-
|
|
8
|
-
"roosterjs-content-model-
|
|
9
|
-
"roosterjs-content-model-dom": "^0.23.0",
|
|
10
|
-
"roosterjs-content-model-types": "^0.23.0"
|
|
6
|
+
"roosterjs-content-model-core": "^0.25.0",
|
|
7
|
+
"roosterjs-content-model-dom": "^0.25.0",
|
|
8
|
+
"roosterjs-content-model-types": "^0.25.0"
|
|
11
9
|
},
|
|
12
|
-
"version": "0.
|
|
10
|
+
"version": "0.25.0",
|
|
13
11
|
"main": "./lib/index.js",
|
|
14
12
|
"typings": "./lib/index.d.ts",
|
|
15
13
|
"module": "./lib-mjs/index.js",
|