prosemirror-schema-list
Advanced tools
Comparing version 0.19.0 to 0.20.0
@@ -74,3 +74,3 @@ var ref = require("prosemirror-transform"); | ||
// perform the change. | ||
function wrapInList(nodeType, attrs) { | ||
function wrapInList(listType, attrs) { | ||
return function(state, dispatch) { | ||
@@ -83,3 +83,3 @@ var ref = state.selection; | ||
// This is at the top of an existing list item | ||
if (range.depth >= 2 && $from.node(range.depth - 1).type.compatibleContent(nodeType) && range.startIndex == 0) { | ||
if (range.depth >= 2 && $from.node(range.depth - 1).type.compatibleContent(listType) && range.startIndex == 0) { | ||
// Don't do anything if this is the top of the list | ||
@@ -93,5 +93,5 @@ if ($from.index(range.depth - 1) == 0) { return false } | ||
} | ||
var wrap = findWrapping(outerRange, nodeType, attrs, range) | ||
var wrap = findWrapping(outerRange, listType, attrs, range) | ||
if (!wrap) { return false } | ||
if (dispatch) { dispatch(doWrapInList(state.tr, range, wrap, doJoin, nodeType).scrollIntoView()) } | ||
if (dispatch) { dispatch(doWrapInList(state.tr, range, wrap, doJoin, listType).scrollIntoView()) } | ||
return true | ||
@@ -102,3 +102,3 @@ } | ||
function doWrapInList(tr, range, wrappers, joinBefore, nodeType) { | ||
function doWrapInList(tr, range, wrappers, joinBefore, listType) { | ||
var content = Fragment.empty | ||
@@ -112,3 +112,3 @@ for (var i = wrappers.length - 1; i >= 0; i--) | ||
var found = 0 | ||
for (var i$1 = 0; i$1 < wrappers.length; i$1++) { if (wrappers[i$1].type == nodeType) { found = i$1 + 1 } } | ||
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 | ||
@@ -127,3 +127,3 @@ | ||
// of a list item by also splitting that list item. | ||
function splitListItem(nodeType) { | ||
function splitListItem(itemType) { | ||
return function(state, dispatch) { | ||
@@ -137,3 +137,3 @@ var ref = state.selection; | ||
var grandParent = $from.node(-1) | ||
if (grandParent.type != nodeType) { return false } | ||
if (grandParent.type != itemType) { return false } | ||
var nextType = $to.pos == $from.end() ? grandParent.defaultContentType($from.indexAfter(-1)) : null | ||
@@ -152,3 +152,3 @@ var tr = state.tr.delete($from.pos, $to.pos) | ||
// a wrapping list. | ||
function liftListItem(nodeType) { | ||
function liftListItem(itemType) { | ||
return function(state, dispatch) { | ||
@@ -158,16 +158,9 @@ var ref = state.selection; | ||
var $to = ref.$to; | ||
var range = $from.blockRange($to, function (node) { return node.childCount && node.firstChild.type == nodeType; }) | ||
if (!range || range.depth < 2 || $from.node(range.depth - 1).type != nodeType) { return false } | ||
if (dispatch) { | ||
var tr = state.tr, end = range.end, endOfList = $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(nodeType.create(null, range.parent.copy())), 1, 0), 1, true)) | ||
range = new NodeRange(tr.doc.resolveNoCache($from.pos), tr.doc.resolveNoCache(endOfList), range.depth) | ||
} | ||
dispatch(tr.lift(range, liftTarget(range)).scrollIntoView()) | ||
} | ||
return true | ||
var range = $from.blockRange($to, function (node) { return node.childCount && node.firstChild.type == itemType; }) | ||
if (!range) { return false } | ||
if (!dispatch) { return true } | ||
if ($from.node(range.depth - 1).type == itemType) // Inside a parent list | ||
{ return liftToOuterList(state, dispatch, itemType, range) } | ||
else // Outer list node | ||
{ return liftOutOfList(state, dispatch, range) } | ||
} | ||
@@ -177,6 +170,44 @@ } | ||
function liftToOuterList(state, dispatch, itemType, range) { | ||
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) | ||
} | ||
dispatch(tr.lift(range, liftTarget(range)).scrollIntoView()) | ||
return true | ||
} | ||
function liftOutOfList(state, dispatch, range) { | ||
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) | ||
} | ||
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)))) | ||
{ return false } | ||
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()) | ||
return true | ||
} | ||
// :: (NodeType) → (state: EditorState, dispatch: ?(tr: Transaction)) → bool | ||
// Create a command to sink the list item around the selection down | ||
// into an inner list. | ||
function sinkListItem(nodeType) { | ||
function sinkListItem(itemType) { | ||
return function(state, dispatch) { | ||
@@ -186,3 +217,3 @@ var ref = state.selection; | ||
var $to = ref.$to; | ||
var range = $from.blockRange($to, function (node) { return node.childCount && node.firstChild.type == nodeType; }) | ||
var range = $from.blockRange($to, function (node) { return node.childCount && node.firstChild.type == itemType; }) | ||
if (!range) { return false } | ||
@@ -192,8 +223,8 @@ var startIndex = range.startIndex | ||
var parent = range.parent, nodeBefore = parent.child(startIndex - 1) | ||
if (nodeBefore.type != nodeType) { return false } | ||
if (nodeBefore.type != itemType) { return false } | ||
if (dispatch) { | ||
var nestedBefore = nodeBefore.lastChild && nodeBefore.lastChild.type == parent.type | ||
var inner = Fragment.from(nestedBefore ? nodeType.create() : null) | ||
var slice = new Slice(Fragment.from(nodeType.create(null, Fragment.from(parent.copy(inner)))), | ||
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) | ||
@@ -200,0 +231,0 @@ var before = range.start, after = range.end |
{ | ||
"name": "prosemirror-schema-list", | ||
"version": "0.19.0", | ||
"version": "0.20.0", | ||
"description": "List-related schema elements and commands for ProseMirror", | ||
@@ -19,11 +19,12 @@ "main": "dist/schema-list.js", | ||
"dependencies": { | ||
"prosemirror-model": "^0.19.0", | ||
"prosemirror-transform": "^0.19.0" | ||
"prosemirror-model": "^0.20.0", | ||
"prosemirror-transform": "^0.20.0" | ||
}, | ||
"devDependencies": { | ||
"buble": "^0.15.1", | ||
"prosemirror-state": "^0.19.0", | ||
"prosemirror-state": "^0.20.0", | ||
"ist": "^1.0.0", | ||
"mocha": "^3.0.2", | ||
"rimraf": "^2.5.4" | ||
"rimraf": "^2.5.4", | ||
"prosemirror-test-builder": "^0.20.0" | ||
}, | ||
@@ -30,0 +31,0 @@ "scripts": { |
@@ -67,3 +67,3 @@ const {findWrapping, liftTarget, canSplit, ReplaceAroundStep} = require("prosemirror-transform") | ||
// perform the change. | ||
function wrapInList(nodeType, attrs) { | ||
function wrapInList(listType, attrs) { | ||
return function(state, dispatch) { | ||
@@ -74,3 +74,3 @@ let {$from, $to} = state.selection | ||
// This is at the top of an existing list item | ||
if (range.depth >= 2 && $from.node(range.depth - 1).type.compatibleContent(nodeType) && range.startIndex == 0) { | ||
if (range.depth >= 2 && $from.node(range.depth - 1).type.compatibleContent(listType) && range.startIndex == 0) { | ||
// Don't do anything if this is the top of the list | ||
@@ -84,5 +84,5 @@ if ($from.index(range.depth - 1) == 0) return false | ||
} | ||
let wrap = findWrapping(outerRange, nodeType, attrs, range) | ||
let wrap = findWrapping(outerRange, listType, attrs, range) | ||
if (!wrap) return false | ||
if (dispatch) dispatch(doWrapInList(state.tr, range, wrap, doJoin, nodeType).scrollIntoView()) | ||
if (dispatch) dispatch(doWrapInList(state.tr, range, wrap, doJoin, listType).scrollIntoView()) | ||
return true | ||
@@ -93,3 +93,3 @@ } | ||
function doWrapInList(tr, range, wrappers, joinBefore, nodeType) { | ||
function doWrapInList(tr, range, wrappers, joinBefore, listType) { | ||
let content = Fragment.empty | ||
@@ -103,3 +103,3 @@ for (let i = wrappers.length - 1; i >= 0; i--) | ||
let found = 0 | ||
for (let i = 0; i < wrappers.length; i++) if (wrappers[i].type == nodeType) found = i + 1 | ||
for (let i = 0; i < wrappers.length; i++) if (wrappers[i].type == listType) found = i + 1 | ||
let splitDepth = wrappers.length - found | ||
@@ -118,3 +118,3 @@ | ||
// of a list item by also splitting that list item. | ||
function splitListItem(nodeType) { | ||
function splitListItem(itemType) { | ||
return function(state, dispatch) { | ||
@@ -125,3 +125,3 @@ let {$from, $to, node} = state.selection | ||
let grandParent = $from.node(-1) | ||
if (grandParent.type != nodeType) return false | ||
if (grandParent.type != itemType) return false | ||
let nextType = $to.pos == $from.end() ? grandParent.defaultContentType($from.indexAfter(-1)) : null | ||
@@ -140,19 +140,12 @@ let tr = state.tr.delete($from.pos, $to.pos) | ||
// a wrapping list. | ||
function liftListItem(nodeType) { | ||
function liftListItem(itemType) { | ||
return function(state, dispatch) { | ||
let {$from, $to} = state.selection | ||
let range = $from.blockRange($to, node => node.childCount && node.firstChild.type == nodeType) | ||
if (!range || range.depth < 2 || $from.node(range.depth - 1).type != nodeType) return false | ||
if (dispatch) { | ||
let tr = state.tr, end = range.end, endOfList = $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(nodeType.create(null, range.parent.copy())), 1, 0), 1, true)) | ||
range = new NodeRange(tr.doc.resolveNoCache($from.pos), tr.doc.resolveNoCache(endOfList), range.depth) | ||
} | ||
dispatch(tr.lift(range, liftTarget(range)).scrollIntoView()) | ||
} | ||
return true | ||
let range = $from.blockRange($to, node => node.childCount && node.firstChild.type == itemType) | ||
if (!range) return false | ||
if (!dispatch) return true | ||
if ($from.node(range.depth - 1).type == itemType) // Inside a parent list | ||
return liftToOuterList(state, dispatch, itemType, range) | ||
else // Outer list node | ||
return liftOutOfList(state, dispatch, range) | ||
} | ||
@@ -162,9 +155,47 @@ } | ||
function liftToOuterList(state, dispatch, itemType, range) { | ||
let 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) | ||
} | ||
dispatch(tr.lift(range, liftTarget(range)).scrollIntoView()) | ||
return true | ||
} | ||
function liftOutOfList(state, dispatch, range) { | ||
let tr = state.tr, list = range.parent | ||
// Merge the list items into a single big item | ||
for (let pos = range.end, i = range.endIndex - 1, e = range.startIndex; i > e; i--) { | ||
pos -= list.child(i).nodeSize | ||
tr.delete(pos - 1, pos + 1) | ||
} | ||
let $start = tr.doc.resolve(range.start), item = $start.nodeAfter | ||
let atStart = range.startIndex == 0, atEnd = range.endIndex == list.childCount | ||
let 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)))) | ||
return false | ||
let 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()) | ||
return true | ||
} | ||
// :: (NodeType) → (state: EditorState, dispatch: ?(tr: Transaction)) → bool | ||
// Create a command to sink the list item around the selection down | ||
// into an inner list. | ||
function sinkListItem(nodeType) { | ||
function sinkListItem(itemType) { | ||
return function(state, dispatch) { | ||
let {$from, $to} = state.selection | ||
let range = $from.blockRange($to, node => node.childCount && node.firstChild.type == nodeType) | ||
let range = $from.blockRange($to, node => node.childCount && node.firstChild.type == itemType) | ||
if (!range) return false | ||
@@ -174,8 +205,8 @@ let startIndex = range.startIndex | ||
let parent = range.parent, nodeBefore = parent.child(startIndex - 1) | ||
if (nodeBefore.type != nodeType) return false | ||
if (nodeBefore.type != itemType) return false | ||
if (dispatch) { | ||
let nestedBefore = nodeBefore.lastChild && nodeBefore.lastChild.type == parent.type | ||
let inner = Fragment.from(nestedBefore ? nodeType.create() : null) | ||
let slice = new Slice(Fragment.from(nodeType.create(null, Fragment.from(parent.copy(inner)))), | ||
let inner = Fragment.from(nestedBefore ? itemType.create() : null) | ||
let slice = new Slice(Fragment.from(itemType.create(null, Fragment.from(parent.copy(inner)))), | ||
nestedBefore ? 3 : 1, 0) | ||
@@ -182,0 +213,0 @@ let before = range.start, after = range.end |
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
27062
406
6
+ Addedprosemirror-model@0.20.0(transitive)
+ Addedprosemirror-transform@0.20.0(transitive)
- Removedprosemirror-model@0.19.0(transitive)
- Removedprosemirror-transform@0.19.0(transitive)
Updatedprosemirror-model@^0.20.0