tiptap-commands
Advanced tools
Comparing version 0.7.1 to 1.0.0
/*! | ||
* tiptap vundefined | ||
* (c) 2018 Scrumpy UG (limited liability) | ||
* @license MIT | ||
*/ | ||
/*! | ||
* tiptap-commands v0.7.1 | ||
* (c) 2018 Scrumpy UG (limited liability) | ||
* @license MIT | ||
*/ | ||
'use strict'; | ||
@@ -13,44 +13,43 @@ | ||
var prosemirrorInputrules = require('prosemirror-inputrules'); | ||
var prosemirrorSchemaList = require('prosemirror-schema-list'); | ||
var prosemirrorCommands = require('prosemirror-commands'); | ||
var tiptapUtils = require('tiptap-utils'); | ||
var prosemirrorSchemaList = require('prosemirror-schema-list'); | ||
function insertText (text) { | ||
if ( text === void 0 ) text = ''; | ||
function insertText () { | ||
var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
return function (state, dispatch) { | ||
var $from = state.selection.$from; | ||
var pos = $from.pos.pos; | ||
dispatch(state.tr.insertText(text, pos)); | ||
return true; | ||
}; | ||
} | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var ref$1 = $from.pos; | ||
var pos = ref$1.pos; | ||
function markInputRule (regexp, markType, getAttrs) { | ||
return new prosemirrorInputrules.InputRule(regexp, function (state, match, start, end) { | ||
var attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; | ||
var tr = state.tr; | ||
var markEnd = end; | ||
dispatch(state.tr.insertText(text, pos)); | ||
if (match[1]) { | ||
var startSpaces = match[0].search(/\S/); | ||
var textStart = start + match[0].indexOf(match[1]); | ||
var textEnd = textStart + match[1].length; | ||
return true | ||
} | ||
} | ||
if (textEnd < end) { | ||
tr.delete(textEnd, end); | ||
} | ||
function markInputRule (regexp, markType, getAttrs) { | ||
return new prosemirrorInputrules.InputRule(regexp, function (state, match, start, end) { | ||
var attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; | ||
var tr = state.tr; | ||
var markEnd = end; | ||
if (textStart > start) { | ||
tr.delete(start + startSpaces, textStart); | ||
} | ||
if (match[1]) { | ||
var startSpaces = match[0].search(/\S/); | ||
var textStart = start + match[0].indexOf(match[1]); | ||
var textEnd = textStart + match[1].length; | ||
if (textEnd < end) { | ||
tr.delete(textEnd, end); | ||
} | ||
if (textStart > start) { | ||
tr.delete(start + startSpaces, textStart); | ||
} | ||
markEnd = start + startSpaces + match[1].length; | ||
} | ||
markEnd = start + startSpaces + match[1].length; | ||
} | ||
tr.addMark(start, markEnd, markType.create(attrs)); | ||
tr.removeStoredMark(markType); // Do not continue with mark. | ||
return tr | ||
}) | ||
tr.addMark(start, markEnd, markType.create(attrs)); | ||
tr.removeStoredMark(markType); // Do not continue with mark. | ||
return tr; | ||
}); | ||
} | ||
@@ -60,47 +59,43 @@ | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var from = ref.from; | ||
var to = ref.to; | ||
return dispatch(state.tr.removeMark(from, to, type)) | ||
} | ||
var _state$selection = state.selection, | ||
from = _state$selection.from, | ||
to = _state$selection.to; | ||
return dispatch(state.tr.removeMark(from, to, type)); | ||
}; | ||
} | ||
function replaceText (range, type, attrs) { | ||
if ( attrs === void 0 ) attrs = {}; | ||
function replaceText (range, type) { | ||
var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return function (state, dispatch) { | ||
var $from = state.selection.$from; | ||
var index = $from.index(); | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var index = $from.index(); | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false; | ||
} | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceWith(range.from, range.to, type.create(attrs))); | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceWith(range.from, range.to, type.create(attrs))); | ||
} | ||
return true | ||
} | ||
return true; | ||
}; | ||
} | ||
function setInlineBlockType (type, attrs) { | ||
if ( attrs === void 0 ) attrs = {}; | ||
function setInlineBlockType (type) { | ||
var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return function (state, dispatch) { | ||
var $from = state.selection.$from; | ||
var index = $from.index(); | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var index = $from.index(); | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false; | ||
} | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceSelectionWith(type.create(attrs))); | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceSelectionWith(type.create(attrs))); | ||
} | ||
return true | ||
} | ||
return true; | ||
}; | ||
} | ||
@@ -110,45 +105,47 @@ | ||
// see https://github.com/ProseMirror/prosemirror-transform/blob/master/src/structure.js | ||
// Since this piece of code was "borrowed" from prosemirror, ESLint rules are ignored. | ||
// Since this piece of code was "borrowed" from prosemirror, ESLint rules are ignored. | ||
/* eslint-disable max-len, no-plusplus, no-undef, eqeqeq */ | ||
function canSplit(doc, pos, depth, typesAfter) { | ||
if ( depth === void 0 ) depth = 1; | ||
function canSplit(doc, pos) { | ||
var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; | ||
var typesAfter = arguments.length > 3 ? arguments[3] : undefined; | ||
var $pos = doc.resolve(pos); | ||
var base = $pos.depth - depth; | ||
var innerType = typesAfter && typesAfter[typesAfter.length - 1] || $pos.parent; | ||
if (base < 0 || $pos.parent.type.spec.isolating || !$pos.parent.canReplace($pos.index(), $pos.parent.childCount) || !innerType.type.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount))) return false; | ||
var $pos = doc.resolve(pos); var | ||
base = $pos.depth - depth; | ||
var innerType = (typesAfter && typesAfter[typesAfter.length - 1]) || $pos.parent; | ||
if (base < 0 || $pos.parent.type.spec.isolating | ||
|| !$pos.parent.canReplace($pos.index(), $pos.parent.childCount) | ||
|| !innerType.type.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount))) { return false } | ||
for (var d = $pos.depth - 1, i = depth - 2; d > base; d--, i--) { | ||
var node = $pos.node(d); var | ||
index$1 = $pos.index(d); | ||
if (node.type.spec.isolating) { return false } | ||
var rest = node.content.cutByIndex(index$1, node.childCount); | ||
var after = (typesAfter && typesAfter[i]) || node; | ||
if (after != node) { rest = rest.replaceChild(0, after.type.create(after.attrs)); } | ||
var node = $pos.node(d); | ||
/* Change starts from here */ | ||
// if (!node.canReplace(index + 1, node.childCount) || !after.type.validContent(rest)) | ||
var _index = $pos.index(d); | ||
if (node.type.spec.isolating) return false; | ||
var rest = node.content.cutByIndex(_index, node.childCount); | ||
var after = typesAfter && typesAfter[i] || node; | ||
if (after != node) rest = rest.replaceChild(0, after.type.create(after.attrs)); | ||
/* Change starts from here */ | ||
// if (!node.canReplace(index + 1, node.childCount) || !after.type.validContent(rest)) | ||
// return false | ||
if (!node.canReplace(index$1 + 1, node.childCount)) { return false } | ||
/* Change ends here */ | ||
if (!node.canReplace(_index + 1, node.childCount)) return false; | ||
/* Change ends here */ | ||
} | ||
var index = $pos.indexAfter(base); | ||
var baseType = typesAfter && typesAfter[0]; | ||
return $pos.node(base).canReplaceWith(index, index, baseType ? baseType.type : $pos.node(base + 1).type) | ||
} | ||
// this is a copy of splitListItem | ||
return $pos.node(base).canReplaceWith(index, index, baseType ? baseType.type : $pos.node(base + 1).type); | ||
} // this is a copy of splitListItem | ||
// see https://github.com/ProseMirror/prosemirror-schema-list/blob/master/src/schema-list.js | ||
function splitListItem(itemType) { | ||
return function _splitListItem(state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var $to = ref.$to; | ||
var node = ref.node; | ||
if ((node && node.isBlock) || $from.depth < 2 || !$from.sameParent($to)) { return false } | ||
var _state$selection = state.selection, | ||
$from = _state$selection.$from, | ||
$to = _state$selection.$to, | ||
node = _state$selection.node; | ||
if (node && node.isBlock || $from.depth < 2 || !$from.sameParent($to)) return false; | ||
var grandParent = $from.node(-1); | ||
if (grandParent.type != itemType) { return false } | ||
if (grandParent.type != itemType) return false; | ||
if ($from.parent.content.size == 0) { | ||
@@ -158,47 +155,64 @@ // In an empty block. If this is a nested list, the wrapping | ||
// command handle lifting. | ||
if ($from.depth == 2 || $from.node(-3).type != itemType | ||
|| $from.index(-2) != $from.node(-2).childCount - 1) { return false } | ||
if ($from.depth == 2 || $from.node(-3).type != itemType || $from.index(-2) != $from.node(-2).childCount - 1) return false; | ||
if (dispatch) { | ||
var wrap = Fragment.empty; var | ||
keepItem = $from.index(-1) > 0; | ||
// Build a fragment containing empty versions of the structure | ||
var wrap = Fragment.empty; | ||
var 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 = 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())); | ||
var tr$1 = state.tr.replace($from.before(keepItem ? null : -1), $from.after(-3), new 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()); | ||
for (var 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())); | ||
var _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 | ||
return true; | ||
} | ||
var nextType = $to.pos == $from.end() ? grandParent.contentMatchAt($from.indexAfter(-1)).defaultType : null; | ||
var tr = state.tr.delete($from.pos, $to.pos); | ||
/* Change starts from here */ | ||
// let types = nextType && [null, {type: nextType}] | ||
var types = nextType && [{ type: itemType }, { type: nextType }]; | ||
if (!types) { types = [{ type: itemType }, null]; } | ||
/* Change ends here */ | ||
var nextType = $to.pos == $from.end() ? grandParent.contentMatchAt($from.indexAfter(-1)).defaultType : null; | ||
var tr = state.tr.delete($from.pos, $to.pos); | ||
/* Change starts from here */ | ||
// let types = nextType && [null, {type: nextType}] | ||
if (!canSplit(tr.doc, $from.pos, 2, types)) { return false } | ||
if (dispatch) { dispatch(tr.split($from.pos, 2, [{ type: state.schema.nodes.todo_item, attrs: { done: false } }]).scrollIntoView()); } | ||
return true | ||
} | ||
var types = nextType && [{ | ||
type: itemType | ||
}, { | ||
type: nextType | ||
}]; | ||
if (!types) types = [{ | ||
type: itemType | ||
}, null]; | ||
/* Change ends here */ | ||
if (!canSplit(tr.doc, $from.pos, 2, types)) return false; | ||
if (dispatch) dispatch(tr.split($from.pos, 2, [{ | ||
type: state.schema.nodes.todo_item, | ||
attrs: { | ||
done: false | ||
} | ||
}]).scrollIntoView()); | ||
return true; | ||
}; | ||
} | ||
/* eslint-enable max-len, no-plusplus, no-undef, eqeqeq */ | ||
function toggleBlockType (type, toggletype, attrs) { | ||
if ( attrs === void 0 ) attrs = {}; | ||
function toggleBlockType (type, toggletype) { | ||
var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type, attrs); | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type, attrs); | ||
if (isActive) { | ||
return prosemirrorCommands.setBlockType(toggletype)(state, dispatch, view); | ||
} | ||
if (isActive) { | ||
return prosemirrorCommands.setBlockType(toggletype)(state, dispatch, view) | ||
} | ||
return prosemirrorCommands.setBlockType(type, attrs)(state, dispatch, view) | ||
} | ||
return prosemirrorCommands.setBlockType(type, attrs)(state, dispatch, view); | ||
}; | ||
} | ||
@@ -211,21 +225,15 @@ | ||
if (isActive) { | ||
return prosemirrorSchemaList.liftListItem(itemType)(state, dispatch, view) | ||
return prosemirrorSchemaList.liftListItem(itemType)(state, dispatch, view); | ||
} | ||
return prosemirrorSchemaList.wrapInList(type)(state, dispatch, view) | ||
} | ||
} | ||
// https://discuss.prosemirror.net/t/list-type-toggle/948 | ||
return prosemirrorSchemaList.wrapInList(type)(state, dispatch, view); | ||
}; | ||
} // https://discuss.prosemirror.net/t/list-type-toggle/948 | ||
// import { wrapInList, liftListItem } from 'prosemirror-schema-list' | ||
// function isList(node, schema) { | ||
// return (node.type === schema.nodes.bullet_list || node.type === schema.nodes.ordered_list) | ||
// } | ||
// export default function toggleList(listType, schema) { | ||
// const lift = liftListItem(schema.nodes.list_item) | ||
// const wrap = wrapInList(listType) | ||
// return (state, dispatch) => { | ||
@@ -237,3 +245,2 @@ // const { $from, $to } = state.selection | ||
// } | ||
// if (range.depth >= 2 && $from.node(range.depth - 1).type === listType) { | ||
@@ -256,11 +263,11 @@ // return lift(state, dispatch) | ||
function toggleWrap (type) { | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type); | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type); | ||
if (isActive) { | ||
return prosemirrorCommands.lift(state, dispatch) | ||
} | ||
if (isActive) { | ||
return prosemirrorCommands.lift(state, dispatch); | ||
} | ||
return prosemirrorCommands.wrapIn(type)(state, dispatch, view) | ||
} | ||
return prosemirrorCommands.wrapIn(type)(state, dispatch, view); | ||
}; | ||
} | ||
@@ -270,7 +277,7 @@ | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var from = ref.from; | ||
var to = ref.to; | ||
return dispatch(state.tr.addMark(from, to, type.create(attrs))) | ||
} | ||
var _state$selection = state.selection, | ||
from = _state$selection.from, | ||
to = _state$selection.to; | ||
return dispatch(state.tr.addMark(from, to, type.create(attrs))); | ||
}; | ||
} | ||
@@ -280,2 +287,7 @@ | ||
exports.textblockTypeInputRule = prosemirrorInputrules.textblockTypeInputRule; | ||
exports.addListNodes = prosemirrorSchemaList.addListNodes; | ||
exports.wrapInList = prosemirrorSchemaList.wrapInList; | ||
exports.splitListItem = prosemirrorSchemaList.splitListItem; | ||
exports.liftListItem = prosemirrorSchemaList.liftListItem; | ||
exports.sinkListItem = prosemirrorSchemaList.sinkListItem; | ||
exports.chainCommands = prosemirrorCommands.chainCommands; | ||
@@ -305,7 +317,2 @@ exports.deleteSelection = prosemirrorCommands.deleteSelection; | ||
exports.macBaseKeymap = prosemirrorCommands.macBaseKeymap; | ||
exports.addListNodes = prosemirrorSchemaList.addListNodes; | ||
exports.wrapInList = prosemirrorSchemaList.wrapInList; | ||
exports.splitListItem = prosemirrorSchemaList.splitListItem; | ||
exports.liftListItem = prosemirrorSchemaList.liftListItem; | ||
exports.sinkListItem = prosemirrorSchemaList.sinkListItem; | ||
exports.insertText = insertText; | ||
@@ -312,0 +319,0 @@ exports.markInputRule = markInputRule; |
/*! | ||
* tiptap vundefined | ||
* (c) 2018 Scrumpy UG (limited liability) | ||
* @license MIT | ||
*/ | ||
/*! | ||
* tiptap-commands v0.7.1 | ||
* (c) 2018 Scrumpy UG (limited liability) | ||
* @license MIT | ||
*/ | ||
import { InputRule, wrappingInputRule, textblockTypeInputRule } from 'prosemirror-inputrules'; | ||
export { wrappingInputRule, textblockTypeInputRule } from 'prosemirror-inputrules'; | ||
import { wrapInList, liftListItem, addListNodes, splitListItem, sinkListItem } from 'prosemirror-schema-list'; | ||
export { addListNodes, wrapInList, splitListItem, liftListItem, sinkListItem } from 'prosemirror-schema-list'; | ||
import { setBlockType, wrapIn, lift, chainCommands, deleteSelection, joinBackward, selectNodeBackward, joinForward, selectNodeForward, joinUp, joinDown, newlineInCode, exitCode, createParagraphNear, liftEmptyBlock, splitBlock, splitBlockKeepMarks, selectParentNode, selectAll, toggleMark, autoJoin, baseKeymap, pcBaseKeymap, macBaseKeymap } from 'prosemirror-commands'; | ||
export { chainCommands, deleteSelection, joinBackward, selectNodeBackward, joinForward, selectNodeForward, joinUp, joinDown, lift, newlineInCode, exitCode, createParagraphNear, liftEmptyBlock, splitBlock, splitBlockKeepMarks, selectParentNode, selectAll, wrapIn, setBlockType, toggleMark, autoJoin, baseKeymap, pcBaseKeymap, macBaseKeymap } from 'prosemirror-commands'; | ||
import { nodeIsActive } from 'tiptap-utils'; | ||
import { wrapInList, liftListItem, addListNodes, splitListItem, sinkListItem } from 'prosemirror-schema-list'; | ||
export { addListNodes, wrapInList, splitListItem, liftListItem, sinkListItem } from 'prosemirror-schema-list'; | ||
function insertText (text) { | ||
if ( text === void 0 ) text = ''; | ||
function insertText () { | ||
var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
return function (state, dispatch) { | ||
var $from = state.selection.$from; | ||
var pos = $from.pos.pos; | ||
dispatch(state.tr.insertText(text, pos)); | ||
return true; | ||
}; | ||
} | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var ref$1 = $from.pos; | ||
var pos = ref$1.pos; | ||
function markInputRule (regexp, markType, getAttrs) { | ||
return new InputRule(regexp, function (state, match, start, end) { | ||
var attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; | ||
var tr = state.tr; | ||
var markEnd = end; | ||
dispatch(state.tr.insertText(text, pos)); | ||
if (match[1]) { | ||
var startSpaces = match[0].search(/\S/); | ||
var textStart = start + match[0].indexOf(match[1]); | ||
var textEnd = textStart + match[1].length; | ||
return true | ||
} | ||
} | ||
if (textEnd < end) { | ||
tr.delete(textEnd, end); | ||
} | ||
function markInputRule (regexp, markType, getAttrs) { | ||
return new InputRule(regexp, function (state, match, start, end) { | ||
var attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; | ||
var tr = state.tr; | ||
var markEnd = end; | ||
if (textStart > start) { | ||
tr.delete(start + startSpaces, textStart); | ||
} | ||
if (match[1]) { | ||
var startSpaces = match[0].search(/\S/); | ||
var textStart = start + match[0].indexOf(match[1]); | ||
var textEnd = textStart + match[1].length; | ||
if (textEnd < end) { | ||
tr.delete(textEnd, end); | ||
} | ||
if (textStart > start) { | ||
tr.delete(start + startSpaces, textStart); | ||
} | ||
markEnd = start + startSpaces + match[1].length; | ||
} | ||
markEnd = start + startSpaces + match[1].length; | ||
} | ||
tr.addMark(start, markEnd, markType.create(attrs)); | ||
tr.removeStoredMark(markType); // Do not continue with mark. | ||
return tr | ||
}) | ||
tr.addMark(start, markEnd, markType.create(attrs)); | ||
tr.removeStoredMark(markType); // Do not continue with mark. | ||
return tr; | ||
}); | ||
} | ||
@@ -58,47 +57,43 @@ | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var from = ref.from; | ||
var to = ref.to; | ||
return dispatch(state.tr.removeMark(from, to, type)) | ||
} | ||
var _state$selection = state.selection, | ||
from = _state$selection.from, | ||
to = _state$selection.to; | ||
return dispatch(state.tr.removeMark(from, to, type)); | ||
}; | ||
} | ||
function replaceText (range, type, attrs) { | ||
if ( attrs === void 0 ) attrs = {}; | ||
function replaceText (range, type) { | ||
var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return function (state, dispatch) { | ||
var $from = state.selection.$from; | ||
var index = $from.index(); | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var index = $from.index(); | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false; | ||
} | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceWith(range.from, range.to, type.create(attrs))); | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceWith(range.from, range.to, type.create(attrs))); | ||
} | ||
return true | ||
} | ||
return true; | ||
}; | ||
} | ||
function setInlineBlockType (type, attrs) { | ||
if ( attrs === void 0 ) attrs = {}; | ||
function setInlineBlockType (type) { | ||
var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return function (state, dispatch) { | ||
var $from = state.selection.$from; | ||
var index = $from.index(); | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var index = $from.index(); | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false; | ||
} | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceSelectionWith(type.create(attrs))); | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceSelectionWith(type.create(attrs))); | ||
} | ||
return true | ||
} | ||
return true; | ||
}; | ||
} | ||
@@ -108,45 +103,47 @@ | ||
// see https://github.com/ProseMirror/prosemirror-transform/blob/master/src/structure.js | ||
// Since this piece of code was "borrowed" from prosemirror, ESLint rules are ignored. | ||
// Since this piece of code was "borrowed" from prosemirror, ESLint rules are ignored. | ||
/* eslint-disable max-len, no-plusplus, no-undef, eqeqeq */ | ||
function canSplit(doc, pos, depth, typesAfter) { | ||
if ( depth === void 0 ) depth = 1; | ||
function canSplit(doc, pos) { | ||
var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; | ||
var typesAfter = arguments.length > 3 ? arguments[3] : undefined; | ||
var $pos = doc.resolve(pos); | ||
var base = $pos.depth - depth; | ||
var innerType = typesAfter && typesAfter[typesAfter.length - 1] || $pos.parent; | ||
if (base < 0 || $pos.parent.type.spec.isolating || !$pos.parent.canReplace($pos.index(), $pos.parent.childCount) || !innerType.type.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount))) return false; | ||
var $pos = doc.resolve(pos); var | ||
base = $pos.depth - depth; | ||
var innerType = (typesAfter && typesAfter[typesAfter.length - 1]) || $pos.parent; | ||
if (base < 0 || $pos.parent.type.spec.isolating | ||
|| !$pos.parent.canReplace($pos.index(), $pos.parent.childCount) | ||
|| !innerType.type.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount))) { return false } | ||
for (var d = $pos.depth - 1, i = depth - 2; d > base; d--, i--) { | ||
var node = $pos.node(d); var | ||
index$1 = $pos.index(d); | ||
if (node.type.spec.isolating) { return false } | ||
var rest = node.content.cutByIndex(index$1, node.childCount); | ||
var after = (typesAfter && typesAfter[i]) || node; | ||
if (after != node) { rest = rest.replaceChild(0, after.type.create(after.attrs)); } | ||
var node = $pos.node(d); | ||
/* Change starts from here */ | ||
// if (!node.canReplace(index + 1, node.childCount) || !after.type.validContent(rest)) | ||
var _index = $pos.index(d); | ||
if (node.type.spec.isolating) return false; | ||
var rest = node.content.cutByIndex(_index, node.childCount); | ||
var after = typesAfter && typesAfter[i] || node; | ||
if (after != node) rest = rest.replaceChild(0, after.type.create(after.attrs)); | ||
/* Change starts from here */ | ||
// if (!node.canReplace(index + 1, node.childCount) || !after.type.validContent(rest)) | ||
// return false | ||
if (!node.canReplace(index$1 + 1, node.childCount)) { return false } | ||
/* Change ends here */ | ||
if (!node.canReplace(_index + 1, node.childCount)) return false; | ||
/* Change ends here */ | ||
} | ||
var index = $pos.indexAfter(base); | ||
var baseType = typesAfter && typesAfter[0]; | ||
return $pos.node(base).canReplaceWith(index, index, baseType ? baseType.type : $pos.node(base + 1).type) | ||
} | ||
// this is a copy of splitListItem | ||
return $pos.node(base).canReplaceWith(index, index, baseType ? baseType.type : $pos.node(base + 1).type); | ||
} // this is a copy of splitListItem | ||
// see https://github.com/ProseMirror/prosemirror-schema-list/blob/master/src/schema-list.js | ||
function splitListItem$1(itemType) { | ||
return function _splitListItem(state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var $to = ref.$to; | ||
var node = ref.node; | ||
if ((node && node.isBlock) || $from.depth < 2 || !$from.sameParent($to)) { return false } | ||
var _state$selection = state.selection, | ||
$from = _state$selection.$from, | ||
$to = _state$selection.$to, | ||
node = _state$selection.node; | ||
if (node && node.isBlock || $from.depth < 2 || !$from.sameParent($to)) return false; | ||
var grandParent = $from.node(-1); | ||
if (grandParent.type != itemType) { return false } | ||
if (grandParent.type != itemType) return false; | ||
if ($from.parent.content.size == 0) { | ||
@@ -156,47 +153,64 @@ // In an empty block. If this is a nested list, the wrapping | ||
// command handle lifting. | ||
if ($from.depth == 2 || $from.node(-3).type != itemType | ||
|| $from.index(-2) != $from.node(-2).childCount - 1) { return false } | ||
if ($from.depth == 2 || $from.node(-3).type != itemType || $from.index(-2) != $from.node(-2).childCount - 1) return false; | ||
if (dispatch) { | ||
var wrap = Fragment.empty; var | ||
keepItem = $from.index(-1) > 0; | ||
// Build a fragment containing empty versions of the structure | ||
var wrap = Fragment.empty; | ||
var 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 = 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())); | ||
var tr$1 = state.tr.replace($from.before(keepItem ? null : -1), $from.after(-3), new 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()); | ||
for (var 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())); | ||
var _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 | ||
return true; | ||
} | ||
var nextType = $to.pos == $from.end() ? grandParent.contentMatchAt($from.indexAfter(-1)).defaultType : null; | ||
var tr = state.tr.delete($from.pos, $to.pos); | ||
/* Change starts from here */ | ||
// let types = nextType && [null, {type: nextType}] | ||
var types = nextType && [{ type: itemType }, { type: nextType }]; | ||
if (!types) { types = [{ type: itemType }, null]; } | ||
/* Change ends here */ | ||
var nextType = $to.pos == $from.end() ? grandParent.contentMatchAt($from.indexAfter(-1)).defaultType : null; | ||
var tr = state.tr.delete($from.pos, $to.pos); | ||
/* Change starts from here */ | ||
// let types = nextType && [null, {type: nextType}] | ||
if (!canSplit(tr.doc, $from.pos, 2, types)) { return false } | ||
if (dispatch) { dispatch(tr.split($from.pos, 2, [{ type: state.schema.nodes.todo_item, attrs: { done: false } }]).scrollIntoView()); } | ||
return true | ||
} | ||
var types = nextType && [{ | ||
type: itemType | ||
}, { | ||
type: nextType | ||
}]; | ||
if (!types) types = [{ | ||
type: itemType | ||
}, null]; | ||
/* Change ends here */ | ||
if (!canSplit(tr.doc, $from.pos, 2, types)) return false; | ||
if (dispatch) dispatch(tr.split($from.pos, 2, [{ | ||
type: state.schema.nodes.todo_item, | ||
attrs: { | ||
done: false | ||
} | ||
}]).scrollIntoView()); | ||
return true; | ||
}; | ||
} | ||
/* eslint-enable max-len, no-plusplus, no-undef, eqeqeq */ | ||
function toggleBlockType (type, toggletype, attrs) { | ||
if ( attrs === void 0 ) attrs = {}; | ||
function toggleBlockType (type, toggletype) { | ||
var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return function (state, dispatch, view) { | ||
var isActive = nodeIsActive(state, type, attrs); | ||
return function (state, dispatch, view) { | ||
var isActive = nodeIsActive(state, type, attrs); | ||
if (isActive) { | ||
return setBlockType(toggletype)(state, dispatch, view); | ||
} | ||
if (isActive) { | ||
return setBlockType(toggletype)(state, dispatch, view) | ||
} | ||
return setBlockType(type, attrs)(state, dispatch, view) | ||
} | ||
return setBlockType(type, attrs)(state, dispatch, view); | ||
}; | ||
} | ||
@@ -209,21 +223,15 @@ | ||
if (isActive) { | ||
return liftListItem(itemType)(state, dispatch, view) | ||
return liftListItem(itemType)(state, dispatch, view); | ||
} | ||
return wrapInList(type)(state, dispatch, view) | ||
} | ||
} | ||
// https://discuss.prosemirror.net/t/list-type-toggle/948 | ||
return wrapInList(type)(state, dispatch, view); | ||
}; | ||
} // https://discuss.prosemirror.net/t/list-type-toggle/948 | ||
// import { wrapInList, liftListItem } from 'prosemirror-schema-list' | ||
// function isList(node, schema) { | ||
// return (node.type === schema.nodes.bullet_list || node.type === schema.nodes.ordered_list) | ||
// } | ||
// export default function toggleList(listType, schema) { | ||
// const lift = liftListItem(schema.nodes.list_item) | ||
// const wrap = wrapInList(listType) | ||
// return (state, dispatch) => { | ||
@@ -235,3 +243,2 @@ // const { $from, $to } = state.selection | ||
// } | ||
// if (range.depth >= 2 && $from.node(range.depth - 1).type === listType) { | ||
@@ -254,11 +261,11 @@ // return lift(state, dispatch) | ||
function toggleWrap (type) { | ||
return function (state, dispatch, view) { | ||
var isActive = nodeIsActive(state, type); | ||
return function (state, dispatch, view) { | ||
var isActive = nodeIsActive(state, type); | ||
if (isActive) { | ||
return lift(state, dispatch) | ||
} | ||
if (isActive) { | ||
return lift(state, dispatch); | ||
} | ||
return wrapIn(type)(state, dispatch, view) | ||
} | ||
return wrapIn(type)(state, dispatch, view); | ||
}; | ||
} | ||
@@ -268,9 +275,9 @@ | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var from = ref.from; | ||
var to = ref.to; | ||
return dispatch(state.tr.addMark(from, to, type.create(attrs))) | ||
} | ||
var _state$selection = state.selection, | ||
from = _state$selection.from, | ||
to = _state$selection.to; | ||
return dispatch(state.tr.addMark(from, to, type.create(attrs))); | ||
}; | ||
} | ||
export { insertText, markInputRule, removeMark, replaceText, setInlineBlockType, splitListItem$1 as splitToDefaultListItem, toggleBlockType, toggleList, toggleWrap, updateMark }; |
/*! | ||
* tiptap vundefined | ||
* (c) 2018 Scrumpy UG (limited liability) | ||
* @license MIT | ||
*/ | ||
/*! | ||
* tiptap-commands v0.7.1 | ||
* (c) 2018 Scrumpy UG (limited liability) | ||
* @license MIT | ||
*/ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('prosemirror-inputrules'), require('prosemirror-commands'), require('tiptap-utils'), require('prosemirror-schema-list')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'prosemirror-inputrules', 'prosemirror-commands', 'tiptap-utils', 'prosemirror-schema-list'], factory) : | ||
(factory((global.tiptap = {}),global.prosemirrorInputrules,global.prosemirrorCommands,global.tiptapUtils,global.prosemirrorSchemaList)); | ||
}(this, (function (exports,prosemirrorInputrules,prosemirrorCommands,tiptapUtils,prosemirrorSchemaList) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('prosemirror-inputrules'), require('prosemirror-schema-list'), require('prosemirror-commands'), require('tiptap-utils')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'prosemirror-inputrules', 'prosemirror-schema-list', 'prosemirror-commands', 'tiptap-utils'], factory) : | ||
(factory((global.tiptap = {}),global.prosemirrorInputrules,global.prosemirrorSchemaList,global.prosemirrorCommands,global.tiptapUtils)); | ||
}(this, (function (exports,prosemirrorInputrules,prosemirrorSchemaList,prosemirrorCommands,tiptapUtils) { 'use strict'; | ||
function insertText (text) { | ||
if ( text === void 0 ) text = ''; | ||
function insertText () { | ||
var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
return function (state, dispatch) { | ||
var $from = state.selection.$from; | ||
var pos = $from.pos.pos; | ||
dispatch(state.tr.insertText(text, pos)); | ||
return true; | ||
}; | ||
} | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var ref$1 = $from.pos; | ||
var pos = ref$1.pos; | ||
function markInputRule (regexp, markType, getAttrs) { | ||
return new prosemirrorInputrules.InputRule(regexp, function (state, match, start, end) { | ||
var attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; | ||
var tr = state.tr; | ||
var markEnd = end; | ||
dispatch(state.tr.insertText(text, pos)); | ||
if (match[1]) { | ||
var startSpaces = match[0].search(/\S/); | ||
var textStart = start + match[0].indexOf(match[1]); | ||
var textEnd = textStart + match[1].length; | ||
return true | ||
} | ||
} | ||
if (textEnd < end) { | ||
tr.delete(textEnd, end); | ||
} | ||
function markInputRule (regexp, markType, getAttrs) { | ||
return new prosemirrorInputrules.InputRule(regexp, function (state, match, start, end) { | ||
var attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; | ||
var tr = state.tr; | ||
var markEnd = end; | ||
if (textStart > start) { | ||
tr.delete(start + startSpaces, textStart); | ||
} | ||
if (match[1]) { | ||
var startSpaces = match[0].search(/\S/); | ||
var textStart = start + match[0].indexOf(match[1]); | ||
var textEnd = textStart + match[1].length; | ||
if (textEnd < end) { | ||
tr.delete(textEnd, end); | ||
} | ||
if (textStart > start) { | ||
tr.delete(start + startSpaces, textStart); | ||
} | ||
markEnd = start + startSpaces + match[1].length; | ||
} | ||
markEnd = start + startSpaces + match[1].length; | ||
} | ||
tr.addMark(start, markEnd, markType.create(attrs)); | ||
tr.removeStoredMark(markType); // Do not continue with mark. | ||
return tr | ||
}) | ||
} | ||
tr.addMark(start, markEnd, markType.create(attrs)); | ||
tr.removeStoredMark(markType); // Do not continue with mark. | ||
function removeMark (type) { | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var from = ref.from; | ||
var to = ref.to; | ||
return dispatch(state.tr.removeMark(from, to, type)) | ||
} | ||
} | ||
return tr; | ||
}); | ||
} | ||
function replaceText (range, type, attrs) { | ||
if ( attrs === void 0 ) attrs = {}; | ||
function removeMark (type) { | ||
return function (state, dispatch) { | ||
var _state$selection = state.selection, | ||
from = _state$selection.from, | ||
to = _state$selection.to; | ||
return dispatch(state.tr.removeMark(from, to, type)); | ||
}; | ||
} | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var index = $from.index(); | ||
function replaceText (range, type) { | ||
var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return function (state, dispatch) { | ||
var $from = state.selection.$from; | ||
var index = $from.index(); | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false; | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceWith(range.from, range.to, type.create(attrs))); | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceWith(range.from, range.to, type.create(attrs))); | ||
} | ||
return true | ||
} | ||
} | ||
return true; | ||
}; | ||
} | ||
function setInlineBlockType (type, attrs) { | ||
if ( attrs === void 0 ) attrs = {}; | ||
function setInlineBlockType (type) { | ||
var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return function (state, dispatch) { | ||
var $from = state.selection.$from; | ||
var index = $from.index(); | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var index = $from.index(); | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false; | ||
} | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceSelectionWith(type.create(attrs))); | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceSelectionWith(type.create(attrs))); | ||
} | ||
return true; | ||
}; | ||
} | ||
return true | ||
} | ||
} | ||
// this is a copy of canSplit | ||
// see https://github.com/ProseMirror/prosemirror-transform/blob/master/src/structure.js | ||
// Since this piece of code was "borrowed" from prosemirror, ESLint rules are ignored. | ||
// this is a copy of canSplit | ||
// see https://github.com/ProseMirror/prosemirror-transform/blob/master/src/structure.js | ||
/* eslint-disable max-len, no-plusplus, no-undef, eqeqeq */ | ||
function canSplit(doc, pos) { | ||
var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; | ||
var typesAfter = arguments.length > 3 ? arguments[3] : undefined; | ||
var $pos = doc.resolve(pos); | ||
var base = $pos.depth - depth; | ||
var innerType = typesAfter && typesAfter[typesAfter.length - 1] || $pos.parent; | ||
if (base < 0 || $pos.parent.type.spec.isolating || !$pos.parent.canReplace($pos.index(), $pos.parent.childCount) || !innerType.type.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount))) return false; | ||
// Since this piece of code was "borrowed" from prosemirror, ESLint rules are ignored. | ||
/* eslint-disable max-len, no-plusplus, no-undef, eqeqeq */ | ||
function canSplit(doc, pos, depth, typesAfter) { | ||
if ( depth === void 0 ) depth = 1; | ||
for (var d = $pos.depth - 1, i = depth - 2; d > base; d--, i--) { | ||
var node = $pos.node(d); | ||
var $pos = doc.resolve(pos); var | ||
base = $pos.depth - depth; | ||
var innerType = (typesAfter && typesAfter[typesAfter.length - 1]) || $pos.parent; | ||
if (base < 0 || $pos.parent.type.spec.isolating | ||
|| !$pos.parent.canReplace($pos.index(), $pos.parent.childCount) | ||
|| !innerType.type.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount))) { return false } | ||
for (var d = $pos.depth - 1, i = depth - 2; d > base; d--, i--) { | ||
var node = $pos.node(d); var | ||
index$1 = $pos.index(d); | ||
if (node.type.spec.isolating) { return false } | ||
var rest = node.content.cutByIndex(index$1, node.childCount); | ||
var after = (typesAfter && typesAfter[i]) || node; | ||
if (after != node) { rest = rest.replaceChild(0, after.type.create(after.attrs)); } | ||
var _index = $pos.index(d); | ||
/* Change starts from here */ | ||
// if (!node.canReplace(index + 1, node.childCount) || !after.type.validContent(rest)) | ||
// return false | ||
if (!node.canReplace(index$1 + 1, node.childCount)) { return false } | ||
/* Change ends here */ | ||
} | ||
var index = $pos.indexAfter(base); | ||
var baseType = typesAfter && typesAfter[0]; | ||
return $pos.node(base).canReplaceWith(index, index, baseType ? baseType.type : $pos.node(base + 1).type) | ||
} | ||
if (node.type.spec.isolating) return false; | ||
var rest = node.content.cutByIndex(_index, node.childCount); | ||
var after = typesAfter && typesAfter[i] || node; | ||
if (after != node) rest = rest.replaceChild(0, after.type.create(after.attrs)); | ||
/* Change starts from here */ | ||
// if (!node.canReplace(index + 1, node.childCount) || !after.type.validContent(rest)) | ||
// return false | ||
// this is a copy of splitListItem | ||
// see https://github.com/ProseMirror/prosemirror-schema-list/blob/master/src/schema-list.js | ||
if (!node.canReplace(_index + 1, node.childCount)) return false; | ||
/* Change ends here */ | ||
} | ||
function splitListItem(itemType) { | ||
return function _splitListItem(state, dispatch) { | ||
var ref = state.selection; | ||
var $from = ref.$from; | ||
var $to = ref.$to; | ||
var node = ref.node; | ||
if ((node && node.isBlock) || $from.depth < 2 || !$from.sameParent($to)) { return false } | ||
var 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 } | ||
var index = $pos.indexAfter(base); | ||
var baseType = typesAfter && typesAfter[0]; | ||
return $pos.node(base).canReplaceWith(index, index, baseType ? baseType.type : $pos.node(base + 1).type); | ||
} // this is a copy of splitListItem | ||
// see https://github.com/ProseMirror/prosemirror-schema-list/blob/master/src/schema-list.js | ||
if (dispatch) { | ||
var wrap = Fragment.empty; var | ||
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 = 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())); | ||
var tr$1 = state.tr.replace($from.before(keepItem ? null : -1), $from.after(-3), new 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.contentMatchAt($from.indexAfter(-1)).defaultType : null; | ||
var tr = state.tr.delete($from.pos, $to.pos); | ||
/* Change starts from here */ | ||
// let types = nextType && [null, {type: nextType}] | ||
var types = nextType && [{ type: itemType }, { type: nextType }]; | ||
if (!types) { types = [{ type: itemType }, null]; } | ||
/* Change ends here */ | ||
function splitListItem(itemType) { | ||
return function _splitListItem(state, dispatch) { | ||
var _state$selection = state.selection, | ||
$from = _state$selection.$from, | ||
$to = _state$selection.$to, | ||
node = _state$selection.node; | ||
if (node && node.isBlock || $from.depth < 2 || !$from.sameParent($to)) return false; | ||
var grandParent = $from.node(-1); | ||
if (grandParent.type != itemType) return false; | ||
if (!canSplit(tr.doc, $from.pos, 2, types)) { return false } | ||
if (dispatch) { dispatch(tr.split($from.pos, 2, [{ type: state.schema.nodes.todo_item, attrs: { done: false } }]).scrollIntoView()); } | ||
return true | ||
} | ||
} | ||
/* eslint-enable max-len, no-plusplus, no-undef, eqeqeq */ | ||
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; | ||
function toggleBlockType (type, toggletype, attrs) { | ||
if ( attrs === void 0 ) attrs = {}; | ||
if (dispatch) { | ||
var wrap = Fragment.empty; | ||
var 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 | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type, attrs); | ||
for (var 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 | ||
if (isActive) { | ||
return prosemirrorCommands.setBlockType(toggletype)(state, dispatch, view) | ||
} | ||
return prosemirrorCommands.setBlockType(type, attrs)(state, dispatch, view) | ||
} | ||
} | ||
wrap = wrap.append(Fragment.from(itemType.createAndFill())); | ||
function toggleList(type, itemType) { | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type); | ||
var _tr = state.tr.replace($from.before(keepItem ? null : -1), $from.after(-3), new Slice(wrap, keepItem ? 3 : 2, 2)); | ||
if (isActive) { | ||
return prosemirrorSchemaList.liftListItem(itemType)(state, dispatch, view) | ||
} | ||
_tr.setSelection(state.selection.constructor.near(_tr.doc.resolve($from.pos + (keepItem ? 3 : 2)))); | ||
return prosemirrorSchemaList.wrapInList(type)(state, dispatch, view) | ||
} | ||
} | ||
dispatch(_tr.scrollIntoView()); | ||
} | ||
// https://discuss.prosemirror.net/t/list-type-toggle/948 | ||
return true; | ||
} | ||
// import { wrapInList, liftListItem } from 'prosemirror-schema-list' | ||
var nextType = $to.pos == $from.end() ? grandParent.contentMatchAt($from.indexAfter(-1)).defaultType : null; | ||
var tr = state.tr.delete($from.pos, $to.pos); | ||
/* Change starts from here */ | ||
// let types = nextType && [null, {type: nextType}] | ||
// function isList(node, schema) { | ||
// return (node.type === schema.nodes.bullet_list || node.type === schema.nodes.ordered_list) | ||
// } | ||
var types = nextType && [{ | ||
type: itemType | ||
}, { | ||
type: nextType | ||
}]; | ||
if (!types) types = [{ | ||
type: itemType | ||
}, null]; | ||
/* Change ends here */ | ||
// export default function toggleList(listType, schema) { | ||
// const lift = liftListItem(schema.nodes.list_item) | ||
// const wrap = wrapInList(listType) | ||
if (!canSplit(tr.doc, $from.pos, 2, types)) return false; | ||
if (dispatch) dispatch(tr.split($from.pos, 2, [{ | ||
type: state.schema.nodes.todo_item, | ||
attrs: { | ||
done: false | ||
} | ||
}]).scrollIntoView()); | ||
return true; | ||
}; | ||
} | ||
/* eslint-enable max-len, no-plusplus, no-undef, eqeqeq */ | ||
// return (state, dispatch) => { | ||
// const { $from, $to } = state.selection | ||
// const range = $from.blockRange($to) | ||
// if (!range) { | ||
// return false | ||
// } | ||
function toggleBlockType (type, toggletype) { | ||
var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type, attrs); | ||
// if (range.depth >= 2 && $from.node(range.depth - 1).type === listType) { | ||
// return lift(state, dispatch) | ||
// } else if (range.depth >= 2 && isList($from.node(range.depth - 1), schema)) { | ||
// const tr = state.tr | ||
// const node = $from.before(range.depth - 1) | ||
// console.log({node}) | ||
// // TODO: how do I pass the node above to `setNodeType`? | ||
// // tr.setNodeType(range.start, listType); | ||
// if (dispatch) dispatch(tr) | ||
// return false | ||
// } else { | ||
// return wrap(state, dispatch) | ||
// } | ||
// } | ||
// } | ||
if (isActive) { | ||
return prosemirrorCommands.setBlockType(toggletype)(state, dispatch, view); | ||
} | ||
function toggleWrap (type) { | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type); | ||
return prosemirrorCommands.setBlockType(type, attrs)(state, dispatch, view); | ||
}; | ||
} | ||
if (isActive) { | ||
return prosemirrorCommands.lift(state, dispatch) | ||
} | ||
function toggleList(type, itemType) { | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type); | ||
return prosemirrorCommands.wrapIn(type)(state, dispatch, view) | ||
} | ||
} | ||
if (isActive) { | ||
return prosemirrorSchemaList.liftListItem(itemType)(state, dispatch, view); | ||
} | ||
function updateMark (type, attrs) { | ||
return function (state, dispatch) { | ||
var ref = state.selection; | ||
var from = ref.from; | ||
var to = ref.to; | ||
return dispatch(state.tr.addMark(from, to, type.create(attrs))) | ||
} | ||
} | ||
return prosemirrorSchemaList.wrapInList(type)(state, dispatch, view); | ||
}; | ||
} // https://discuss.prosemirror.net/t/list-type-toggle/948 | ||
// import { wrapInList, liftListItem } from 'prosemirror-schema-list' | ||
// function isList(node, schema) { | ||
// return (node.type === schema.nodes.bullet_list || node.type === schema.nodes.ordered_list) | ||
// } | ||
// export default function toggleList(listType, schema) { | ||
// const lift = liftListItem(schema.nodes.list_item) | ||
// const wrap = wrapInList(listType) | ||
// return (state, dispatch) => { | ||
// const { $from, $to } = state.selection | ||
// const range = $from.blockRange($to) | ||
// if (!range) { | ||
// return false | ||
// } | ||
// if (range.depth >= 2 && $from.node(range.depth - 1).type === listType) { | ||
// return lift(state, dispatch) | ||
// } else if (range.depth >= 2 && isList($from.node(range.depth - 1), schema)) { | ||
// const tr = state.tr | ||
// const node = $from.before(range.depth - 1) | ||
// console.log({node}) | ||
// // TODO: how do I pass the node above to `setNodeType`? | ||
// // tr.setNodeType(range.start, listType); | ||
// if (dispatch) dispatch(tr) | ||
// return false | ||
// } else { | ||
// return wrap(state, dispatch) | ||
// } | ||
// } | ||
// } | ||
exports.wrappingInputRule = prosemirrorInputrules.wrappingInputRule; | ||
exports.textblockTypeInputRule = prosemirrorInputrules.textblockTypeInputRule; | ||
exports.chainCommands = prosemirrorCommands.chainCommands; | ||
exports.deleteSelection = prosemirrorCommands.deleteSelection; | ||
exports.joinBackward = prosemirrorCommands.joinBackward; | ||
exports.selectNodeBackward = prosemirrorCommands.selectNodeBackward; | ||
exports.joinForward = prosemirrorCommands.joinForward; | ||
exports.selectNodeForward = prosemirrorCommands.selectNodeForward; | ||
exports.joinUp = prosemirrorCommands.joinUp; | ||
exports.joinDown = prosemirrorCommands.joinDown; | ||
exports.lift = prosemirrorCommands.lift; | ||
exports.newlineInCode = prosemirrorCommands.newlineInCode; | ||
exports.exitCode = prosemirrorCommands.exitCode; | ||
exports.createParagraphNear = prosemirrorCommands.createParagraphNear; | ||
exports.liftEmptyBlock = prosemirrorCommands.liftEmptyBlock; | ||
exports.splitBlock = prosemirrorCommands.splitBlock; | ||
exports.splitBlockKeepMarks = prosemirrorCommands.splitBlockKeepMarks; | ||
exports.selectParentNode = prosemirrorCommands.selectParentNode; | ||
exports.selectAll = prosemirrorCommands.selectAll; | ||
exports.wrapIn = prosemirrorCommands.wrapIn; | ||
exports.setBlockType = prosemirrorCommands.setBlockType; | ||
exports.toggleMark = prosemirrorCommands.toggleMark; | ||
exports.autoJoin = prosemirrorCommands.autoJoin; | ||
exports.baseKeymap = prosemirrorCommands.baseKeymap; | ||
exports.pcBaseKeymap = prosemirrorCommands.pcBaseKeymap; | ||
exports.macBaseKeymap = prosemirrorCommands.macBaseKeymap; | ||
exports.addListNodes = prosemirrorSchemaList.addListNodes; | ||
exports.wrapInList = prosemirrorSchemaList.wrapInList; | ||
exports.splitListItem = prosemirrorSchemaList.splitListItem; | ||
exports.liftListItem = prosemirrorSchemaList.liftListItem; | ||
exports.sinkListItem = prosemirrorSchemaList.sinkListItem; | ||
exports.insertText = insertText; | ||
exports.markInputRule = markInputRule; | ||
exports.removeMark = removeMark; | ||
exports.replaceText = replaceText; | ||
exports.setInlineBlockType = setInlineBlockType; | ||
exports.splitToDefaultListItem = splitListItem; | ||
exports.toggleBlockType = toggleBlockType; | ||
exports.toggleList = toggleList; | ||
exports.toggleWrap = toggleWrap; | ||
exports.updateMark = updateMark; | ||
function toggleWrap (type) { | ||
return function (state, dispatch, view) { | ||
var isActive = tiptapUtils.nodeIsActive(state, type); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
if (isActive) { | ||
return prosemirrorCommands.lift(state, dispatch); | ||
} | ||
return prosemirrorCommands.wrapIn(type)(state, dispatch, view); | ||
}; | ||
} | ||
function updateMark (type, attrs) { | ||
return function (state, dispatch) { | ||
var _state$selection = state.selection, | ||
from = _state$selection.from, | ||
to = _state$selection.to; | ||
return dispatch(state.tr.addMark(from, to, type.create(attrs))); | ||
}; | ||
} | ||
exports.wrappingInputRule = prosemirrorInputrules.wrappingInputRule; | ||
exports.textblockTypeInputRule = prosemirrorInputrules.textblockTypeInputRule; | ||
exports.addListNodes = prosemirrorSchemaList.addListNodes; | ||
exports.wrapInList = prosemirrorSchemaList.wrapInList; | ||
exports.splitListItem = prosemirrorSchemaList.splitListItem; | ||
exports.liftListItem = prosemirrorSchemaList.liftListItem; | ||
exports.sinkListItem = prosemirrorSchemaList.sinkListItem; | ||
exports.chainCommands = prosemirrorCommands.chainCommands; | ||
exports.deleteSelection = prosemirrorCommands.deleteSelection; | ||
exports.joinBackward = prosemirrorCommands.joinBackward; | ||
exports.selectNodeBackward = prosemirrorCommands.selectNodeBackward; | ||
exports.joinForward = prosemirrorCommands.joinForward; | ||
exports.selectNodeForward = prosemirrorCommands.selectNodeForward; | ||
exports.joinUp = prosemirrorCommands.joinUp; | ||
exports.joinDown = prosemirrorCommands.joinDown; | ||
exports.lift = prosemirrorCommands.lift; | ||
exports.newlineInCode = prosemirrorCommands.newlineInCode; | ||
exports.exitCode = prosemirrorCommands.exitCode; | ||
exports.createParagraphNear = prosemirrorCommands.createParagraphNear; | ||
exports.liftEmptyBlock = prosemirrorCommands.liftEmptyBlock; | ||
exports.splitBlock = prosemirrorCommands.splitBlock; | ||
exports.splitBlockKeepMarks = prosemirrorCommands.splitBlockKeepMarks; | ||
exports.selectParentNode = prosemirrorCommands.selectParentNode; | ||
exports.selectAll = prosemirrorCommands.selectAll; | ||
exports.wrapIn = prosemirrorCommands.wrapIn; | ||
exports.setBlockType = prosemirrorCommands.setBlockType; | ||
exports.toggleMark = prosemirrorCommands.toggleMark; | ||
exports.autoJoin = prosemirrorCommands.autoJoin; | ||
exports.baseKeymap = prosemirrorCommands.baseKeymap; | ||
exports.pcBaseKeymap = prosemirrorCommands.pcBaseKeymap; | ||
exports.macBaseKeymap = prosemirrorCommands.macBaseKeymap; | ||
exports.insertText = insertText; | ||
exports.markInputRule = markInputRule; | ||
exports.removeMark = removeMark; | ||
exports.replaceText = replaceText; | ||
exports.setInlineBlockType = setInlineBlockType; | ||
exports.splitToDefaultListItem = splitListItem; | ||
exports.toggleBlockType = toggleBlockType; | ||
exports.toggleList = toggleList; | ||
exports.toggleWrap = toggleWrap; | ||
exports.updateMark = updateMark; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
/*! | ||
* tiptap vundefined | ||
* (c) 2018 Scrumpy UG (limited liability) | ||
* @license MIT | ||
*/ | ||
/*! | ||
* tiptap-commands v0.7.1 | ||
* (c) 2018 Scrumpy UG (limited liability) | ||
* @license MIT | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("prosemirror-inputrules"),require("prosemirror-commands"),require("tiptap-utils"),require("prosemirror-schema-list")):"function"==typeof define&&define.amd?define(["exports","prosemirror-inputrules","prosemirror-commands","tiptap-utils","prosemirror-schema-list"],t):t(e.tiptap={},e.prosemirrorInputrules,e.prosemirrorCommands,e.tiptapUtils,e.prosemirrorSchemaList)}(this,function(e,t,a,p,i){"use strict";e.wrappingInputRule=t.wrappingInputRule,e.textblockTypeInputRule=t.textblockTypeInputRule,e.chainCommands=a.chainCommands,e.deleteSelection=a.deleteSelection,e.joinBackward=a.joinBackward,e.selectNodeBackward=a.selectNodeBackward,e.joinForward=a.joinForward,e.selectNodeForward=a.selectNodeForward,e.joinUp=a.joinUp,e.joinDown=a.joinDown,e.lift=a.lift,e.newlineInCode=a.newlineInCode,e.exitCode=a.exitCode,e.createParagraphNear=a.createParagraphNear,e.liftEmptyBlock=a.liftEmptyBlock,e.splitBlock=a.splitBlock,e.splitBlockKeepMarks=a.splitBlockKeepMarks,e.selectParentNode=a.selectParentNode,e.selectAll=a.selectAll,e.wrapIn=a.wrapIn,e.setBlockType=a.setBlockType,e.toggleMark=a.toggleMark,e.autoJoin=a.autoJoin,e.baseKeymap=a.baseKeymap,e.pcBaseKeymap=a.pcBaseKeymap,e.macBaseKeymap=a.macBaseKeymap,e.addListNodes=i.addListNodes,e.wrapInList=i.wrapInList,e.splitListItem=i.splitListItem,e.liftListItem=i.liftListItem,e.sinkListItem=i.sinkListItem,e.insertText=function(n){return void 0===n&&(n=""),function(e,t){var r=e.selection.$from.pos.pos;return t(e.tr.insertText(n,r)),!0}},e.markInputRule=function(e,s,d){return new t.InputRule(e,function(e,t,r,n){var o=d instanceof Function?d(t):d,i=e.tr,a=n;if(t[1]){var p=t[0].search(/\S/),c=r+t[0].indexOf(t[1]),l=c+t[1].length;l<n&&i.delete(l,n),r<c&&i.delete(r+p,c),a=r+p+t[1].length}return i.addMark(r,a,s.create(o)),i.removeStoredMark(s),i})},e.removeMark=function(i){return function(e,t){var r=e.selection,n=r.from,o=r.to;return t(e.tr.removeMark(n,o,i))}},e.replaceText=function(o,i,a){return void 0===a&&(a={}),function(e,t){var r=e.selection.$from,n=r.index();return!!r.parent.canReplaceWith(n,n,i)&&(t&&t(e.tr.replaceWith(o.from,o.to,i.create(a))),!0)}},e.setInlineBlockType=function(o,i){return void 0===i&&(i={}),function(e,t){var r=e.selection.$from,n=r.index();return!!r.parent.canReplaceWith(n,n,o)&&(t&&t(e.tr.replaceSelectionWith(o.create(i))),!0)}},e.splitToDefaultListItem=function(m){return function(e,t){var r=e.selection,n=r.$from,o=r.$to,i=r.node;if(i&&i.isBlock||n.depth<2||!n.sameParent(o))return!1;var a=n.node(-1);if(a.type!=m)return!1;if(0==n.parent.content.size){if(2==n.depth||n.node(-3).type!=m||n.index(-2)!=n.node(-2).childCount-1)return!1;if(t){for(var p=Fragment.empty,c=0<n.index(-1),l=n.depth-(c?1:2);l>=n.depth-3;l--)p=Fragment.from(n.node(l).copy(p));p=p.append(Fragment.from(m.createAndFill()));var s=e.tr.replace(n.before(c?null:-1),n.after(-3),new Slice(p,c?3:2,2));s.setSelection(e.selection.constructor.near(s.doc.resolve(n.pos+(c?3:2)))),t(s.scrollIntoView())}return!0}var d=o.pos==n.end()?a.contentMatchAt(n.indexAfter(-1)).defaultType:null,u=e.tr.delete(n.pos,o.pos),f=d&&[{type:m},{type:d}];return f||(f=[{type:m},null]),!!function(e,t,r,n){void 0===r&&(r=1);var o=e.resolve(t),i=o.depth-r,a=n&&n[n.length-1]||o.parent;if(i<0||o.parent.type.spec.isolating||!o.parent.canReplace(o.index(),o.parent.childCount)||!a.type.validContent(o.parent.content.cutByIndex(o.index(),o.parent.childCount)))return!1;for(var p=o.depth-1,c=r-2;i<p;p--,c--){var l=o.node(p),s=o.index(p);if(l.type.spec.isolating)return!1;var d=l.content.cutByIndex(s,l.childCount),u=n&&n[c]||l;if(u!=l&&(d=d.replaceChild(0,u.type.create(u.attrs))),!l.canReplace(s+1,l.childCount))return!1}var f=o.indexAfter(i),m=n&&n[0];return o.node(i).canReplaceWith(f,f,m?m.type:o.node(i+1).type)}(u.doc,n.pos,2,f)&&(t&&t(u.split(n.pos,2,[{type:e.schema.nodes.todo_item,attrs:{done:!1}}]).scrollIntoView()),!0)}},e.toggleBlockType=function(n,o,i){return void 0===i&&(i={}),function(e,t,r){return p.nodeIsActive(e,n,i)?a.setBlockType(o)(e,t,r):a.setBlockType(n,i)(e,t,r)}},e.toggleList=function(n,o){return function(e,t,r){return p.nodeIsActive(e,n)?i.liftListItem(o)(e,t,r):i.wrapInList(n)(e,t,r)}},e.toggleWrap=function(n){return function(e,t,r){return p.nodeIsActive(e,n)?a.lift(e,t):a.wrapIn(n)(e,t,r)}},e.updateMark=function(i,a){return function(e,t){var r=e.selection,n=r.from,o=r.to;return t(e.tr.addMark(n,o,i.create(a)))}},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("prosemirror-inputrules"),require("prosemirror-schema-list"),require("prosemirror-commands"),require("tiptap-utils")):"function"==typeof define&&define.amd?define(["exports","prosemirror-inputrules","prosemirror-schema-list","prosemirror-commands","tiptap-utils"],t):t(e.tiptap={},e.prosemirrorInputrules,e.prosemirrorSchemaList,e.prosemirrorCommands,e.tiptapUtils)}(this,function(e,t,i,a,l){"use strict";e.wrappingInputRule=t.wrappingInputRule,e.textblockTypeInputRule=t.textblockTypeInputRule,e.addListNodes=i.addListNodes,e.wrapInList=i.wrapInList,e.splitListItem=i.splitListItem,e.liftListItem=i.liftListItem,e.sinkListItem=i.sinkListItem,e.chainCommands=a.chainCommands,e.deleteSelection=a.deleteSelection,e.joinBackward=a.joinBackward,e.selectNodeBackward=a.selectNodeBackward,e.joinForward=a.joinForward,e.selectNodeForward=a.selectNodeForward,e.joinUp=a.joinUp,e.joinDown=a.joinDown,e.lift=a.lift,e.newlineInCode=a.newlineInCode,e.exitCode=a.exitCode,e.createParagraphNear=a.createParagraphNear,e.liftEmptyBlock=a.liftEmptyBlock,e.splitBlock=a.splitBlock,e.splitBlockKeepMarks=a.splitBlockKeepMarks,e.selectParentNode=a.selectParentNode,e.selectAll=a.selectAll,e.wrapIn=a.wrapIn,e.setBlockType=a.setBlockType,e.toggleMark=a.toggleMark,e.autoJoin=a.autoJoin,e.baseKeymap=a.baseKeymap,e.pcBaseKeymap=a.pcBaseKeymap,e.macBaseKeymap=a.macBaseKeymap,e.insertText=function(){var n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";return function(e,t){var r=e.selection.$from.pos.pos;return t(e.tr.insertText(n,r)),!0}},e.markInputRule=function(e,s,d){return new t.InputRule(e,function(e,t,r,n){var o=d instanceof Function?d(t):d,i=e.tr,a=n;if(t[1]){var l=t[0].search(/\S/),p=r+t[0].indexOf(t[1]),c=p+t[1].length;c<n&&i.delete(c,n),r<p&&i.delete(r+l,p),a=r+l+t[1].length}return i.addMark(r,a,s.create(o)),i.removeStoredMark(s),i})},e.removeMark=function(i){return function(e,t){var r=e.selection,n=r.from,o=r.to;return t(e.tr.removeMark(n,o,i))}},e.replaceText=function(o,i){var a=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return function(e,t){var r=e.selection.$from,n=r.index();return!!r.parent.canReplaceWith(n,n,i)&&(t&&t(e.tr.replaceWith(o.from,o.to,i.create(a))),!0)}},e.setInlineBlockType=function(o){var i=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};return function(e,t){var r=e.selection.$from,n=r.index();return!!r.parent.canReplaceWith(n,n,o)&&(t&&t(e.tr.replaceSelectionWith(o.create(i))),!0)}},e.splitToDefaultListItem=function(m){return function(e,t){var r=e.selection,n=r.$from,o=r.$to,i=r.node;if(i&&i.isBlock||n.depth<2||!n.sameParent(o))return!1;var a=n.node(-1);if(a.type!=m)return!1;if(0==n.parent.content.size){if(2==n.depth||n.node(-3).type!=m||n.index(-2)!=n.node(-2).childCount-1)return!1;if(t){for(var l=Fragment.empty,p=0<n.index(-1),c=n.depth-(p?1:2);c>=n.depth-3;c--)l=Fragment.from(n.node(c).copy(l));l=l.append(Fragment.from(m.createAndFill()));var s=e.tr.replace(n.before(p?null:-1),n.after(-3),new Slice(l,p?3:2,2));s.setSelection(e.selection.constructor.near(s.doc.resolve(n.pos+(p?3:2)))),t(s.scrollIntoView())}return!0}var d=o.pos==n.end()?a.contentMatchAt(n.indexAfter(-1)).defaultType:null,u=e.tr.delete(n.pos,o.pos),f=d&&[{type:m},{type:d}];return f||(f=[{type:m},null]),!!function(e,t){var r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:1,n=3<arguments.length?arguments[3]:void 0,o=e.resolve(t),i=o.depth-r,a=n&&n[n.length-1]||o.parent;if(i<0||o.parent.type.spec.isolating||!o.parent.canReplace(o.index(),o.parent.childCount)||!a.type.validContent(o.parent.content.cutByIndex(o.index(),o.parent.childCount)))return!1;for(var l=o.depth-1,p=r-2;i<l;l--,p--){var c=o.node(l),s=o.index(l);if(c.type.spec.isolating)return!1;var d=c.content.cutByIndex(s,c.childCount),u=n&&n[p]||c;if(u!=c&&(d=d.replaceChild(0,u.type.create(u.attrs))),!c.canReplace(s+1,c.childCount))return!1}var f=o.indexAfter(i),m=n&&n[0];return o.node(i).canReplaceWith(f,f,m?m.type:o.node(i+1).type)}(u.doc,n.pos,2,f)&&(t&&t(u.split(n.pos,2,[{type:e.schema.nodes.todo_item,attrs:{done:!1}}]).scrollIntoView()),!0)}},e.toggleBlockType=function(n,o){var i=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return function(e,t,r){return l.nodeIsActive(e,n,i)?a.setBlockType(o)(e,t,r):a.setBlockType(n,i)(e,t,r)}},e.toggleList=function(n,o){return function(e,t,r){return l.nodeIsActive(e,n)?i.liftListItem(o)(e,t,r):i.wrapInList(n)(e,t,r)}},e.toggleWrap=function(n){return function(e,t,r){return l.nodeIsActive(e,n)?a.lift(e,t):a.wrapIn(n)(e,t,r)}},e.updateMark=function(i,a){return function(e,t){var r=e.selection,n=r.from,o=r.to;return t(e.tr.addMark(n,o,i.create(a)))}},Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "tiptap-commands", | ||
"version": "0.7.1", | ||
"version": "1.0.0", | ||
"description": "Commands for tiptap", | ||
@@ -26,5 +26,5 @@ "homepage": "https://tiptap.scrumpy.io", | ||
"prosemirror-schema-list": "^1.0.1", | ||
"tiptap-utils": "^0.4.1" | ||
"tiptap-utils": "^1.0.0" | ||
}, | ||
"gitHead": "d90839a74af73406ce1ea9aebc350da683d84fa1" | ||
"gitHead": "eb7fe0c837b6d5c432e8bf815674ee6fa84205fb" | ||
} |
@@ -7,2 +7,2 @@ # tiptap-commands | ||
[![](https://img.shields.io/npm/l/tiptap-commands.svg)](https://www.npmjs.com/package/tiptap-commands) | ||
[![](http://img.badgesize.io/https://unpkg.com/tiptap-commands/dist/commands.min.js?compression=gzip&label=size&colorB=000000)](https://www.npmjs.com/package/tiptap-commands) | ||
[![](http://img.badgesize.io/https://unpkg.com/tiptap-commands/dist/commands.min.js?compression=gzip&label=size&colorB=000000)](https://www.npmjs.com/package/tiptap-commands) |
export default function (text = '') { | ||
return (state, dispatch) => { | ||
const { $from } = state.selection | ||
const { pos } = $from.pos | ||
return (state, dispatch) => { | ||
const { $from } = state.selection | ||
const { pos } = $from.pos | ||
dispatch(state.tr.insertText(text, pos)) | ||
dispatch(state.tr.insertText(text, pos)) | ||
return true | ||
} | ||
return true | ||
} | ||
} |
import { InputRule } from 'prosemirror-inputrules' | ||
export default function (regexp, markType, getAttrs) { | ||
return new InputRule(regexp, (state, match, start, end) => { | ||
const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs | ||
return new InputRule(regexp, (state, match, start, end) => { | ||
const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs | ||
const { tr } = state | ||
let markEnd = end | ||
if (match[1]) { | ||
const startSpaces = match[0].search(/\S/) | ||
const textStart = start + match[0].indexOf(match[1]) | ||
const textEnd = textStart + match[1].length | ||
if (textEnd < end) { | ||
tr.delete(textEnd, end) | ||
} | ||
if (textStart > start) { | ||
tr.delete(start + startSpaces, textStart) | ||
} | ||
markEnd = start + startSpaces + match[1].length | ||
} | ||
if (match[1]) { | ||
const startSpaces = match[0].search(/\S/) | ||
const textStart = start + match[0].indexOf(match[1]) | ||
const textEnd = textStart + match[1].length | ||
if (textEnd < end) { | ||
tr.delete(textEnd, end) | ||
} | ||
if (textStart > start) { | ||
tr.delete(start + startSpaces, textStart) | ||
} | ||
markEnd = start + startSpaces + match[1].length | ||
} | ||
tr.addMark(start, markEnd, markType.create(attrs)) | ||
tr.removeStoredMark(markType) // Do not continue with mark. | ||
return tr | ||
}) | ||
tr.addMark(start, markEnd, markType.create(attrs)) | ||
tr.removeStoredMark(markType) // Do not continue with mark. | ||
return tr | ||
}) | ||
} |
export default function (type) { | ||
return (state, dispatch) => { | ||
const { from, to } = state.selection | ||
return dispatch(state.tr.removeMark(from, to, type)) | ||
} | ||
const { from, to } = state.selection | ||
return dispatch(state.tr.removeMark(from, to, type)) | ||
} | ||
} |
export default function (range, type, attrs = {}) { | ||
return (state, dispatch) => { | ||
const { $from } = state.selection | ||
const index = $from.index() | ||
return (state, dispatch) => { | ||
const { $from } = state.selection | ||
const index = $from.index() | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceWith(range.from, range.to, type.create(attrs))) | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceWith(range.from, range.to, type.create(attrs))) | ||
} | ||
return true | ||
} | ||
return true | ||
} | ||
} |
export default function (type, attrs = {}) { | ||
return (state, dispatch) => { | ||
const { $from } = state.selection | ||
const index = $from.index() | ||
return (state, dispatch) => { | ||
const { $from } = state.selection | ||
const index = $from.index() | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (!$from.parent.canReplaceWith(index, index, type)) { | ||
return false | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceSelectionWith(type.create(attrs))) | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.replaceSelectionWith(type.create(attrs))) | ||
} | ||
return true | ||
} | ||
return true | ||
} | ||
} |
@@ -19,9 +19,9 @@ // this is a copy of canSplit | ||
const after = (typesAfter && typesAfter[i]) || node | ||
if (after != node) rest = rest.replaceChild(0, after.type.create(after.attrs)) | ||
if (after != node) rest = rest.replaceChild(0, after.type.create(after.attrs)) | ||
/* Change starts from here */ | ||
// if (!node.canReplace(index + 1, node.childCount) || !after.type.validContent(rest)) | ||
/* Change starts from here */ | ||
// if (!node.canReplace(index + 1, node.childCount) || !after.type.validContent(rest)) | ||
// return false | ||
if (!node.canReplace(index + 1, node.childCount)) return false | ||
/* Change ends here */ | ||
/* Change ends here */ | ||
} | ||
@@ -47,3 +47,3 @@ const index = $pos.indexAfter(base) | ||
if ($from.depth == 2 || $from.node(-3).type != itemType | ||
|| $from.index(-2) != $from.node(-2).childCount - 1) return false | ||
|| $from.index(-2) != $from.node(-2).childCount - 1) return false | ||
@@ -57,3 +57,3 @@ if (dispatch) { | ||
// Add a second list item with an empty default start node | ||
wrap = wrap.append(Fragment.from(itemType.createAndFill())) | ||
wrap = wrap.append(Fragment.from(itemType.createAndFill())) | ||
const tr = state.tr.replace($from.before(keepItem ? null : -1), $from.after(-3), new Slice(wrap, keepItem ? 3 : 2, 2)) | ||
@@ -65,12 +65,12 @@ tr.setSelection(state.selection.constructor.near(tr.doc.resolve($from.pos + (keepItem ? 3 : 2)))) | ||
} | ||
const nextType = $to.pos == $from.end() ? grandParent.contentMatchAt($from.indexAfter(-1)).defaultType : null | ||
const tr = state.tr.delete($from.pos, $to.pos) | ||
const nextType = $to.pos == $from.end() ? grandParent.contentMatchAt($from.indexAfter(-1)).defaultType : null | ||
const tr = state.tr.delete($from.pos, $to.pos) | ||
/* Change starts from here */ | ||
// let types = nextType && [null, {type: nextType}] | ||
/* Change starts from here */ | ||
// let types = nextType && [null, {type: nextType}] | ||
let types = nextType && [{ type: itemType }, { type: nextType }] | ||
if (!types) types = [{ type: itemType }, null] | ||
/* Change ends here */ | ||
/* Change ends here */ | ||
if (!canSplit(tr.doc, $from.pos, 2, types)) return false | ||
if (!canSplit(tr.doc, $from.pos, 2, types)) return false | ||
if (dispatch) dispatch(tr.split($from.pos, 2, [{ type: state.schema.nodes.todo_item, attrs: { done: false } }]).scrollIntoView()) | ||
@@ -77,0 +77,0 @@ return true |
@@ -5,11 +5,11 @@ import { setBlockType } from 'prosemirror-commands' | ||
export default function (type, toggletype, attrs = {}) { | ||
return (state, dispatch, view) => { | ||
const isActive = nodeIsActive(state, type, attrs) | ||
return (state, dispatch, view) => { | ||
const isActive = nodeIsActive(state, type, attrs) | ||
if (isActive) { | ||
return setBlockType(toggletype)(state, dispatch, view) | ||
} | ||
if (isActive) { | ||
return setBlockType(toggletype)(state, dispatch, view) | ||
} | ||
return setBlockType(type, attrs)(state, dispatch, view) | ||
} | ||
return setBlockType(type, attrs)(state, dispatch, view) | ||
} | ||
} |
@@ -5,11 +5,11 @@ import { wrapIn, lift } from 'prosemirror-commands' | ||
export default function (type) { | ||
return (state, dispatch, view) => { | ||
const isActive = nodeIsActive(state, type) | ||
return (state, dispatch, view) => { | ||
const isActive = nodeIsActive(state, type) | ||
if (isActive) { | ||
return lift(state, dispatch) | ||
} | ||
if (isActive) { | ||
return lift(state, dispatch) | ||
} | ||
return wrapIn(type)(state, dispatch, view) | ||
} | ||
return wrapIn(type)(state, dispatch, view) | ||
} | ||
} |
export default function (type, attrs) { | ||
return (state, dispatch) => { | ||
const { from, to } = state.selection | ||
return dispatch(state.tr.addMark(from, to, type.create(attrs))) | ||
} | ||
const { from, to } = state.selection | ||
return dispatch(state.tr.addMark(from, to, type.create(attrs))) | ||
} | ||
} |
152
src/index.js
import { | ||
chainCommands, | ||
deleteSelection, | ||
joinBackward, | ||
selectNodeBackward, | ||
joinForward, | ||
selectNodeForward, | ||
joinUp, | ||
joinDown, | ||
lift, | ||
newlineInCode, | ||
exitCode, | ||
createParagraphNear, | ||
liftEmptyBlock, | ||
splitBlock, | ||
splitBlockKeepMarks, | ||
selectParentNode, | ||
selectAll, | ||
wrapIn, | ||
setBlockType, | ||
toggleMark, | ||
autoJoin, | ||
baseKeymap, | ||
pcBaseKeymap, | ||
macBaseKeymap, | ||
chainCommands, | ||
deleteSelection, | ||
joinBackward, | ||
selectNodeBackward, | ||
joinForward, | ||
selectNodeForward, | ||
joinUp, | ||
joinDown, | ||
lift, | ||
newlineInCode, | ||
exitCode, | ||
createParagraphNear, | ||
liftEmptyBlock, | ||
splitBlock, | ||
splitBlockKeepMarks, | ||
selectParentNode, | ||
selectAll, | ||
wrapIn, | ||
setBlockType, | ||
toggleMark, | ||
autoJoin, | ||
baseKeymap, | ||
pcBaseKeymap, | ||
macBaseKeymap, | ||
} from 'prosemirror-commands' | ||
import { | ||
addListNodes, | ||
wrapInList, | ||
splitListItem, | ||
liftListItem, | ||
sinkListItem, | ||
addListNodes, | ||
wrapInList, | ||
splitListItem, | ||
liftListItem, | ||
sinkListItem, | ||
} from 'prosemirror-schema-list' | ||
import { | ||
wrappingInputRule, | ||
textblockTypeInputRule, | ||
wrappingInputRule, | ||
textblockTypeInputRule, | ||
} from 'prosemirror-inputrules' | ||
@@ -53,50 +53,50 @@ | ||
export { | ||
// prosemirror-commands | ||
chainCommands, | ||
deleteSelection, | ||
joinBackward, | ||
selectNodeBackward, | ||
joinForward, | ||
selectNodeForward, | ||
joinUp, | ||
joinDown, | ||
lift, | ||
newlineInCode, | ||
exitCode, | ||
createParagraphNear, | ||
liftEmptyBlock, | ||
splitBlock, | ||
splitBlockKeepMarks, | ||
selectParentNode, | ||
selectAll, | ||
wrapIn, | ||
setBlockType, | ||
toggleMark, | ||
autoJoin, | ||
baseKeymap, | ||
pcBaseKeymap, | ||
macBaseKeymap, | ||
// prosemirror-commands | ||
chainCommands, | ||
deleteSelection, | ||
joinBackward, | ||
selectNodeBackward, | ||
joinForward, | ||
selectNodeForward, | ||
joinUp, | ||
joinDown, | ||
lift, | ||
newlineInCode, | ||
exitCode, | ||
createParagraphNear, | ||
liftEmptyBlock, | ||
splitBlock, | ||
splitBlockKeepMarks, | ||
selectParentNode, | ||
selectAll, | ||
wrapIn, | ||
setBlockType, | ||
toggleMark, | ||
autoJoin, | ||
baseKeymap, | ||
pcBaseKeymap, | ||
macBaseKeymap, | ||
// prosemirror-schema-list | ||
addListNodes, | ||
wrapInList, | ||
splitListItem, | ||
liftListItem, | ||
sinkListItem, | ||
// prosemirror-schema-list | ||
addListNodes, | ||
wrapInList, | ||
splitListItem, | ||
liftListItem, | ||
sinkListItem, | ||
// prosemirror-inputrules | ||
wrappingInputRule, | ||
textblockTypeInputRule, | ||
// prosemirror-inputrules | ||
wrappingInputRule, | ||
textblockTypeInputRule, | ||
// custom | ||
insertText, | ||
markInputRule, | ||
removeMark, | ||
replaceText, | ||
setInlineBlockType, | ||
splitToDefaultListItem, | ||
toggleBlockType, | ||
toggleList, | ||
toggleWrap, | ||
updateMark, | ||
// custom | ||
insertText, | ||
markInputRule, | ||
removeMark, | ||
replaceText, | ||
setInlineBlockType, | ||
splitToDefaultListItem, | ||
toggleBlockType, | ||
toggleList, | ||
toggleWrap, | ||
updateMark, | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
51422
1055
2
8
+ Addedprosemirror-tables@1.6.3(transitive)
+ Addedtiptap-utils@1.13.1(transitive)
- Removedprosemirror-tables@0.6.50.7.11(transitive)
- Removedprosemirror-utils@0.6.7(transitive)
- Removedtiptap-utils@0.4.1(transitive)
Updatedtiptap-utils@^1.0.0