@atlaskit/editor-plugin-show-diff 20.0.11 → 21.0.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/CHANGELOG.md +17 -0
- package/dist/cjs/pm-plugins/NodeViewSerializer.js +71 -7
- package/dist/cjs/pm-plugins/createEditorProxy.js +84 -0
- package/dist/cjs/pm-plugins/decorations/createContributorTagWidget.js +25 -6
- package/dist/cjs/pm-plugins/decorations/createNodeChangedDecorationWidget.js +35 -11
- package/dist/cjs/pm-plugins/decorations/utils/wrapBlockNodeView.js +38 -2
- package/dist/cjs/pm-plugins/markViews.js +65 -0
- package/dist/es2019/pm-plugins/NodeViewSerializer.js +64 -8
- package/dist/es2019/pm-plugins/createEditorProxy.js +76 -0
- package/dist/es2019/pm-plugins/decorations/createContributorTagWidget.js +26 -8
- package/dist/es2019/pm-plugins/decorations/createNodeChangedDecorationWidget.js +35 -12
- package/dist/es2019/pm-plugins/decorations/utils/wrapBlockNodeView.js +36 -2
- package/dist/es2019/pm-plugins/markViews.js +61 -0
- package/dist/esm/pm-plugins/NodeViewSerializer.js +71 -7
- package/dist/esm/pm-plugins/createEditorProxy.js +78 -0
- package/dist/esm/pm-plugins/decorations/createContributorTagWidget.js +27 -8
- package/dist/esm/pm-plugins/decorations/createNodeChangedDecorationWidget.js +37 -12
- package/dist/esm/pm-plugins/decorations/utils/wrapBlockNodeView.js +38 -2
- package/dist/esm/pm-plugins/markViews.js +60 -0
- package/dist/types/pm-plugins/NodeViewSerializer.d.ts +7 -2
- package/dist/types/pm-plugins/createEditorProxy.d.ts +18 -0
- package/dist/types/pm-plugins/decorations/createContributorTagWidget.d.ts +2 -1
- package/dist/types/pm-plugins/markViews.d.ts +14 -0
- package/package.json +10 -10
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { GapCursorSelection, Side } from '@atlaskit/editor-common/selection/gap-cursor/selection';
|
|
2
|
+
/**
|
|
3
|
+
* A preview is rendered by the live view's own node views, which read `getPos()` against
|
|
4
|
+
* `view.state.doc` for both layout (`isTableNested`) and writes (`scaleTable` builds attribute
|
|
5
|
+
* steps at `getPos() + 1`). Handing them live positions makes the layout correct but points the
|
|
6
|
+
* writes at unrelated live nodes, so give them a view whose document *is* the preview instead.
|
|
7
|
+
*
|
|
8
|
+
* With `$sourcePos` (the node's position in the doc it came from) the node is swapped into a copy
|
|
9
|
+
* of its real top-level ancestor, so a table inside an expand still resolves as nested.
|
|
10
|
+
*
|
|
11
|
+
* Returns `null` when the node cannot be a top-level child (e.g. a bare `tableCell`), in which
|
|
12
|
+
* case the caller falls back to the live view and the previous stubbed positions.
|
|
13
|
+
*/
|
|
14
|
+
export function createEditorProxy(liveView, rootNode, $sourcePos) {
|
|
15
|
+
const liveState = liveView.state;
|
|
16
|
+
const createDoc = topLevel => liveState.schema.topNodeType.createChecked(null, topLevel);
|
|
17
|
+
let previewDoc;
|
|
18
|
+
let rootPos = 0;
|
|
19
|
+
try {
|
|
20
|
+
var _$sourcePos$nodeAfter;
|
|
21
|
+
if ($sourcePos && $sourcePos.depth > 0 && ((_$sourcePos$nodeAfter = $sourcePos.nodeAfter) === null || _$sourcePos$nodeAfter === void 0 ? void 0 : _$sourcePos$nodeAfter.type) === rootNode.type) {
|
|
22
|
+
let topLevel = rootNode;
|
|
23
|
+
for (let depth = $sourcePos.depth; depth > 0; depth--) {
|
|
24
|
+
const ancestor = $sourcePos.node(depth);
|
|
25
|
+
topLevel = ancestor.copy(ancestor.content.replaceChild($sourcePos.index(depth), topLevel));
|
|
26
|
+
}
|
|
27
|
+
previewDoc = createDoc(topLevel);
|
|
28
|
+
rootPos = $sourcePos.pos - $sourcePos.before(1);
|
|
29
|
+
} else {
|
|
30
|
+
previewDoc = createDoc(rootNode);
|
|
31
|
+
}
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// A gap cursor just before the node selects nothing, so the table's node view doesn't render
|
|
37
|
+
// as selected or show its controls.
|
|
38
|
+
const previewSelection = new GapCursorSelection(previewDoc.resolve(rootPos), Side.LEFT);
|
|
39
|
+
// Proxy rather than `EditorState.create` so plugin state is shared as-is: re-initialising
|
|
40
|
+
// every plugin per preview would be costly and re-runs their side effects.
|
|
41
|
+
let stateProxy = new Proxy(liveState, {
|
|
42
|
+
get(target, prop, receiver) {
|
|
43
|
+
if (prop === 'doc') {
|
|
44
|
+
return previewDoc;
|
|
45
|
+
}
|
|
46
|
+
if (prop === 'selection') {
|
|
47
|
+
return previewSelection;
|
|
48
|
+
}
|
|
49
|
+
return Reflect.get(target, prop, receiver);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
const editorProxy = new Proxy(liveView, {
|
|
53
|
+
get(target, prop, receiver) {
|
|
54
|
+
if (prop === 'state') {
|
|
55
|
+
return stateProxy;
|
|
56
|
+
}
|
|
57
|
+
if (prop === 'dispatch') {
|
|
58
|
+
// Keep the transaction inside the preview. It was built against `previewDoc`, so
|
|
59
|
+
// applying it to the live state would corrupt the real document.
|
|
60
|
+
return tr => {
|
|
61
|
+
try {
|
|
62
|
+
stateProxy = stateProxy.apply(tr);
|
|
63
|
+
} catch {
|
|
64
|
+
// A preview is throwaway; a failed self-update is not worth surfacing.
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const value = Reflect.get(target, prop, receiver);
|
|
69
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return {
|
|
73
|
+
editorProxy,
|
|
74
|
+
rootPos
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -2,8 +2,8 @@ import { Decoration } from '@atlaskit/editor-prosemirror/view';
|
|
|
2
2
|
import { fg } from '@atlaskit/platform-feature-flags/fg';
|
|
3
3
|
import { ContributorTagController } from '../../ui/ContributorTag/contributorTagController';
|
|
4
4
|
import { buildCharsByOffset, isWhitespaceChar } from '../utils/charsByOffset';
|
|
5
|
-
import { clampAnchorPosIntoCell } from './createAnchorDecorationWidgets';
|
|
6
|
-
import { AnchorTypeKey, buildAnchorDecorationKey, buildContributorTagDecorationSpec } from './decorationKeys';
|
|
5
|
+
import { clampAnchorPosIntoCell, createAnchorNameSpan } from './createAnchorDecorationWidgets';
|
|
6
|
+
import { AnchorTypeKey, buildAnchorDecorationKey, buildAnchorDecorationSpec, buildContributorTagDecorationSpec } from './decorationKeys';
|
|
7
7
|
|
|
8
8
|
/** Marks the host element of one tag, so a tag can be found in the document by its own diff. */
|
|
9
9
|
export const CONTRIBUTOR_TAG_HOST_ATTRIBUTE = 'data-contributor-tag-host';
|
|
@@ -212,7 +212,8 @@ export const resolveHoistedCodeBlockAnchor = (doc, pos, diffId) => {
|
|
|
212
212
|
* highlights — or, with `anchorAtRangeStart`, at `from` itself. A host inside a code block is
|
|
213
213
|
* hoisted in front of it instead, anchored via `resolveHoistedCodeBlockAnchor` (EDITOR-9045).
|
|
214
214
|
*
|
|
215
|
-
* Returns one decoration
|
|
215
|
+
* Returns one decoration when a caller-supplied `anchorName` is used, two (marker + tag)
|
|
216
|
+
* otherwise — hoisted out of a code block, or anchored to its own point marker.
|
|
216
217
|
*
|
|
217
218
|
* The tag is mounted in `toDOM` and taken down in `destroy`, so it lives exactly as long as the host
|
|
218
219
|
* element ProseMirror drew it into — see `mountContributorTag`.
|
|
@@ -226,7 +227,7 @@ export const createContributorTagWidget = ({
|
|
|
226
227
|
diffId,
|
|
227
228
|
mountContext
|
|
228
229
|
}) => {
|
|
229
|
-
var _hoisted$
|
|
230
|
+
var _hoisted$codeBlockSta, _ref2, _hoisted$anchorName;
|
|
230
231
|
if (!isContributorTagWidgetEnabled()) {
|
|
231
232
|
return undefined;
|
|
232
233
|
}
|
|
@@ -238,15 +239,32 @@ export const createContributorTagWidget = ({
|
|
|
238
239
|
// A change inside a code block is hoisted out of it, anchored to its own marker; anything else
|
|
239
240
|
// keeps its own anchor.
|
|
240
241
|
const hoisted = resolveHoistedCodeBlockAnchor(doc, anchorPos, diffId);
|
|
241
|
-
|
|
242
|
+
// Keep the host out of the table row's grid (EDITOR-8442).
|
|
243
|
+
const hostPos = clampAnchorPosIntoCell(doc, (_hoisted$codeBlockSta = hoisted === null || hoisted === void 0 ? void 0 : hoisted.codeBlockStart) !== null && _hoisted$codeBlockSta !== void 0 ? _hoisted$codeBlockSta : anchorPos, 1);
|
|
244
|
+
|
|
245
|
+
// Inline diffs pass no anchorName. Give them their own point anchor instead of the
|
|
246
|
+
// `position: relative` fallback, which drifts left once the range wraps a line (EDITOR-9286).
|
|
247
|
+
const ownAnchorName = buildAnchorDecorationKey({
|
|
248
|
+
diffId,
|
|
249
|
+
anchorType: AnchorTypeKey.tag
|
|
250
|
+
});
|
|
251
|
+
const effectiveAnchorName = (_ref2 = (_hoisted$anchorName = hoisted === null || hoisted === void 0 ? void 0 : hoisted.anchorName) !== null && _hoisted$anchorName !== void 0 ? _hoisted$anchorName : anchorName) !== null && _ref2 !== void 0 ? _ref2 : ownAnchorName;
|
|
242
252
|
const tagAnchorName = effectiveAnchorName && supportsAnchorPositioning() ? effectiveAnchorName : undefined;
|
|
243
253
|
const decorations = [];
|
|
244
254
|
if (hoisted) {
|
|
245
255
|
decorations.push(hoisted.marker);
|
|
256
|
+
} else if (!anchorName && tagAnchorName) {
|
|
257
|
+
decorations.push(Decoration.widget(hostPos, () => createAnchorNameSpan(ownAnchorName), {
|
|
258
|
+
...buildAnchorDecorationSpec({
|
|
259
|
+
diffId,
|
|
260
|
+
anchorType: AnchorTypeKey.tag,
|
|
261
|
+
side: -1
|
|
262
|
+
}),
|
|
263
|
+
marks: [],
|
|
264
|
+
ignoreSelection: true
|
|
265
|
+
}));
|
|
246
266
|
}
|
|
247
|
-
decorations.push(Decoration.widget(
|
|
248
|
-
// Keep the host out of the table row's grid (EDITOR-8442).
|
|
249
|
-
clampAnchorPosIntoCell(doc, (_hoisted$codeBlockSta = hoisted === null || hoisted === void 0 ? void 0 : hoisted.codeBlockStart) !== null && _hoisted$codeBlockSta !== void 0 ? _hoisted$codeBlockSta : anchorPos, 1), () => {
|
|
267
|
+
decorations.push(Decoration.widget(hostPos, () => {
|
|
250
268
|
const host = buildContributorTagHost(diffId, tagAnchorName);
|
|
251
269
|
mount = mountContributorTag({
|
|
252
270
|
anchorName: tagAnchorName,
|
|
@@ -2,7 +2,7 @@ import { Decoration } from '@atlaskit/editor-prosemirror/view';
|
|
|
2
2
|
import { fg } from '@atlaskit/platform-feature-flags/fg';
|
|
3
3
|
import { countEmptyTextBlockOnlySlice } from '../utils/emptyTextBlocks';
|
|
4
4
|
import { isEmptyParagraphSlice } from '../utils/isEmptyParagraphSlice';
|
|
5
|
-
import { clampAnchorPosIntoCell, createLeftAnchorWidget } from './createAnchorDecorationWidgets';
|
|
5
|
+
import { clampAnchorPosIntoCell, createAnchorNameSpan, createLeftAnchorWidget } from './createAnchorDecorationWidgets';
|
|
6
6
|
import { createChangedRowDecorationWidgets } from './createChangedRowDecorationWidgets';
|
|
7
7
|
import { createContributorTagHost, resolveHoistedCodeBlockAnchor, unmountContributorTag } from './createContributorTagWidget';
|
|
8
8
|
import { AnchorTypeKey, buildContributorTagDecorationSpec, buildDiffDecorationSpec, buildAnchorDecorationKey, scrollMarginTopValue } from './decorationKeys';
|
|
@@ -14,6 +14,22 @@ import { findSafeInsertPos } from './utils/findSafeInsertPos';
|
|
|
14
14
|
import { safeResolve } from './utils/safeResolve';
|
|
15
15
|
import { getTableDiffMode } from './utils/tableDiffMode';
|
|
16
16
|
import { wrapBlockNodeView, createRemovedLozenge, injectInnerWrapper, createContentWrapper } from './utils/wrapBlockNodeView';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Where a slice's `index`th child sits in `doc`: the children belong to the ancestor `openStart`
|
|
20
|
+
* levels above the change. `null` if out of range — `posAtIndex` throws past the last child.
|
|
21
|
+
*/
|
|
22
|
+
const getSourcePos = (doc, $changeFrom, openStart, index) => {
|
|
23
|
+
const depth = $changeFrom ? $changeFrom.depth - openStart : -1;
|
|
24
|
+
if (!$changeFrom || depth < 0) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const childIndex = $changeFrom.index(depth) + index;
|
|
28
|
+
if (childIndex >= $changeFrom.node(depth).childCount) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return safeResolve(doc, $changeFrom.posAtIndex(childIndex, depth));
|
|
32
|
+
};
|
|
17
33
|
const isHeadingLevel = level => typeof level === 'number' && level >= 1 && level <= 6;
|
|
18
34
|
|
|
19
35
|
/**
|
|
@@ -282,7 +298,7 @@ export const createNodeChangedDecorationWidget = ({
|
|
|
282
298
|
* and if it's the first or last content, we go in however many the sliced Open
|
|
283
299
|
* or sliced End depth is and match only the entire node.
|
|
284
300
|
*/
|
|
285
|
-
slice.content.forEach(node => {
|
|
301
|
+
slice.content.forEach((node, _offset, index) => {
|
|
286
302
|
const isFirst = firstReplacedNode === node;
|
|
287
303
|
const isLast = lastReplacedNode === node;
|
|
288
304
|
const isOpenAtSliceBoundary = isFirst && slice.openStart > 0 || isLast && slice.openEnd > 0;
|
|
@@ -347,7 +363,8 @@ export const createNodeChangedDecorationWidget = ({
|
|
|
347
363
|
}
|
|
348
364
|
|
|
349
365
|
// Try to create node view, fallback to serialization
|
|
350
|
-
const
|
|
366
|
+
const $sourcePos = fg('platform_editor_ai_show_diff_patch_2') ? getSourcePos(doc, $changeFromA, slice.openStart, index) : null;
|
|
367
|
+
const nodeView = serializer.tryCreateNodeView(node, 0, $sourcePos !== null && $sourcePos !== void 0 ? $sourcePos : undefined);
|
|
351
368
|
if (nodeView) {
|
|
352
369
|
if (node.isInline) {
|
|
353
370
|
const wrapper = createContentWrapper(colorScheme, isActive, isInserted, reveal);
|
|
@@ -450,18 +467,25 @@ export const createNodeChangedDecorationWidget = ({
|
|
|
450
467
|
});
|
|
451
468
|
}
|
|
452
469
|
|
|
453
|
-
// Needed even
|
|
454
|
-
// widget.
|
|
470
|
+
// Needed even with the indicator bar off, since it anchors its top/bottom to the widget.
|
|
455
471
|
if (showIndicators || showContributorTags) {
|
|
456
472
|
dom.style.setProperty('anchor-name', `--${buildAnchorDecorationKey({
|
|
457
473
|
diffId
|
|
458
474
|
})}`);
|
|
459
|
-
//
|
|
460
|
-
//
|
|
461
|
-
if (fg('confluence_ncs_step_diffing_version_history')) {
|
|
462
|
-
|
|
463
|
-
diffId
|
|
475
|
+
// `dom` is inline, so its box fragments across a line wrap and `anchor()` resolves to the
|
|
476
|
+
// wrong edge. The tag anchors to its own point marker instead (EDITOR-9286).
|
|
477
|
+
if (fg('confluence_ncs_step_diffing_version_history') && !contributorTagAnchorName) {
|
|
478
|
+
const tagPointAnchorName = buildAnchorDecorationKey({
|
|
479
|
+
diffId,
|
|
480
|
+
anchorType: AnchorTypeKey.tag
|
|
464
481
|
});
|
|
482
|
+
contributorTagAnchorName = tagPointAnchorName;
|
|
483
|
+
decorations.push(Decoration.widget(safeInsertPos, () => createAnchorNameSpan(tagPointAnchorName), {
|
|
484
|
+
key: `contributor-tag-anchor-${diffId}`,
|
|
485
|
+
side: -1,
|
|
486
|
+
marks: [],
|
|
487
|
+
ignoreSelection: true
|
|
488
|
+
}));
|
|
465
489
|
}
|
|
466
490
|
}
|
|
467
491
|
|
|
@@ -495,8 +519,7 @@ export const createNodeChangedDecorationWidget = ({
|
|
|
495
519
|
}
|
|
496
520
|
}));
|
|
497
521
|
} else {
|
|
498
|
-
//
|
|
499
|
-
// the deleted content wraps its box is the union of its line fragments.
|
|
522
|
+
// Anchored to the point marker pushed above, not to `dom` (EDITOR-9286).
|
|
500
523
|
const tagHost = createContributorTagHost({
|
|
501
524
|
anchorName: contributorTagAnchorName,
|
|
502
525
|
diffId,
|
|
@@ -311,6 +311,19 @@ const applyTextLikeBlockNodeStyles = ({
|
|
|
311
311
|
/**
|
|
312
312
|
* Creates a content wrapper with deleted styles for a block node
|
|
313
313
|
*/
|
|
314
|
+
/**
|
|
315
|
+
* The element that spans breakout width, for nodes carrying a `breakout` mark. The wrapper divs
|
|
316
|
+
* stay at editor-column width, so anything that should align to the node's visible edges —
|
|
317
|
+
* outlines, absolutely positioned lozenges — has to attach here instead.
|
|
318
|
+
*/
|
|
319
|
+
const findBreakoutElement = (targetNode, nodeView) => {
|
|
320
|
+
var _targetNode$marks;
|
|
321
|
+
const hasBreakoutMark = (_targetNode$marks = targetNode.marks) === null || _targetNode$marks === void 0 ? void 0 : _targetNode$marks.some(mark => mark.type.name === 'breakout');
|
|
322
|
+
if (!hasBreakoutMark || !(nodeView instanceof HTMLElement)) {
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
return nodeView.matches('[data-node-type]') ? nodeView : nodeView.querySelector('[data-node-type]');
|
|
326
|
+
};
|
|
314
327
|
const createBlockNodeContentWrapper = ({
|
|
315
328
|
nodeView,
|
|
316
329
|
targetNode,
|
|
@@ -328,7 +341,16 @@ const createBlockNodeContentWrapper = ({
|
|
|
328
341
|
// should not also get the inline content style (borderBottom underline) on their container.
|
|
329
342
|
const shouldSkipContentStyle = isInserted && nodeStyle !== undefined;
|
|
330
343
|
const contentStyle = shouldSkipContentStyle ? '' : getChangedContentStyle(colorScheme, isActive, isInserted, hideAddedDiffsUnderline);
|
|
331
|
-
|
|
344
|
+
|
|
345
|
+
// A `breakout` node renders wider than the editor column while the wrapper divs
|
|
346
|
+
// stay at column width, so paint the outline on the node element that spans that width.
|
|
347
|
+
const breakoutTarget = nodeStyle ? findBreakoutElement(targetNode, nodeView) : null;
|
|
348
|
+
if (fg('platform_editor_ai_show_diff_patch_2') && breakoutTarget) {
|
|
349
|
+
appendStyleToElement(breakoutTarget, nodeStyle !== null && nodeStyle !== void 0 ? nodeStyle : '');
|
|
350
|
+
contentWrapper.setAttribute('style', contentStyle || '');
|
|
351
|
+
} else {
|
|
352
|
+
contentWrapper.setAttribute('style', `${contentStyle}${nodeStyle || ''}`);
|
|
353
|
+
}
|
|
332
354
|
contentWrapper.append(nodeView);
|
|
333
355
|
return contentWrapper;
|
|
334
356
|
};
|
|
@@ -456,7 +478,19 @@ const wrapBlockNode = ({
|
|
|
456
478
|
})) {
|
|
457
479
|
return;
|
|
458
480
|
}
|
|
459
|
-
|
|
481
|
+
|
|
482
|
+
// `lozenge` is absolutely positioned, so it lands on the nearest positioned ancestor. For a
|
|
483
|
+
// breakout node that is the column-width wrapper, leaving the pill short of the node's
|
|
484
|
+
// right edge — host it on the breakout element, which must establish a containing block.
|
|
485
|
+
const breakoutHost = fg('platform_editor_ai_show_diff_patch_2') ? findBreakoutElement(targetNode, nodeView) : null;
|
|
486
|
+
if (breakoutHost) {
|
|
487
|
+
appendStyleToElement(breakoutHost, convertToInlineCss({
|
|
488
|
+
position: 'relative'
|
|
489
|
+
}));
|
|
490
|
+
breakoutHost.append(lozenge);
|
|
491
|
+
} else {
|
|
492
|
+
blockWrapper.append(lozenge);
|
|
493
|
+
}
|
|
460
494
|
}
|
|
461
495
|
const contentWrapper = createBlockNodeContentWrapper({
|
|
462
496
|
nodeView,
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
|
|
2
|
+
import { fg } from '@atlaskit/platform-feature-flags/fg';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `@atlaskit/editor-prosemirror/view` doesn't re-export prosemirror-view's `MarkViewConstructor`,
|
|
6
|
+
* so it's typed locally, as `EditorSSRRenderer` does.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Where to look up a mark's renderer, and the view to render it against. */
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Renders a mark as ProseMirror does: `view.nodeViews` also holds mark views, keyed by mark name,
|
|
13
|
+
* so `breakout` resolves whether it's registered under `markViews` or `nodeViews`. Falls back to
|
|
14
|
+
* `toDOM` — they can differ: `breakout` renders `.fabric-editor-breakout-mark-dom`, which the width
|
|
15
|
+
* CSS targets.
|
|
16
|
+
*/
|
|
17
|
+
const renderMarkView = (mark, inline, {
|
|
18
|
+
nodeViews,
|
|
19
|
+
view
|
|
20
|
+
}) => {
|
|
21
|
+
try {
|
|
22
|
+
const markView = nodeViews === null || nodeViews === void 0 ? void 0 : nodeViews[mark.type.name];
|
|
23
|
+
if (markView && view) {
|
|
24
|
+
const rendered = markView(mark, view, inline);
|
|
25
|
+
return {
|
|
26
|
+
dom: rendered.dom,
|
|
27
|
+
contentDOM: rendered.contentDOM
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const toDOM = mark.type.spec.toDOM;
|
|
31
|
+
if (!toDOM) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return DOMSerializer.renderSpec(document, toDOM(mark, inline));
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Wraps rendered DOM in its marks, as ProseMirror does for the live document. Without this a
|
|
42
|
+
* block node loses `breakout`, whose wrapper supplies the width. Marks nest
|
|
43
|
+
* outside-in, so `marks[0]` is outermost.
|
|
44
|
+
*/
|
|
45
|
+
export const wrapInMarkViews = (targetNode, nodeDom, sources) => {
|
|
46
|
+
if (targetNode.marks.length === 0 || !fg('platform_editor_ai_show_diff_patch_2')) {
|
|
47
|
+
return nodeDom;
|
|
48
|
+
}
|
|
49
|
+
let wrapped = nodeDom;
|
|
50
|
+
for (let index = targetNode.marks.length - 1; index >= 0; index--) {
|
|
51
|
+
const mark = targetNode.marks[index];
|
|
52
|
+
const rendered = renderMarkView(mark, targetNode.isInline, sources);
|
|
53
|
+
// No content hole means nowhere to nest the node, so skip the mark, not the node.
|
|
54
|
+
if (!(rendered !== null && rendered !== void 0 && rendered.contentDOM)) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
rendered.contentDOM.appendChild(wrapped);
|
|
58
|
+
wrapped = rendered.dom;
|
|
59
|
+
}
|
|
60
|
+
return wrapped;
|
|
61
|
+
};
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
2
|
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
+
import { expandedState } from '@atlaskit/editor-common/expand';
|
|
3
4
|
import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
|
|
5
|
+
import { contains } from '@atlaskit/editor-prosemirror/utils';
|
|
4
6
|
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
|
|
7
|
+
import { fg } from '@atlaskit/platform-feature-flags/fg';
|
|
8
|
+
import { createEditorProxy } from './createEditorProxy';
|
|
9
|
+
import { wrapInMarkViews } from './markViews';
|
|
5
10
|
|
|
6
11
|
/**
|
|
7
12
|
* Utilities for working with ProseMirror node views and DOM serialization within the
|
|
@@ -30,6 +35,16 @@ export function isEditorViewWithNodeViews(view) {
|
|
|
30
35
|
return view.nodeViews !== undefined;
|
|
31
36
|
}
|
|
32
37
|
|
|
38
|
+
/** Expand-family nodes whose expanded state lives in a node-keyed WeakMap. */
|
|
39
|
+
var EXPAND_TYPES = new Set(['expand', 'nestedExpand']);
|
|
40
|
+
|
|
41
|
+
/** Tables are the only node views that read and write positions, so only they need a preview. */
|
|
42
|
+
var containsTable = function containsTable(node) {
|
|
43
|
+
var table = node.type.schema.nodes.table;
|
|
44
|
+
// `contains` only checks descendants, so the node itself is checked separately.
|
|
45
|
+
return !!table && (node.type === table || contains(node, table));
|
|
46
|
+
};
|
|
47
|
+
|
|
33
48
|
/**
|
|
34
49
|
* Encapsulates DOM serialization and node view access/creation.
|
|
35
50
|
*
|
|
@@ -75,11 +90,18 @@ export var NodeViewSerializer = /*#__PURE__*/function () {
|
|
|
75
90
|
key: "appendChildNodes",
|
|
76
91
|
value: function appendChildNodes(children, contentDOM) {
|
|
77
92
|
var _this = this;
|
|
93
|
+
var basePos = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
|
|
94
|
+
var editorProxy = arguments.length > 3 ? arguments[3] : undefined;
|
|
95
|
+
// A node's first child sits one position inside it, and each subsequent
|
|
96
|
+
// child is offset by the previous child's `nodeSize`. Tracking this lets nested node
|
|
97
|
+
// views resolve to their real depth instead of every one of them seeing depth 0.
|
|
98
|
+
var childPos = basePos + 1;
|
|
78
99
|
children.forEach(function (child) {
|
|
79
|
-
var childNode = _this.
|
|
100
|
+
var childNode = _this.tryCreateNodeViewInner(child, childPos, editorProxy) || _this.serializeNode(child);
|
|
80
101
|
if (childNode) {
|
|
81
102
|
contentDOM === null || contentDOM === void 0 || contentDOM.append(childNode);
|
|
82
103
|
}
|
|
104
|
+
childPos += child.nodeSize;
|
|
83
105
|
});
|
|
84
106
|
}
|
|
85
107
|
|
|
@@ -88,11 +110,29 @@ export var NodeViewSerializer = /*#__PURE__*/function () {
|
|
|
88
110
|
*
|
|
89
111
|
* Returns `null` when there is no `EditorView`, no constructor for the node type,
|
|
90
112
|
* or the node type is blocklisted. Otherwise returns the constructed node view instance.
|
|
113
|
+
*
|
|
114
|
+
* `$sourcePos` is where the node sits in the doc it came from, so nested tables keep their depth.
|
|
91
115
|
*/
|
|
92
116
|
}, {
|
|
93
117
|
key: "tryCreateNodeView",
|
|
94
118
|
value: function tryCreateNodeView(targetNode) {
|
|
119
|
+
var _preview$rootPos;
|
|
120
|
+
var basePos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
121
|
+
var $sourcePos = arguments.length > 2 ? arguments[2] : undefined;
|
|
122
|
+
if (!this.editorView) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
// The preview becomes the document every nested node view resolves its position against,
|
|
126
|
+
// so positions stay internally consistent.
|
|
127
|
+
var preview = fg('platform_editor_ai_show_diff_patch_2') && containsTable(targetNode) ? createEditorProxy(this.editorView, targetNode, $sourcePos) : null;
|
|
128
|
+
return this.tryCreateNodeViewInner(targetNode, (_preview$rootPos = preview === null || preview === void 0 ? void 0 : preview.rootPos) !== null && _preview$rootPos !== void 0 ? _preview$rootPos : basePos, preview === null || preview === void 0 ? void 0 : preview.editorProxy);
|
|
129
|
+
}
|
|
130
|
+
}, {
|
|
131
|
+
key: "tryCreateNodeViewInner",
|
|
132
|
+
value: function tryCreateNodeViewInner(targetNode) {
|
|
95
133
|
var _this$nodeViews;
|
|
134
|
+
var basePos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
135
|
+
var editorProxy = arguments.length > 2 ? arguments[2] : undefined;
|
|
96
136
|
if (!this.editorView) {
|
|
97
137
|
return null;
|
|
98
138
|
}
|
|
@@ -138,23 +178,47 @@ export var NodeViewSerializer = /*#__PURE__*/function () {
|
|
|
138
178
|
if (!isExperimentEnabled('platform_editor_show_diff_deleted_nodeview_content') && targetNode.type.name === 'paragraph' && targetNode.children.length === 1) {
|
|
139
179
|
return this.serializeFragment(targetNode.content);
|
|
140
180
|
}
|
|
141
|
-
this.appendChildNodes(targetNode.children, _contentDOM);
|
|
181
|
+
this.appendChildNodes(targetNode.children, _contentDOM, basePos, editorProxy);
|
|
142
182
|
}
|
|
143
|
-
return _dom;
|
|
183
|
+
return this.withMarkViews(targetNode, _dom, editorProxy);
|
|
144
184
|
}
|
|
145
|
-
|
|
146
|
-
|
|
185
|
+
|
|
186
|
+
// The expand node view reads `expandedState.get(node) ?? false`, never `attrs.__expanded`.
|
|
187
|
+
// Open the preview so its tables get real layout; an existing entry is the live expand's
|
|
188
|
+
// own state, so leave it alone.
|
|
189
|
+
if (EXPAND_TYPES.has(targetNode.type.name) && !expandedState.has(targetNode) && fg('platform_editor_ai_show_diff_patch_2')) {
|
|
190
|
+
expandedState.set(targetNode, true);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// `isTableNested` is `doc.resolve(pos).depth > 0`, so the old hardcoded `0` made nested
|
|
194
|
+
// tables size as top-level. Positions are resolved against the preview document, so
|
|
195
|
+
// they are only meaningful with an editor proxy.
|
|
196
|
+
var view = editorProxy !== null && editorProxy !== void 0 ? editorProxy : this.editorView;
|
|
197
|
+
var docSize = view.state.doc.content.size;
|
|
198
|
+
var resolvedPos = editorProxy ? Math.max(0, Math.min(basePos, docSize)) : 0;
|
|
199
|
+
var _constructor = constructor(targetNode, view, function () {
|
|
200
|
+
return resolvedPos;
|
|
147
201
|
}, [], {}),
|
|
148
202
|
dom = _constructor.dom,
|
|
149
203
|
contentDOM = _constructor.contentDOM;
|
|
150
204
|
// Iteratively populate children
|
|
151
|
-
this.appendChildNodes(targetNode.children, contentDOM);
|
|
152
|
-
return dom;
|
|
205
|
+
this.appendChildNodes(targetNode.children, contentDOM, basePos, editorProxy);
|
|
206
|
+
return this.withMarkViews(targetNode, dom, editorProxy);
|
|
153
207
|
} catch (_unused) {
|
|
154
208
|
return null;
|
|
155
209
|
}
|
|
156
210
|
}
|
|
157
211
|
|
|
212
|
+
/** Wraps rendered DOM in its marks, rendered against the editor proxy when there is one. */
|
|
213
|
+
}, {
|
|
214
|
+
key: "withMarkViews",
|
|
215
|
+
value: function withMarkViews(targetNode, nodeDom, editorProxy) {
|
|
216
|
+
return wrapInMarkViews(targetNode, nodeDom, {
|
|
217
|
+
nodeViews: this.nodeViews,
|
|
218
|
+
view: editorProxy !== null && editorProxy !== void 0 ? editorProxy : this.editorView
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
158
222
|
/**
|
|
159
223
|
* Serializes a node to a DOM `Node` using the schema's `DOMSerializer`.
|
|
160
224
|
*/
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { GapCursorSelection, Side } from '@atlaskit/editor-common/selection/gap-cursor/selection';
|
|
2
|
+
/**
|
|
3
|
+
* A preview is rendered by the live view's own node views, which read `getPos()` against
|
|
4
|
+
* `view.state.doc` for both layout (`isTableNested`) and writes (`scaleTable` builds attribute
|
|
5
|
+
* steps at `getPos() + 1`). Handing them live positions makes the layout correct but points the
|
|
6
|
+
* writes at unrelated live nodes, so give them a view whose document *is* the preview instead.
|
|
7
|
+
*
|
|
8
|
+
* With `$sourcePos` (the node's position in the doc it came from) the node is swapped into a copy
|
|
9
|
+
* of its real top-level ancestor, so a table inside an expand still resolves as nested.
|
|
10
|
+
*
|
|
11
|
+
* Returns `null` when the node cannot be a top-level child (e.g. a bare `tableCell`), in which
|
|
12
|
+
* case the caller falls back to the live view and the previous stubbed positions.
|
|
13
|
+
*/
|
|
14
|
+
export function createEditorProxy(liveView, rootNode, $sourcePos) {
|
|
15
|
+
var liveState = liveView.state;
|
|
16
|
+
var createDoc = function createDoc(topLevel) {
|
|
17
|
+
return liveState.schema.topNodeType.createChecked(null, topLevel);
|
|
18
|
+
};
|
|
19
|
+
var previewDoc;
|
|
20
|
+
var rootPos = 0;
|
|
21
|
+
try {
|
|
22
|
+
var _$sourcePos$nodeAfter;
|
|
23
|
+
if ($sourcePos && $sourcePos.depth > 0 && ((_$sourcePos$nodeAfter = $sourcePos.nodeAfter) === null || _$sourcePos$nodeAfter === void 0 ? void 0 : _$sourcePos$nodeAfter.type) === rootNode.type) {
|
|
24
|
+
var topLevel = rootNode;
|
|
25
|
+
for (var depth = $sourcePos.depth; depth > 0; depth--) {
|
|
26
|
+
var ancestor = $sourcePos.node(depth);
|
|
27
|
+
topLevel = ancestor.copy(ancestor.content.replaceChild($sourcePos.index(depth), topLevel));
|
|
28
|
+
}
|
|
29
|
+
previewDoc = createDoc(topLevel);
|
|
30
|
+
rootPos = $sourcePos.pos - $sourcePos.before(1);
|
|
31
|
+
} else {
|
|
32
|
+
previewDoc = createDoc(rootNode);
|
|
33
|
+
}
|
|
34
|
+
} catch (_unused) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// A gap cursor just before the node selects nothing, so the table's node view doesn't render
|
|
39
|
+
// as selected or show its controls.
|
|
40
|
+
var previewSelection = new GapCursorSelection(previewDoc.resolve(rootPos), Side.LEFT);
|
|
41
|
+
// Proxy rather than `EditorState.create` so plugin state is shared as-is: re-initialising
|
|
42
|
+
// every plugin per preview would be costly and re-runs their side effects.
|
|
43
|
+
var stateProxy = new Proxy(liveState, {
|
|
44
|
+
get: function get(target, prop, receiver) {
|
|
45
|
+
if (prop === 'doc') {
|
|
46
|
+
return previewDoc;
|
|
47
|
+
}
|
|
48
|
+
if (prop === 'selection') {
|
|
49
|
+
return previewSelection;
|
|
50
|
+
}
|
|
51
|
+
return Reflect.get(target, prop, receiver);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
var editorProxy = new Proxy(liveView, {
|
|
55
|
+
get: function get(target, prop, receiver) {
|
|
56
|
+
if (prop === 'state') {
|
|
57
|
+
return stateProxy;
|
|
58
|
+
}
|
|
59
|
+
if (prop === 'dispatch') {
|
|
60
|
+
// Keep the transaction inside the preview. It was built against `previewDoc`, so
|
|
61
|
+
// applying it to the live state would corrupt the real document.
|
|
62
|
+
return function (tr) {
|
|
63
|
+
try {
|
|
64
|
+
stateProxy = stateProxy.apply(tr);
|
|
65
|
+
} catch (_unused2) {
|
|
66
|
+
// A preview is throwaway; a failed self-update is not worth surfacing.
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
var value = Reflect.get(target, prop, receiver);
|
|
71
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
editorProxy: editorProxy,
|
|
76
|
+
rootPos: rootPos
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -5,8 +5,8 @@ import { Decoration } from '@atlaskit/editor-prosemirror/view';
|
|
|
5
5
|
import { fg } from '@atlaskit/platform-feature-flags/fg';
|
|
6
6
|
import { ContributorTagController } from '../../ui/ContributorTag/contributorTagController';
|
|
7
7
|
import { buildCharsByOffset, isWhitespaceChar } from '../utils/charsByOffset';
|
|
8
|
-
import { clampAnchorPosIntoCell } from './createAnchorDecorationWidgets';
|
|
9
|
-
import { AnchorTypeKey, buildAnchorDecorationKey, buildContributorTagDecorationSpec } from './decorationKeys';
|
|
8
|
+
import { clampAnchorPosIntoCell, createAnchorNameSpan } from './createAnchorDecorationWidgets';
|
|
9
|
+
import { AnchorTypeKey, buildAnchorDecorationKey, buildAnchorDecorationSpec, buildContributorTagDecorationSpec } from './decorationKeys';
|
|
10
10
|
|
|
11
11
|
/** Marks the host element of one tag, so a tag can be found in the document by its own diff. */
|
|
12
12
|
export var CONTRIBUTOR_TAG_HOST_ATTRIBUTE = 'data-contributor-tag-host';
|
|
@@ -218,13 +218,14 @@ export var resolveHoistedCodeBlockAnchor = function resolveHoistedCodeBlockAncho
|
|
|
218
218
|
* highlights — or, with `anchorAtRangeStart`, at `from` itself. A host inside a code block is
|
|
219
219
|
* hoisted in front of it instead, anchored via `resolveHoistedCodeBlockAnchor` (EDITOR-9045).
|
|
220
220
|
*
|
|
221
|
-
* Returns one decoration
|
|
221
|
+
* Returns one decoration when a caller-supplied `anchorName` is used, two (marker + tag)
|
|
222
|
+
* otherwise — hoisted out of a code block, or anchored to its own point marker.
|
|
222
223
|
*
|
|
223
224
|
* The tag is mounted in `toDOM` and taken down in `destroy`, so it lives exactly as long as the host
|
|
224
225
|
* element ProseMirror drew it into — see `mountContributorTag`.
|
|
225
226
|
*/
|
|
226
227
|
export var createContributorTagWidget = function createContributorTagWidget(_ref4) {
|
|
227
|
-
var _hoisted$
|
|
228
|
+
var _hoisted$codeBlockSta, _ref5, _hoisted$anchorName;
|
|
228
229
|
var _ref4$anchorAtRangeSt = _ref4.anchorAtRangeStart,
|
|
229
230
|
anchorAtRangeStart = _ref4$anchorAtRangeSt === void 0 ? false : _ref4$anchorAtRangeSt,
|
|
230
231
|
anchorName = _ref4.anchorName,
|
|
@@ -244,15 +245,33 @@ export var createContributorTagWidget = function createContributorTagWidget(_ref
|
|
|
244
245
|
// A change inside a code block is hoisted out of it, anchored to its own marker; anything else
|
|
245
246
|
// keeps its own anchor.
|
|
246
247
|
var hoisted = resolveHoistedCodeBlockAnchor(doc, anchorPos, diffId);
|
|
247
|
-
|
|
248
|
+
// Keep the host out of the table row's grid (EDITOR-8442).
|
|
249
|
+
var hostPos = clampAnchorPosIntoCell(doc, (_hoisted$codeBlockSta = hoisted === null || hoisted === void 0 ? void 0 : hoisted.codeBlockStart) !== null && _hoisted$codeBlockSta !== void 0 ? _hoisted$codeBlockSta : anchorPos, 1);
|
|
250
|
+
|
|
251
|
+
// Inline diffs pass no anchorName. Give them their own point anchor instead of the
|
|
252
|
+
// `position: relative` fallback, which drifts left once the range wraps a line (EDITOR-9286).
|
|
253
|
+
var ownAnchorName = buildAnchorDecorationKey({
|
|
254
|
+
diffId: diffId,
|
|
255
|
+
anchorType: AnchorTypeKey.tag
|
|
256
|
+
});
|
|
257
|
+
var effectiveAnchorName = (_ref5 = (_hoisted$anchorName = hoisted === null || hoisted === void 0 ? void 0 : hoisted.anchorName) !== null && _hoisted$anchorName !== void 0 ? _hoisted$anchorName : anchorName) !== null && _ref5 !== void 0 ? _ref5 : ownAnchorName;
|
|
248
258
|
var tagAnchorName = effectiveAnchorName && supportsAnchorPositioning() ? effectiveAnchorName : undefined;
|
|
249
259
|
var decorations = [];
|
|
250
260
|
if (hoisted) {
|
|
251
261
|
decorations.push(hoisted.marker);
|
|
262
|
+
} else if (!anchorName && tagAnchorName) {
|
|
263
|
+
decorations.push(Decoration.widget(hostPos, function () {
|
|
264
|
+
return createAnchorNameSpan(ownAnchorName);
|
|
265
|
+
}, _objectSpread(_objectSpread({}, buildAnchorDecorationSpec({
|
|
266
|
+
diffId: diffId,
|
|
267
|
+
anchorType: AnchorTypeKey.tag,
|
|
268
|
+
side: -1
|
|
269
|
+
})), {}, {
|
|
270
|
+
marks: [],
|
|
271
|
+
ignoreSelection: true
|
|
272
|
+
})));
|
|
252
273
|
}
|
|
253
|
-
decorations.push(Decoration.widget(
|
|
254
|
-
// Keep the host out of the table row's grid (EDITOR-8442).
|
|
255
|
-
clampAnchorPosIntoCell(doc, (_hoisted$codeBlockSta = hoisted === null || hoisted === void 0 ? void 0 : hoisted.codeBlockStart) !== null && _hoisted$codeBlockSta !== void 0 ? _hoisted$codeBlockSta : anchorPos, 1), function () {
|
|
274
|
+
decorations.push(Decoration.widget(hostPos, function () {
|
|
256
275
|
var host = buildContributorTagHost(diffId, tagAnchorName);
|
|
257
276
|
mount = mountContributorTag({
|
|
258
277
|
anchorName: tagAnchorName,
|