Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,11 @@ describe('patchRawProperties validates patch op shapes', () => {

describe('create.contentControl validation', () => {
const createAdapter = { create: mock(noop) } as any;
const validAt = {
kind: 'selection' as const,
start: { kind: 'text' as const, blockId: 'p1', offset: 0 },
end: { kind: 'text' as const, blockId: 'p1', offset: 5 },
};

it('rejects null input', () => {
expect(() => executeCreateContentControl(createAdapter, null as any)).toThrow(/non-null object/);
Expand Down Expand Up @@ -652,4 +657,43 @@ describe('create.contentControl validation', () => {
});
expect(adapter.create).toHaveBeenCalled();
});

it('rejects at and target together', () => {
expect(() =>
executeCreateContentControl(createAdapter, {
kind: 'inline',
target: validTarget,
at: validAt,
} as any),
).toThrow(/mutually exclusive/);
});

it('rejects invalid at (missing start)', () => {
expect(() =>
executeCreateContentControl(createAdapter, {
kind: 'inline',
at: { kind: 'selection', end: { kind: 'text', blockId: 'p1', offset: 5 } },
} as any),
).toThrow(/valid SelectionTarget/);
});

it('rejects invalid at (wrong kind)', () => {
expect(() =>
executeCreateContentControl(createAdapter, {
kind: 'inline',
at: { ...validAt, kind: 'bogus' },
} as any),
).toThrow(/valid SelectionTarget/);
});

it('accepts valid at (SelectionTarget)', () => {
const adapter = { create: mock(noop) } as any;
executeCreateContentControl(adapter, {
kind: 'inline',
at: validAt,
tag: 'name',
alias: 'Name',
});
expect(adapter.create).toHaveBeenCalled();
});
});
15 changes: 15 additions & 0 deletions packages/document-api/src/content-controls/content-controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import type { MutationOptions } from '../write/write.js';
import { DocumentApiValidationError } from '../errors.js';
import { isRecord, isInteger } from '../validation-primitives.js';
import { isSelectionTarget } from '../validation/selection-target-validator.js';
import { LOCK_MODES, CONTENT_CONTROL_TYPES, CONTENT_CONTROL_APPEARANCES } from './content-controls.types.js';
import type { NodeKind } from '../types/base.js';
import { NODE_KINDS } from '../types/base.js';
Expand Down Expand Up @@ -1033,9 +1034,23 @@ export function executeCreateContentControl(
{ field: 'lockMode', value: input.lockMode },
);
}
if (input.at !== undefined && input.target !== undefined) {
throw new DocumentApiValidationError(
'INVALID_INPUT',
`create.contentControl: "at" and "target" are mutually exclusive — provide one or neither.`,
{ field: 'at' },
);
}
if (input.target !== undefined) {
validateCCTarget(input.target, 'create.contentControl');
}
if (input.at !== undefined && !isSelectionTarget(input.at)) {
throw new DocumentApiValidationError(
'INVALID_INPUT',
`create.contentControl: "at" must be a valid SelectionTarget with kind "selection", start, and end.`,
{ field: 'at', value: input.at },
);
}
if (input.content !== undefined && typeof input.content !== 'string') {
throw new DocumentApiValidationError(
'INVALID_INPUT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import type { NodeKind } from '../types/base.js';
import type { SelectionTarget } from '../types/address.js';
import type { ReceiptFailure } from '../types/receipt.js';

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -206,6 +207,12 @@ export interface CreateContentControlInput {
kind: NodeKind;
controlType?: ContentControlType;
target?: ContentControlTarget;
/**
* Text range to wrap in the new content control. Mutually exclusive with `target`.
* When `content` is also provided, it replaces the selected text inside the new SDT.
* When `content` is omitted, the existing text in the range becomes the SDT content.
*/
at?: SelectionTarget;
tag?: string;
alias?: string;
lockMode?: LockMode;
Expand Down
1 change: 1 addition & 0 deletions packages/document-api/src/contract/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@
const trackedChangeAddressSchema = ref('TrackedChangeAddress');
const entityAddressSchema = ref('EntityAddress');
const selectionTargetSchema = ref('SelectionTarget');
const targetLocatorSchema = ref('TargetLocator');

Check warning on line 613 in packages/document-api/src/contract/schemas.ts

View workflow job for this annotation

GitHub Actions / build

'targetLocatorSchema' is assigned a value but never used. Allowed unused vars must match /^_/u
const deleteBehaviorSchema = ref('DeleteBehavior');
const resolvedHandleSchema = ref('ResolvedHandle');
const pageInfoSchema = ref('PageInfo');
Expand Down Expand Up @@ -885,7 +885,7 @@
text: { type: 'string' },
});

const nodeInfoSchema: JsonSchema = {

Check warning on line 888 in packages/document-api/src/contract/schemas.ts

View workflow job for this annotation

GitHub Actions / build

'nodeInfoSchema' is assigned a value but never used. Allowed unused vars must match /^_/u
type: 'object',
required: ['nodeType', 'kind'],
properties: {
Expand All @@ -901,7 +901,7 @@
additionalProperties: false,
};

const matchContextSchema = objectSchema(

Check warning on line 904 in packages/document-api/src/contract/schemas.ts

View workflow job for this annotation

GitHub Actions / build

'matchContextSchema' is assigned a value but never used. Allowed unused vars must match /^_/u
{
address: nodeAddressSchema,
snippet: { type: 'string' },
Expand All @@ -912,7 +912,7 @@
['address', 'snippet', 'highlightRange'],
);

const unknownNodeDiagnosticSchema = objectSchema(

Check warning on line 915 in packages/document-api/src/contract/schemas.ts

View workflow job for this annotation

GitHub Actions / build

'unknownNodeDiagnosticSchema' is assigned a value but never used. Allowed unused vars must match /^_/u
{
message: { type: 'string' },
address: nodeAddressSchema,
Expand Down Expand Up @@ -2190,6 +2190,7 @@
kind: { enum: ['block', 'inline'] },
controlType: { type: 'string' },
target: contentControlTargetSchema,
at: selectionTargetSchema,
tag: { type: 'string' },
alias: { type: 'string' },
lockMode: { enum: ['unlocked', 'sdtLocked', 'contentLocked', 'sdtContentLocked'] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

import { Fragment, type Node as ProseMirrorNode, type Schema } from 'prosemirror-model';
import { TextSelection } from 'prosemirror-state';
import type { Editor } from '../../core/Editor.js';
import type { ProseMirrorJSON } from '../../core/types/EditorTypes.js';
import type {
Expand Down Expand Up @@ -89,6 +90,7 @@ import type {
import { DocumentApiAdapterError } from '../errors.js';
import { executeDomainCommand } from './plan-wrappers.js';
import { clearIndexCache } from '../helpers/index-cache.js';
import { resolveSelectionTarget } from '../helpers/selection-target-resolver.js';

// Shared helpers — single source of truth for SDT logic
import {
Expand Down Expand Up @@ -1853,30 +1855,44 @@ function createWrapper(
return true;
}

// When a SelectionTarget is provided, set the editor selection to that
// range so insertStructuredContentInline/Block wraps the targeted text.
if (input.at) {
const { absFrom, absTo } = resolveSelectionTarget(editor, input.at);
const { tr } = editor.state;
tr.setSelection(TextSelection.create(tr.doc, absFrom, absTo));
dispatchTransaction(editor, tr);
}

// Re-acquire the command so it picks up fresh editor state — important
// when `at` dispatched a selection change above.
const cmd = editor.commands?.[commandName];
if (typeof cmd !== 'function') return false;

// Default: delegate to the editor command (inserts at current selection).
if (contentText !== undefined) {
if (input.kind === 'block') {
if (isCheckboxCreate) {
return Boolean(
insertCmd({
cmd({
attrs,
json: { type: 'paragraph', content: [buildCheckboxTextJson(checkboxSymbol)] },
}),
);
}
return Boolean(
insertCmd({
cmd({
attrs,
json: { type: 'paragraph', content: [{ type: 'text', text: contentText }] },
}),
);
}
if (isCheckboxCreate) {
return Boolean(insertCmd({ attrs, json: buildCheckboxTextJson(checkboxSymbol) }));
return Boolean(cmd({ attrs, json: buildCheckboxTextJson(checkboxSymbol) }));
}
return Boolean(insertCmd({ attrs, text: contentText }));
return Boolean(cmd({ attrs, text: contentText }));
}
return Boolean(insertCmd({ attrs }));
return Boolean(cmd({ attrs }));
});
}

Expand Down
Loading