Skip to content
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

Add splitListItemKeepMarks command #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/schema-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ export function splitListItem(itemType: NodeType): Command {
}
}

/// Acts like [`splitListItem`](#schema-list.splitListItem), but without
/// resetting the set of active marks at the cursor.
export function splitListItemKeepMarks(itemType: NodeType) {
return function(state: EditorState, dispatch?: (tr: Transaction) => void) {
const split = splitListItem(itemType)

return split(state, dispatch && (tr => {
let marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks())
if (marks) tr.ensureMarks(marks)
dispatch(tr)
}))
}
}

/// Create a command to lift the list item around the selection up into
/// a wrapping list.
export function liftListItem(itemType: NodeType): Command {
Expand Down
12 changes: 10 additions & 2 deletions test/test-commands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {EditorState, Selection, TextSelection, NodeSelection, Command} from "prosemirror-state"
import {schema, eq, doc, blockquote, p, li, ol, ul} from "prosemirror-test-builder"
import {schema, eq, doc, blockquote, p, li, ol, ul, strong} from "prosemirror-test-builder"
import ist from "ist"
import {wrapInList, splitListItem, liftListItem, sinkListItem} from "prosemirror-schema-list"
import {wrapInList, splitListItem, liftListItem, sinkListItem, splitListItemKeepMarks} from "prosemirror-schema-list"
import {Node} from "prosemirror-model"

function selFor(doc: Node) {
Expand Down Expand Up @@ -85,6 +85,14 @@ describe("splitListItem", () => {
doc(ul(li(p("one")), li(p("<a>")), li(p("two"))))))
})

describe("splitListItemKeepMarks", () => {
let splitWithMarks = splitListItemKeepMarks(schema.nodes.list_item)

it("preserves marks after splitting", () =>
apply(doc(ul(li(p(strong("foo<a>bar"))))), splitWithMarks,
doc(ul(li(p(strong("foo"))), li(p(strong("bar")))))))
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it doesn't actually test the preserving---it doesn't look at what the stored marks are after the command runs.


describe("liftListItem", () => {
let lift = liftListItem(schema.nodes.list_item)

Expand Down