Skip to content

feat(List): rename me #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
212 changes: 180 additions & 32 deletions src/extensions/markdown/Lists/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import ist from 'ist';
import type {Node} from 'prosemirror-model';
import {Schema} from 'prosemirror-model';
import {builders, li, ul} from 'prosemirror-test-builder';

import {
type Command,
EditorState,
NodeSelection,
Selection,
TextSelection,
} from 'prosemirror-state';
import {doc, eq, li, p, schema, ul} from 'prosemirror-test-builder';

import {sinkOnlySelectedListItem} from 'src/extensions/markdown/Lists/commands';

function selFor(doc: Node) {
const a = (doc as any).tag.a,
b = (doc as any).tag.b;
if (a !== null) {
const $a = doc.resolve(a);
if ($a.parent.inlineContent)
return new TextSelection($a, b !== null ? doc.resolve(b) : undefined);
else return new NodeSelection($a);
}
return Selection.atStart(doc);
}

function apply(doc: Node, command: Command, result: Node | null) {
let state = EditorState.create({doc, selection: selFor(doc)});
// eslint-disable-next-line no-return-assign
command(state, (tr) => (state = state.apply(tr)));
ist(state.doc, result || doc, eq);
// eslint-disable-next-line no-eq-null
if (result && (result as any).tag.a != null) ist(state.selection, selFor(result), eq);
}
getListItemsToTransform,
sinkOnlySelectedListItem,
} from 'src/extensions/markdown/Lists/commands';
import {apply, assertMapEntries, getParams} from 'src/extensions/markdown/Lists/testUtils';
import {getSchemaSpecs as getYfmNoteSchemaSpecs} from 'src/extensions/yfm/YfmNote/YfmNoteSpecs/schema';

const schema = new Schema({
nodes: {
doc: {content: 'block+'},
text: {group: 'inline'},
paragraph: {
group: 'block',
content: 'inline*',
toDOM: () => ['p', 0],
},
...getYfmNoteSchemaSpecs(),
},
});

const {
doc,
paragraph: p,
yfm_note: note,
yfm_note_title: noteTitle,
yfm_note_content: noteContent,
} = builders(schema);

describe('sinkOnlySelectedListItem', () => {
const sink = sinkOnlySelectedListItem(schema.nodes.list_item);
Expand Down Expand Up @@ -232,4 +229,155 @@ describe('sinkOnlySelectedListItem', () => {
),
),
));
it('sinks nested list items with a reverse staircase selection from outdented item to indented one', () =>
apply(
doc(
ul(
li(p('aa')),
li(
p('bb'),
ul(
li(p('cc')),
li(p('dd'), ul(li(p('ee')), li(p('s<a>s')))),
li(p('z<b>z')),
li(p('ww')),
),
),
li(p('pp')),
li(p('hh')),
),
),
sink,
doc(
ul(
li(p('aa')),
li(
p('bb'),
ul(
li(p('cc')),
li(p('dd'), ul(li(p('ee'), ul(li(p('ss')))), li(p('zz')))),
li(p('ww')),
),
),
li(p('pp')),
li(p('hh')),
),
),
));

it('removes selection markers without changing list structure for first item', () =>
apply(
doc(ul(li(p('1<a><b>1')), li(p('22')), li(p('33')))),
sink,
doc(ul(li(p('11')), li(p('22')), li(p('33')))),
));

it('indents the second item into a sublist when selected', () =>
apply(
doc(ul(li(p('11')), li(p('2<a><b>2')), li(p('33')))),
sink,
doc(ul(li(p('11'), ul(li(p('22')))), li(p('33')))),
));

it('indents only the selected item when selection spans two items', () =>
apply(
doc(ul(li(p('11')), li(p('2<a>2')), li(p('3<b>3')))),
sink,
doc(ul(li(p('11'), ul(li(p('22')))), li(p('33')))),
));
});

describe('getListItemsToTransform (using getParams helper)', () => {
it('1', () => {
const testDoc = doc(ul(li(p('11')), li(p('2<a><b>2')), li(p('33'))));

const resultMap = getListItemsToTransform(...getParams(testDoc));

assertMapEntries(resultMap, [[7, 13]]);
});

it('2', () => {
const testDoc = doc(ul(li(p('11')), li(p('2<a>2')), li(p('3<b>3'))));

const resultMap = getListItemsToTransform(...getParams(testDoc));

assertMapEntries(resultMap, [
[7, 13],
[13, 19],
]);
});

it('3', () => {
const testDoc = doc(ul(li(p('11')), li(p('2<a>2'), ul(li(p('3<b>3'))))));
const resultMap = getListItemsToTransform(...getParams(testDoc));

assertMapEntries(resultMap, [
[7, 21],
[13, 19],
]);
});

it('4', () => {
const testDoc = doc(ul(li(p('11')), li(p('2<a>2'), ul(li(p('33')))), li(p('4<b>4'))));
const resultMap = getListItemsToTransform(...getParams(testDoc));

assertMapEntries(resultMap, [
[7, 21],
[13, 19],
[21, 27],
]);
});

it('5', () => {
const testDoc = doc(ul(li(p('11'), ul(li(p('22')), li(p('3<a>3')))), li(p('4<b>4'))));
const resultMap = getListItemsToTransform(...getParams(testDoc));

assertMapEntries(resultMap, [
[13, 19],
[21, 27],
]);
});

it('6', () => {
const testDoc = doc(
ul(
li(p('11')),
li(p('2<a>2'), ul(li(p('33'), ul(li(p('44')))), li(p('55')))),
li(p('6<b>6')),
),
);

const resultMap = getListItemsToTransform(...getParams(testDoc));

assertMapEntries(resultMap, [
[7, 35],
[13, 27],
[19, 25],
[27, 33],
[35, 41],
]);
});

it('7', () => {
const testDoc = doc(
ul(
li(p('11')),
li(
p('2<a>2'),
note(noteTitle('Note'), noteContent(ul(li(p('33')), li(p('44')), li(p('55'))))),
),
li(p('6<b>6')),
),
);

const resultMap = getListItemsToTransform(...getParams(testDoc));

assertMapEntries(resultMap, [
[7, 43],
[43, 49],
[21, 27],
[27, 33],
[33, 39],
]);
});
});
Loading
Loading