prosemirror-schema-list
Advanced tools
Comparing version 0.22.0 to 0.23.0
@@ -16,3 +16,3 @@ # How to contribute | ||
[GitHub issue tracker](http://github.com/prosemirror/prosemirror/issues). | ||
Before reporting a bug, read these pointers. | ||
Before reporting a bug, please read these pointers. | ||
@@ -26,6 +26,7 @@ - The issue tracker is for *bugs*, not requests for help. Questions | ||
- Mention very precisely what went wrong. "X is broken" is not a good bug | ||
report. What did you expect to happen? What happened instead? Describe the | ||
exact steps a maintainer has to take to make the problem occur. We can not | ||
fix something that we can not observe. | ||
- Mention very precisely what went wrong. "X is broken" is not a good | ||
bug report. What did you expect to happen? What happened instead? | ||
Describe the exact steps a maintainer has to take to make the | ||
problem occur. A screencast can be useful, but is no substitute for | ||
a textual description. | ||
@@ -50,4 +51,4 @@ - A great way to make it easy to reproduce your problem, if it can not | ||
- Follow the code style of the rest of the project (see below). Run | ||
`npm run lint` (in the main repository checkout) that the linter is | ||
happy. | ||
`npm run lint` (in the main repository checkout) to make sure that | ||
the linter is happy. | ||
@@ -54,0 +55,0 @@ - If your changes are easy to test or likely to regress, add tests in |
@@ -1,15 +0,13 @@ | ||
var ref = require("prosemirror-transform"); | ||
var findWrapping = ref.findWrapping; | ||
var liftTarget = ref.liftTarget; | ||
var canSplit = ref.canSplit; | ||
var ReplaceAroundStep = ref.ReplaceAroundStep; | ||
var ref$1 = require("prosemirror-model"); | ||
var Slice = ref$1.Slice; | ||
var Fragment = ref$1.Fragment; | ||
var NodeRange = ref$1.NodeRange; | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var prosemirrorTransform = require('prosemirror-transform'); | ||
var prosemirrorModel = require('prosemirror-model'); | ||
// :: NodeSpec | ||
// An ordered list node type spec. Has a single attribute, `order`, | ||
// which determines the number at which the list starts counting, and | ||
// defaults to 1. | ||
// An ordered list [node spec](#model.NodeSpec). Has a single | ||
// attribute, `order`, which determines the number at which the list | ||
// starts counting, and defaults to 1. Represented as an `<ol>` | ||
// element. | ||
var orderedList = { | ||
@@ -23,15 +21,13 @@ attrs: {order: {default: 1}}, | ||
} | ||
} | ||
exports.orderedList = orderedList | ||
}; | ||
// :: NodeSpec | ||
// A bullet list node spec. | ||
// A bullet list node spec, represented in the DOM as `<ul>`. | ||
var bulletList = { | ||
parseDOM: [{tag: "ul"}], | ||
toDOM: function toDOM() { return ["ul", 0] } | ||
} | ||
exports.bulletList = bulletList | ||
}; | ||
// :: NodeSpec | ||
// A list item node spec. | ||
// A list item (`<li>`) spec. | ||
var listItem = { | ||
@@ -41,9 +37,8 @@ parseDOM: [{tag: "li"}], | ||
defining: true | ||
} | ||
exports.listItem = listItem | ||
}; | ||
function add(obj, props) { | ||
var copy = {} | ||
for (var prop in obj) { copy[prop] = obj[prop] } | ||
for (var prop$1 in props) { copy[prop$1] = props[prop$1] } | ||
var copy = {}; | ||
for (var prop in obj) { copy[prop] = obj[prop]; } | ||
for (var prop$1 in props) { copy[prop$1] = props[prop$1]; } | ||
return copy | ||
@@ -54,10 +49,13 @@ } | ||
// Convenience function for adding list-related node types to a map | ||
// describing the nodes in a schema. Adds `OrderedList` as | ||
// `"ordered_list"`, `BulletList` as `"bullet_list"`, and `ListItem` | ||
// as `"list_item"`. `itemContent` determines the content expression | ||
// for the list items. If you want the commands defined in this module | ||
// to apply to your list structure, it should have a shape like | ||
// `"paragraph block*"`, a plain textblock type followed by zero or | ||
// more arbitrary nodes. `listGroup` can be given to assign a group | ||
// name to the list node types, for example `"block"`. | ||
// specifying the nodes for a schema. Adds | ||
// [`orderedList`](#schema-list.orderedList) as `"ordered_list"`, | ||
// [`bulletList`](#schema-list.bulletList) as `"bullet_list"`, and | ||
// [`listItem`](#schema-list.listItem) as `"list_item"`. | ||
// | ||
// `itemContent` determines the content expression for the list items. | ||
// If you want the commands defined in this module to apply to your | ||
// list structure, it should have a shape like `"paragraph block*"` or | ||
// `"paragraph (ordered_list | bullet_list)*"`. `listGroup` can be | ||
// given to assign a group name to the list node types, for example | ||
// `"block"`. | ||
function addListNodes(nodes, itemContent, listGroup) { | ||
@@ -70,7 +68,6 @@ return nodes.append({ | ||
} | ||
exports.addListNodes = addListNodes | ||
// :: (NodeType, ?Object) → (state: EditorState, dispatch: ?(tr: Transaction)) → bool | ||
// Returns a command function that wraps the selection in a list with | ||
// the given type an attributes. If `apply` is `false`, only return a | ||
// the given type an attributes. If `dispatch` is null, only return a | ||
// value to indicate whether this is possible, but don't actually | ||
@@ -83,3 +80,3 @@ // perform the change. | ||
var $to = ref.$to; | ||
var range = $from.blockRange($to), doJoin = false, outerRange = range | ||
var range = $from.blockRange($to), doJoin = false, outerRange = range; | ||
if (!range) { return false } | ||
@@ -90,32 +87,31 @@ // This is at the top of an existing list item | ||
if ($from.index(range.depth - 1) == 0) { return false } | ||
var $insert = state.doc.resolve(range.start - 2) | ||
outerRange = new NodeRange($insert, $insert, range.depth) | ||
var $insert = state.doc.resolve(range.start - 2); | ||
outerRange = new prosemirrorModel.NodeRange($insert, $insert, range.depth); | ||
if (range.endIndex < range.parent.childCount) | ||
{ range = new NodeRange($from, state.doc.resolve($to.end(range.depth)), range.depth) } | ||
doJoin = true | ||
{ range = new prosemirrorModel.NodeRange($from, state.doc.resolve($to.end(range.depth)), range.depth); } | ||
doJoin = true; | ||
} | ||
var wrap = findWrapping(outerRange, listType, attrs, range) | ||
var wrap = prosemirrorTransform.findWrapping(outerRange, listType, attrs, range); | ||
if (!wrap) { return false } | ||
if (dispatch) { dispatch(doWrapInList(state.tr, range, wrap, doJoin, listType).scrollIntoView()) } | ||
if (dispatch) { dispatch(doWrapInList(state.tr, range, wrap, doJoin, listType).scrollIntoView()); } | ||
return true | ||
} | ||
} | ||
exports.wrapInList = wrapInList | ||
function doWrapInList(tr, range, wrappers, joinBefore, listType) { | ||
var content = Fragment.empty | ||
var content = prosemirrorModel.Fragment.empty; | ||
for (var i = wrappers.length - 1; i >= 0; i--) | ||
{ content = Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content)) } | ||
{ content = prosemirrorModel.Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content)); } | ||
tr.step(new ReplaceAroundStep(range.start - (joinBefore ? 2 : 0), range.end, range.start, range.end, | ||
new Slice(content, 0, 0), wrappers.length, true)) | ||
tr.step(new prosemirrorTransform.ReplaceAroundStep(range.start - (joinBefore ? 2 : 0), range.end, range.start, range.end, | ||
new prosemirrorModel.Slice(content, 0, 0), wrappers.length, true)); | ||
var found = 0 | ||
for (var i$1 = 0; i$1 < wrappers.length; i$1++) { if (wrappers[i$1].type == listType) { found = i$1 + 1 } } | ||
var splitDepth = wrappers.length - found | ||
var found = 0; | ||
for (var i$1 = 0; i$1 < wrappers.length; i$1++) { if (wrappers[i$1].type == listType) { found = i$1 + 1; } } | ||
var splitDepth = wrappers.length - found; | ||
var splitPos = range.start + wrappers.length - (joinBefore ? 2 : 0), parent = range.parent | ||
var splitPos = range.start + wrappers.length - (joinBefore ? 2 : 0), parent = range.parent; | ||
for (var i$2 = range.startIndex, e = range.endIndex, first = true; i$2 < e; i$2++, first = false) { | ||
if (!first && canSplit(tr.doc, splitPos, splitDepth)) { tr.split(splitPos, splitDepth) } | ||
splitPos += parent.child(i$2).nodeSize + (first ? 0 : 2 * splitDepth) | ||
if (!first && prosemirrorTransform.canSplit(tr.doc, splitPos, splitDepth)) { tr.split(splitPos, splitDepth); } | ||
splitPos += parent.child(i$2).nodeSize + (first ? 0 : 2 * splitDepth); | ||
} | ||
@@ -134,15 +130,33 @@ return tr | ||
var node = ref.node; | ||
if ((node && node.isBlock) || !$from.parent.content.size || | ||
$from.depth < 2 || !$from.sameParent($to)) { return false } | ||
var grandParent = $from.node(-1) | ||
if ((node && node.isBlock) || $from.depth < 2 || !$from.sameParent($to)) { return false } | ||
var grandParent = $from.node(-1); | ||
if (grandParent.type != itemType) { return false } | ||
var nextType = $to.pos == $from.end() ? grandParent.defaultContentType(0) : null | ||
var tr = state.tr.delete($from.pos, $to.pos) | ||
var types = nextType && [null, {type: nextType}] | ||
if (!canSplit(tr.doc, $from.pos, 2, types)) { return false } | ||
if (dispatch) { dispatch(tr.split($from.pos, 2, types).scrollIntoView()) } | ||
if ($from.parent.content.size == 0) { | ||
// In an empty block. If this is a nested list, the wrapping | ||
// list item should be split. Otherwise, bail out and let next | ||
// command handle lifting. | ||
if ($from.depth == 2 || $from.node(-3).type != itemType || | ||
$from.index(-2) != $from.node(-2).childCount - 1) { return false } | ||
if (dispatch) { | ||
var wrap = prosemirrorModel.Fragment.empty, keepItem = $from.index(-1) > 0; | ||
// Build a fragment containing empty versions of the structure | ||
// from the outer list item to the parent node of the cursor | ||
for (var d = $from.depth - (keepItem ? 1 : 2); d >= $from.depth - 3; d--) | ||
{ wrap = prosemirrorModel.Fragment.from($from.node(d).copy(wrap)); } | ||
// Add a second list item with an empty default start node | ||
wrap = wrap.append(prosemirrorModel.Fragment.from(itemType.createAndFill())); | ||
var tr$1 = state.tr.replace($from.before(keepItem ? null : -1), $from.after(-3), new prosemirrorModel.Slice(wrap, keepItem ? 3 : 2, 2)); | ||
tr$1.setSelection(state.selection.constructor.near(tr$1.doc.resolve($from.pos + (keepItem ? 3 : 2)))); | ||
dispatch(tr$1.scrollIntoView()); | ||
} | ||
return true | ||
} | ||
var nextType = $to.pos == $from.end() ? grandParent.defaultContentType(0) : null; | ||
var tr = state.tr.delete($from.pos, $to.pos); | ||
var types = nextType && [null, {type: nextType}]; | ||
if (!prosemirrorTransform.canSplit(tr.doc, $from.pos, 2, types)) { return false } | ||
if (dispatch) { dispatch(tr.split($from.pos, 2, types).scrollIntoView()); } | ||
return true | ||
} | ||
} | ||
exports.splitListItem = splitListItem | ||
@@ -157,3 +171,3 @@ // :: (NodeType) → (state: EditorState, dispatch: ?(tr: Transaction)) → bool | ||
var $to = ref.$to; | ||
var range = $from.blockRange($to, function (node) { return node.childCount && node.firstChild.type == itemType; }) | ||
var range = $from.blockRange($to, function (node) { return node.childCount && node.firstChild.type == itemType; }); | ||
if (!range) { return false } | ||
@@ -167,14 +181,13 @@ if (!dispatch) { return true } | ||
} | ||
exports.liftListItem = liftListItem | ||
function liftToOuterList(state, dispatch, itemType, range) { | ||
var tr = state.tr, end = range.end, endOfList = range.$to.end(range.depth) | ||
var tr = state.tr, end = range.end, endOfList = range.$to.end(range.depth); | ||
if (end < endOfList) { | ||
// There are siblings after the lifted items, which must become | ||
// children of the last item | ||
tr.step(new ReplaceAroundStep(end - 1, endOfList, end, endOfList, | ||
new Slice(Fragment.from(itemType.create(null, range.parent.copy())), 1, 0), 1, true)) | ||
range = new NodeRange(tr.doc.resolveNoCache(range.$from.pos), tr.doc.resolveNoCache(endOfList), range.depth) | ||
tr.step(new prosemirrorTransform.ReplaceAroundStep(end - 1, endOfList, end, endOfList, | ||
new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(itemType.create(null, range.parent.copy())), 1, 0), 1, true)); | ||
range = new prosemirrorModel.NodeRange(tr.doc.resolveNoCache(range.$from.pos), tr.doc.resolveNoCache(endOfList), range.depth); | ||
} | ||
dispatch(tr.lift(range, liftTarget(range)).scrollIntoView()) | ||
dispatch(tr.lift(range, prosemirrorTransform.liftTarget(range)).scrollIntoView()); | ||
return true | ||
@@ -184,23 +197,23 @@ } | ||
function liftOutOfList(state, dispatch, range) { | ||
var tr = state.tr, list = range.parent | ||
var tr = state.tr, list = range.parent; | ||
// Merge the list items into a single big item | ||
for (var pos = range.end, i = range.endIndex - 1, e = range.startIndex; i > e; i--) { | ||
pos -= list.child(i).nodeSize | ||
tr.delete(pos - 1, pos + 1) | ||
pos -= list.child(i).nodeSize; | ||
tr.delete(pos - 1, pos + 1); | ||
} | ||
var $start = tr.doc.resolve(range.start), item = $start.nodeAfter | ||
var atStart = range.startIndex == 0, atEnd = range.endIndex == list.childCount | ||
var parent = $start.node(-1), indexBefore = $start.index(-1) | ||
var $start = tr.doc.resolve(range.start), item = $start.nodeAfter; | ||
var atStart = range.startIndex == 0, atEnd = range.endIndex == list.childCount; | ||
var parent = $start.node(-1), indexBefore = $start.index(-1); | ||
if (!parent.canReplace(indexBefore + (atStart ? 0 : 1), indexBefore + 1, | ||
item.content.append(atEnd ? Fragment.empty : Fragment.from(list)))) | ||
item.content.append(atEnd ? prosemirrorModel.Fragment.empty : prosemirrorModel.Fragment.from(list)))) | ||
{ return false } | ||
var start = $start.pos, end = start + item.nodeSize | ||
var start = $start.pos, end = start + item.nodeSize; | ||
// Strip off the surrounding list. At the sides where we're not at | ||
// the end of the list, the existing list is closed. At sides where | ||
// this is the end, it is overwritten to its end. | ||
tr.step(new ReplaceAroundStep(start - (atStart ? 1 : 0), end + (atEnd ? 1 : 0), start + 1, end - 1, | ||
new Slice((atStart ? Fragment.empty : Fragment.from(list.copy(Fragment.empty))) | ||
.append(atEnd ? Fragment.empty : Fragment.from(list.copy(Fragment.empty))), | ||
atStart ? 0 : 1, atEnd ? 0 : 1), atStart ? 0 : 1)) | ||
dispatch(tr.scrollIntoView()) | ||
tr.step(new prosemirrorTransform.ReplaceAroundStep(start - (atStart ? 1 : 0), end + (atEnd ? 1 : 0), start + 1, end - 1, | ||
new prosemirrorModel.Slice((atStart ? prosemirrorModel.Fragment.empty : prosemirrorModel.Fragment.from(list.copy(prosemirrorModel.Fragment.empty))) | ||
.append(atEnd ? prosemirrorModel.Fragment.empty : prosemirrorModel.Fragment.from(list.copy(prosemirrorModel.Fragment.empty))), | ||
atStart ? 0 : 1, atEnd ? 0 : 1), atStart ? 0 : 1)); | ||
dispatch(tr.scrollIntoView()); | ||
return true | ||
@@ -217,18 +230,18 @@ } | ||
var $to = ref.$to; | ||
var range = $from.blockRange($to, function (node) { return node.childCount && node.firstChild.type == itemType; }) | ||
var range = $from.blockRange($to, function (node) { return node.childCount && node.firstChild.type == itemType; }); | ||
if (!range) { return false } | ||
var startIndex = range.startIndex | ||
var startIndex = range.startIndex; | ||
if (startIndex == 0) { return false } | ||
var parent = range.parent, nodeBefore = parent.child(startIndex - 1) | ||
var parent = range.parent, nodeBefore = parent.child(startIndex - 1); | ||
if (nodeBefore.type != itemType) { return false } | ||
if (dispatch) { | ||
var nestedBefore = nodeBefore.lastChild && nodeBefore.lastChild.type == parent.type | ||
var inner = Fragment.from(nestedBefore ? itemType.create() : null) | ||
var slice = new Slice(Fragment.from(itemType.create(null, Fragment.from(parent.copy(inner)))), | ||
nestedBefore ? 3 : 1, 0) | ||
var before = range.start, after = range.end | ||
dispatch(state.tr.step(new ReplaceAroundStep(before - (nestedBefore ? 3 : 1), after, | ||
var nestedBefore = nodeBefore.lastChild && nodeBefore.lastChild.type == parent.type; | ||
var inner = prosemirrorModel.Fragment.from(nestedBefore ? itemType.create() : null); | ||
var slice = new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(itemType.create(null, prosemirrorModel.Fragment.from(parent.copy(inner)))), | ||
nestedBefore ? 3 : 1, 0); | ||
var before = range.start, after = range.end; | ||
dispatch(state.tr.step(new prosemirrorTransform.ReplaceAroundStep(before - (nestedBefore ? 3 : 1), after, | ||
before, after, slice, 1, true)) | ||
.scrollIntoView()) | ||
.scrollIntoView()); | ||
} | ||
@@ -238,2 +251,11 @@ return true | ||
} | ||
exports.sinkListItem = sinkListItem | ||
exports.orderedList = orderedList; | ||
exports.bulletList = bulletList; | ||
exports.listItem = listItem; | ||
exports.addListNodes = addListNodes; | ||
exports.wrapInList = wrapInList; | ||
exports.splitListItem = splitListItem; | ||
exports.liftListItem = liftListItem; | ||
exports.sinkListItem = sinkListItem; | ||
//# sourceMappingURL=schema-list.js.map |
{ | ||
"name": "prosemirror-schema-list", | ||
"version": "0.22.0", | ||
"version": "0.23.0", | ||
"description": "List-related schema elements and commands for ProseMirror", | ||
@@ -19,18 +19,19 @@ "main": "dist/schema-list.js", | ||
"dependencies": { | ||
"prosemirror-model": "^0.22.0", | ||
"prosemirror-transform": "^0.22.0" | ||
"prosemirror-model": "^0.23.0", | ||
"prosemirror-transform": "^0.23.0" | ||
}, | ||
"devDependencies": { | ||
"buble": "^0.15.1", | ||
"prosemirror-state": "^0.22.0", | ||
"ist": "^1.0.0", | ||
"mocha": "^3.0.2", | ||
"rimraf": "^2.5.4", | ||
"prosemirror-test-builder": "^0.22.0" | ||
"prosemirror-state": "^0.23.0", | ||
"prosemirror-test-builder": "^0.23.0", | ||
"rollup": "^0.49.0", | ||
"rollup-plugin-buble": "^0.15.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha test/test-*.js", | ||
"build": "rimraf dist && buble -i src -o dist", | ||
"prepublish": "npm run build" | ||
"build": "rollup -c", | ||
"watch": "rollup -c -w", | ||
"prepare": "npm run build" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # prosemirror-schema-list | ||
This [module](http://prosemirror.net/ref.html#schema-list) exports | ||
This [module](http://prosemirror.net/docs/ref/#schema-list) exports | ||
schema elements and commands for including lists in a ProseMirror | ||
@@ -15,9 +15,5 @@ editor. | ||
The [project page](http://prosemirror.net) has more information, a | ||
number of [demos](http://prosemirror.net/#demos) and the | ||
[documentation](http://prosemirror.net/docs.html). | ||
number of [examples](http://prosemirror.net/examples/) and the | ||
[documentation](http://prosemirror.net/docs/). | ||
**NOTE:** This project is in *BETA* stage. It isn't thoroughly tested, | ||
and the API might still change across `0.x` releases. You are welcome | ||
to use it, but don't expect it to be very stable yet. | ||
This code is released under an | ||
@@ -24,0 +20,0 @@ [MIT license](https://github.com/prosemirror/prosemirror/tree/master/LICENSE). |
This module exports list-related schema elements and commands. The | ||
commands assume which assume lists to be nestable, but with the | ||
restriction that the first child of a list item is a plain paragraph. | ||
commands assume lists to be nestable, with the restriction that the | ||
first child of a list item is a plain paragraph. | ||
@@ -11,4 +11,2 @@ These are the node specs: | ||
You can extend a schema with this helper function. | ||
@addListNodes | ||
@@ -18,8 +16,10 @@ | ||
const mySchema = new Schema({ | ||
nodes: addListNodes(baseSchema.nodeSpec, "paragraph block*", "block"), | ||
marks: baseSchema.markSpec | ||
}) | ||
```javascript | ||
const mySchema = new Schema({ | ||
nodes: addListNodes(baseSchema.spec.nodes, "paragraph block*", "block"), | ||
marks: baseSchema.spec.marks | ||
}) | ||
``` | ||
The following functions are [commands](#commands): | ||
The following functions are [commands](/docs/guide/#commands): | ||
@@ -26,0 +26,0 @@ @wrapInList |
@@ -1,9 +0,10 @@ | ||
const {findWrapping, liftTarget, canSplit, ReplaceAroundStep} = require("prosemirror-transform") | ||
const {Slice, Fragment, NodeRange} = require("prosemirror-model") | ||
import {findWrapping, liftTarget, canSplit, ReplaceAroundStep} from "prosemirror-transform" | ||
import {Slice, Fragment, NodeRange} from "prosemirror-model" | ||
// :: NodeSpec | ||
// An ordered list node type spec. Has a single attribute, `order`, | ||
// which determines the number at which the list starts counting, and | ||
// defaults to 1. | ||
const orderedList = { | ||
// An ordered list [node spec](#model.NodeSpec). Has a single | ||
// attribute, `order`, which determines the number at which the list | ||
// starts counting, and defaults to 1. Represented as an `<ol>` | ||
// element. | ||
export const orderedList = { | ||
attrs: {order: {default: 1}}, | ||
@@ -17,15 +18,13 @@ parseDOM: [{tag: "ol", getAttrs(dom) { | ||
} | ||
exports.orderedList = orderedList | ||
// :: NodeSpec | ||
// A bullet list node spec. | ||
const bulletList = { | ||
// A bullet list node spec, represented in the DOM as `<ul>`. | ||
export const bulletList = { | ||
parseDOM: [{tag: "ul"}], | ||
toDOM() { return ["ul", 0] } | ||
} | ||
exports.bulletList = bulletList | ||
// :: NodeSpec | ||
// A list item node spec. | ||
const listItem = { | ||
// A list item (`<li>`) spec. | ||
export const listItem = { | ||
parseDOM: [{tag: "li"}], | ||
@@ -35,3 +34,2 @@ toDOM() { return ["li", 0] }, | ||
} | ||
exports.listItem = listItem | ||
@@ -47,11 +45,14 @@ function add(obj, props) { | ||
// Convenience function for adding list-related node types to a map | ||
// describing the nodes in a schema. Adds `OrderedList` as | ||
// `"ordered_list"`, `BulletList` as `"bullet_list"`, and `ListItem` | ||
// as `"list_item"`. `itemContent` determines the content expression | ||
// for the list items. If you want the commands defined in this module | ||
// to apply to your list structure, it should have a shape like | ||
// `"paragraph block*"`, a plain textblock type followed by zero or | ||
// more arbitrary nodes. `listGroup` can be given to assign a group | ||
// name to the list node types, for example `"block"`. | ||
function addListNodes(nodes, itemContent, listGroup) { | ||
// specifying the nodes for a schema. Adds | ||
// [`orderedList`](#schema-list.orderedList) as `"ordered_list"`, | ||
// [`bulletList`](#schema-list.bulletList) as `"bullet_list"`, and | ||
// [`listItem`](#schema-list.listItem) as `"list_item"`. | ||
// | ||
// `itemContent` determines the content expression for the list items. | ||
// If you want the commands defined in this module to apply to your | ||
// list structure, it should have a shape like `"paragraph block*"` or | ||
// `"paragraph (ordered_list | bullet_list)*"`. `listGroup` can be | ||
// given to assign a group name to the list node types, for example | ||
// `"block"`. | ||
export function addListNodes(nodes, itemContent, listGroup) { | ||
return nodes.append({ | ||
@@ -63,10 +64,9 @@ ordered_list: add(orderedList, {content: "list_item+", group: listGroup}), | ||
} | ||
exports.addListNodes = addListNodes | ||
// :: (NodeType, ?Object) → (state: EditorState, dispatch: ?(tr: Transaction)) → bool | ||
// Returns a command function that wraps the selection in a list with | ||
// the given type an attributes. If `apply` is `false`, only return a | ||
// the given type an attributes. If `dispatch` is null, only return a | ||
// value to indicate whether this is possible, but don't actually | ||
// perform the change. | ||
function wrapInList(listType, attrs) { | ||
export function wrapInList(listType, attrs) { | ||
return function(state, dispatch) { | ||
@@ -92,3 +92,2 @@ let {$from, $to} = state.selection | ||
} | ||
exports.wrapInList = wrapInList | ||
@@ -118,9 +117,28 @@ function doWrapInList(tr, range, wrappers, joinBefore, listType) { | ||
// of a list item by also splitting that list item. | ||
function splitListItem(itemType) { | ||
export function splitListItem(itemType) { | ||
return function(state, dispatch) { | ||
let {$from, $to, node} = state.selection | ||
if ((node && node.isBlock) || !$from.parent.content.size || | ||
$from.depth < 2 || !$from.sameParent($to)) return false | ||
if ((node && node.isBlock) || $from.depth < 2 || !$from.sameParent($to)) return false | ||
let grandParent = $from.node(-1) | ||
if (grandParent.type != itemType) return false | ||
if ($from.parent.content.size == 0) { | ||
// In an empty block. If this is a nested list, the wrapping | ||
// list item should be split. Otherwise, bail out and let next | ||
// command handle lifting. | ||
if ($from.depth == 2 || $from.node(-3).type != itemType || | ||
$from.index(-2) != $from.node(-2).childCount - 1) return false | ||
if (dispatch) { | ||
let wrap = Fragment.empty, keepItem = $from.index(-1) > 0 | ||
// Build a fragment containing empty versions of the structure | ||
// from the outer list item to the parent node of the cursor | ||
for (let d = $from.depth - (keepItem ? 1 : 2); d >= $from.depth - 3; d--) | ||
wrap = Fragment.from($from.node(d).copy(wrap)) | ||
// Add a second list item with an empty default start node | ||
wrap = wrap.append(Fragment.from(itemType.createAndFill())) | ||
let tr = state.tr.replace($from.before(keepItem ? null : -1), $from.after(-3), new Slice(wrap, keepItem ? 3 : 2, 2)) | ||
tr.setSelection(state.selection.constructor.near(tr.doc.resolve($from.pos + (keepItem ? 3 : 2)))) | ||
dispatch(tr.scrollIntoView()) | ||
} | ||
return true | ||
} | ||
let nextType = $to.pos == $from.end() ? grandParent.defaultContentType(0) : null | ||
@@ -134,3 +152,2 @@ let tr = state.tr.delete($from.pos, $to.pos) | ||
} | ||
exports.splitListItem = splitListItem | ||
@@ -140,3 +157,3 @@ // :: (NodeType) → (state: EditorState, dispatch: ?(tr: Transaction)) → bool | ||
// a wrapping list. | ||
function liftListItem(itemType) { | ||
export function liftListItem(itemType) { | ||
return function(state, dispatch) { | ||
@@ -153,3 +170,2 @@ let {$from, $to} = state.selection | ||
} | ||
exports.liftListItem = liftListItem | ||
@@ -197,3 +213,3 @@ function liftToOuterList(state, dispatch, itemType, range) { | ||
// into an inner list. | ||
function sinkListItem(itemType) { | ||
export function sinkListItem(itemType) { | ||
return function(state, dispatch) { | ||
@@ -221,2 +237,1 @@ let {$from, $to} = state.selection | ||
} | ||
exports.sinkListItem = sinkListItem |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51253
10
447
23
+ Addedprosemirror-model@0.23.1(transitive)
+ Addedprosemirror-transform@0.23.0(transitive)
- Removedprosemirror-model@0.22.0(transitive)
- Removedprosemirror-transform@0.22.2(transitive)
Updatedprosemirror-model@^0.23.0