Skip to content

Commit 98c661a

Browse files
committed
temp
1 parent 63d0173 commit 98c661a

File tree

10 files changed

+86
-74
lines changed

10 files changed

+86
-74
lines changed

src/components/text-editor/prosemirror-adapter/menu/menu-commands.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { toggleMark, setBlockType, wrapIn, lift } from 'prosemirror-commands';
2-
import { Schema, MarkType, NodeType, Attrs } from 'prosemirror-model';
2+
import {
3+
Schema,
4+
MarkType,
5+
NodeType,
6+
Attrs,
7+
ResolvedPos,
8+
} from 'prosemirror-model';
39
import { findWrapping, liftTarget } from 'prosemirror-transform';
410
import {
511
Command,
@@ -137,7 +143,7 @@ const createToggleMarkCommand = (
137143
const getAttributes = (
138144
markName: string,
139145
link: EditorTextLink,
140-
): Attrs | null => {
146+
): Attrs | undefined => {
141147
if (markName === EditorMenuTypes.Link && link.href) {
142148
return {
143149
href: link.href,
@@ -152,6 +158,13 @@ export const isExternalLink = (url: string): boolean => {
152158
return !url.startsWith(window.location.origin);
153159
};
154160

161+
const checkForActiveWrap = (
162+
$from: ResolvedPos,
163+
nodeType: NodeType,
164+
): boolean => {
165+
return $from.depth > 0 && $from.node($from.depth - 1).type === nodeType;
166+
};
167+
155168
/**
156169
* Toggles or wraps a node type based on the selection and parameters.
157170
* - Toggles to paragraph if the selection is of the specified type.
@@ -175,7 +188,7 @@ const toggleNodeType = (
175188
return (state, dispatch) => {
176189
const { $from, $to } = state.selection;
177190

178-
const hasActiveWrap = $from.node($from.depth - 1).type === nodeType;
191+
const hasActiveWrap = checkForActiveWrap($from, nodeType);
179192

180193
if (
181194
state.selection instanceof TextSelection &&

src/components/text-editor/test-setup/command-tester.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function getCommandResult(
3838
(tr) => {
3939
transaction = tr;
4040
},
41-
null,
41+
undefined,
4242
);
4343

4444
if (!commandResult) {
@@ -77,9 +77,7 @@ function verifyDocumentIncludes(
7777
if (Array.isArray(expectedContent)) {
7878
// Check that all strings in the array are contained in the doc content
7979
for (const content of expectedContent) {
80-
expect(doc.textContent).toContain(
81-
content,
82-
);
80+
expect(doc.textContent).toContain(content);
8381
}
8482
} else {
8583
expect(doc.textContent).toContain(expectedContent);
@@ -99,8 +97,8 @@ function verifyDocumentSize(
9997
/**
10098
* Verifies the content of the document against expected values
10199
*
102-
* @param state The editor state containing the document to verify
103-
* @param expected The expected values to verify against
100+
* @param state - The editor state containing the document to verify
101+
* @param expected - The expected values to verify against
104102
*/
105103
function verifyDocumentContent(
106104
state: EditorState,
@@ -114,7 +112,7 @@ function verifyDocumentContent(
114112
verifyExactDocumentContent(state.doc, expected.docContentAfter);
115113
}
116114

117-
if (expected.includesContent) {
115+
if ('includesContent' in expected) {
118116
verifyDocumentIncludes(state.doc, expected.includesContent);
119117
}
120118

@@ -126,13 +124,13 @@ function verifyDocumentContent(
126124
/**
127125
* Tests a ProseMirror command and verifies its result
128126
*
129-
* @param command The ProseMirror command to test
130-
* @param state The editor state to apply the command to
131-
* @param expected An object containing expected values to verify
132-
* @param expected.shouldApply Whether the command should be applicable to the state
133-
* @param expected.docContentAfter Optional expected document content after applying command
134-
* @param expected.docSizeAfter Optional expected document size after applying command
135-
* @param expected.includesContent Optional content to check if it exists in the document content
127+
* @param command - The ProseMirror command to test
128+
* @param state - The editor state to apply the command to
129+
* @param expected - An object containing expected values to verify:
130+
* - `shouldApply`: whether the command should be applicable
131+
* - `docContentAfter?`: expected document content after applying
132+
* - `docSizeAfter?`: expected document size after applying
133+
* = `includesContent?`: content that should exist in the document
136134
* @returns The result of applying the command for further assertions if needed
137135
*/
138136
export function testCommand(
@@ -164,9 +162,9 @@ export function testCommand(
164162
* @param command - The command to test
165163
* @param state - The editor state to apply the command to
166164
* @param expected - Expected results after command execution
167-
* @param expected.shouldApply - Whether the command should be applicable to the state
168-
* @param expected.docContentAfter - Optional expected document content after applying command
169-
* @param expected.docSizeAfter - Optional expected document size after applying command
165+
* - `shouldApply`: Whether the command should be applicable
166+
* - `docContentAfter?`: Expected document content after applying
167+
* - `docSizeAfter?`: Expected document size after applying
170168
* @returns An extended result containing the command result and the created view
171169
*/
172170
export function testCommandWithView(

src/components/text-editor/test-setup/content-generator.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ import { EditorState } from 'prosemirror-state';
1010
* @param schema - Optional schema to use (defaults to createTestSchema())
1111
* @returns An EditorState with the specified text content
1212
*/
13-
export function createDocWithText(text: string = '', schema?: Schema): EditorState {
13+
export function createDocWithText(
14+
text: string = '',
15+
schema?: Schema,
16+
): EditorState {
1417
const editorSchema = schema || createTestSchema();
1518
const content = text ? `<p>${text}</p>` : '<p></p>';
19+
1620
return createEditorState(content, editorSchema);
1721
}
1822

@@ -40,7 +44,7 @@ export function createDocWithHTML(html: string, schema?: Schema): EditorState {
4044
export function createDocWithFormattedText(
4145
text: string,
4246
marks: MarkSpec[],
43-
schema?: Schema
47+
schema?: Schema,
4448
): EditorState {
4549
const editorSchema = schema || createTestSchema();
4650

@@ -62,7 +66,7 @@ export function createDocWithFormattedText(
6266
function createTextNodeWithMarks(
6367
text: string,
6468
marks: MarkSpec[],
65-
schema: Schema
69+
schema: Schema,
6670
): Node {
6771
const appliedMarks: Mark[] = marks.map((markSpec) => {
6872
const markType = schema.marks[markSpec.type];
@@ -87,7 +91,7 @@ function createTextNodeWithMarks(
8791
*/
8892
export function createDocWithBulletList(
8993
items: string[],
90-
schema?: Schema
94+
schema?: Schema,
9195
): EditorState {
9296
const editorSchema = schema || createTestSchema();
9397

@@ -100,7 +104,7 @@ export function createDocWithBulletList(
100104

101105
const bulletList = editorSchema.nodes.bullet_list.create(
102106
null,
103-
Fragment.from(listItems)
107+
Fragment.from(listItems),
104108
);
105109

106110
const doc = editorSchema.nodes.doc.createAndFill(null, bulletList);
@@ -119,14 +123,14 @@ export function createDocWithBulletList(
119123
export function createDocWithHeading(
120124
text: string,
121125
level: number = 1,
122-
schema?: Schema
126+
schema?: Schema,
123127
): EditorState {
124128
const editorSchema = schema || createTestSchema();
125129

126130
const textNode = editorSchema.text(text);
127131
const heading = editorSchema.nodes.heading.create(
128132
{ level: level },
129-
textNode
133+
textNode,
130134
);
131135

132136
const doc = editorSchema.nodes.doc.createAndFill(null, heading);
@@ -143,7 +147,7 @@ export function createDocWithHeading(
143147
*/
144148
export function createDocWithBlockquote(
145149
text: string,
146-
schema?: Schema
150+
schema?: Schema,
147151
): EditorState {
148152
const editorSchema = schema || createTestSchema();
149153

@@ -165,7 +169,7 @@ export function createDocWithBlockquote(
165169
*/
166170
export function createDocWithCodeBlock(
167171
code: string,
168-
schema?: Schema
172+
schema?: Schema,
169173
): EditorState {
170174
const editorSchema = schema || createTestSchema();
171175

src/components/text-editor/test-setup/editor-state-builder.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('Editor State Utilities', () => {
3636

3737
if (node) {
3838
const hasStrongMark = node.marks.some(
39-
(m) => m.type.name === 'strong'
39+
(m) => m.type.name === 'strong',
4040
);
4141
expect(hasStrongMark).toBe(true);
4242
}

src/components/text-editor/test-setup/editor-state-builder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { createTestSchema } from './schema-builder';
1414
export function createEditorState(
1515
content?: string,
1616
schema?: Schema,
17-
plugins: Plugin[] = []
17+
plugins: Plugin[] = [],
1818
): EditorState {
1919
const editorSchema = schema || createTestSchema();
2020

@@ -43,7 +43,7 @@ export function createEditorStateWithSelection(
4343
from: number,
4444
to: number,
4545
schema?: Schema,
46-
plugins: Plugin[] = []
46+
plugins: Plugin[] = [],
4747
): EditorState {
4848
const editorSchema = schema || createTestSchema();
4949

@@ -69,7 +69,7 @@ export function createEditorStateWithSelection(
6969
export function setTextSelection(
7070
state: EditorState,
7171
from: number,
72-
to: number = from
72+
to: number = from,
7373
): EditorState {
7474
const selection = TextSelection.create(state.doc, from, to);
7575

src/components/text-editor/test-setup/editor-view-builder.spec.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe('Editor View Utilities', () => {
140140
const result = createEditorView(
141141
undefined,
142142
undefined,
143-
customContainer
143+
customContainer,
144144
);
145145
view = result.view;
146146
container = result.container;
@@ -173,9 +173,6 @@ describe('Editor View Utilities', () => {
173173
});
174174

175175
it('should create mock DOM if none exists', () => {
176-
const originalWindow = global.window;
177-
const originalDocument = global.document;
178-
179176
try {
180177
global.window = undefined;
181178
global.document = undefined;
@@ -197,8 +194,6 @@ describe('Editor View Utilities', () => {
197194
});
198195

199196
it('should not modify existing DOM environment', () => {
200-
const originalWindow = global.window;
201-
const originalDocument = global.document;
202197
let cleanup: () => void;
203198

204199
try {
@@ -216,6 +211,7 @@ describe('Editor View Utilities', () => {
216211
if (cleanup) {
217212
cleanup();
218213
}
214+
219215
global.window = originalWindow;
220216
global.document = originalDocument;
221217
}

src/components/text-editor/test-setup/editor-view-builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { createTestSchema } from './schema-builder';
1414
export function createEditorView(
1515
state?: EditorState,
1616
dispatchSpy?: jest.Mock,
17-
parentElement?: HTMLElement
17+
parentElement?: HTMLElement,
1818
): { view: EditorView; container: HTMLElement } {
1919
const container = parentElement || document.createElement('div');
2020
if (!parentElement) {
@@ -77,7 +77,7 @@ export function createDispatchSpy(autoUpdate = true): jest.Mock {
7777
*/
7878
export function cleanupEditorView(
7979
view: EditorView,
80-
container?: HTMLElement
80+
container?: HTMLElement,
8181
): void {
8282
view.destroy();
8383

src/components/text-editor/test-setup/event-simulator.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('Event Simulation Utilities', () => {
101101
const event = dispatchEventSpy.mock.calls[0][0] as any;
102102
const clipboardData = event.clipboardData;
103103
expect(clipboardData.getData('text/html')).toBe(
104-
'<p>Formatted <strong>content</strong></p>'
104+
'<p>Formatted <strong>content</strong></p>',
105105
);
106106

107107
dispatchEventSpy.mockRestore();
@@ -148,7 +148,7 @@ describe('Event Simulation Utilities', () => {
148148
expect(dispatchEventSpy).toHaveBeenCalledTimes(5);
149149

150150
const eventTypes = dispatchEventSpy.mock.calls.map(
151-
(call) => call[0].type
151+
(call) => call[0].type,
152152
);
153153
expect(eventTypes).toEqual([
154154
'mousedown',
@@ -184,7 +184,7 @@ describe('Event Simulation Utilities', () => {
184184
'Dragged text',
185185
);
186186
expect(dragstart.dataTransfer.getData('text/html')).toBe(
187-
'<p>Dragged HTML</p>'
187+
'<p>Dragged HTML</p>',
188188
);
189189

190190
dispatchEventSpy.mockRestore();

0 commit comments

Comments
 (0)