add commands and utils packages

This commit is contained in:
Philipp Kühn
2018-08-23 10:33:49 +02:00
parent 14cb2ef085
commit d5039489dd
12 changed files with 8354 additions and 24 deletions

View File

@@ -83,6 +83,62 @@ export default [
format: 'es',
external: ['vue'],
},
{
input: resolve('packages/tiptap-commands/src/index.js'),
file: resolve('packages/tiptap-commands/dist/commands.js'),
format: 'umd',
env: 'development',
external: [],
},
{
input: resolve('packages/tiptap-commands/src/index.js'),
file: resolve('packages/tiptap-commands/dist/commands.min.js'),
format: 'umd',
env: 'production',
external: [],
},
{
input: resolve('packages/tiptap-commands/src/index.js'),
file: resolve('packages/tiptap-commands/dist/commands.common.js'),
format: 'cjs',
external: [],
},
{
input: resolve('packages/tiptap-commands/src/index.js'),
file: resolve('packages/tiptap-commands/dist/commands.esm.js'),
format: 'es',
external: [],
},
{
input: resolve('packages/tiptap-utils/src/index.js'),
file: resolve('packages/tiptap-utils/dist/utils.js'),
format: 'umd',
env: 'development',
external: [],
},
{
input: resolve('packages/tiptap-utils/src/index.js'),
file: resolve('packages/tiptap-utils/dist/utils.min.js'),
format: 'umd',
env: 'production',
external: [],
},
{
input: resolve('packages/tiptap-utils/src/index.js'),
file: resolve('packages/tiptap-utils/dist/utils.common.js'),
format: 'cjs',
external: [],
},
{
input: resolve('packages/tiptap-utils/src/index.js'),
file: resolve('packages/tiptap-utils/dist/utils.esm.js'),
format: 'es',
external: [],
},
// {
// input: resolve('packages/tiptap/src/helpers/index.js'),
// file: resolve('packages/tiptap/dist/helpers.min.js'),

View File

@@ -0,0 +1,27 @@
{
"name": "tiptap-commands",
"version": "0.1.0",
"description": "Commands for tiptap",
"homepage": "https://tiptap.scrumpy.io",
"license": "MIT",
"main": "dist/commands.common.js",
"module": "dist/commands.esm.js",
"unpkg": "dist/commands.js",
"jsdelivr": "dist/commands.js",
"files": [
"src",
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/heyscrumpy/tiptap.git"
},
"bugs": {
"url": "https://github.com/heyscrumpy/tiptap/issues"
},
"dependencies": {
"prosemirror-commands": "^1.0.7",
"prosemirror-inputrules": "^1.0.1",
"prosemirror-schema-list": "^1.0.1"
}
}

View File

@@ -0,0 +1,6 @@
export default function (type) {
return (state, dispatch) => {
const { from, to } = state.selection
return dispatch(state.tr.removeMark(from, to, type))
}
}

View File

@@ -0,0 +1,14 @@
import { setBlockType } from 'prosemirror-commands'
import { nodeIsActive } from '../utils'
export default function (type, toggletype, attrs = {}) {
return (state, dispatch, view) => {
const isActive = nodeIsActive(state, type, attrs)
if (isActive) {
return setBlockType(toggletype)(state, dispatch, view)
}
return setBlockType(type, attrs)(state, dispatch, view)
}
}

View File

@@ -0,0 +1,49 @@
import { nodeIsActive } from '../utils'
import { wrapInList, liftListItem } from '..'
export default function toggleList(type, itemType) {
return (state, dispatch, view) => {
const isActive = nodeIsActive(state, type)
if (isActive) {
return liftListItem(itemType)(state, dispatch, view)
}
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) => {
// 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)
// }
// }
// }

View File

@@ -0,0 +1,6 @@
export default function (type, attrs) {
return (state, dispatch) => {
const { from, to } = state.selection
return dispatch(state.tr.addMark(from, to, type.create(attrs)))
}
}

View File

@@ -0,0 +1,89 @@
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,
} from 'prosemirror-commands'
import {
addListNodes,
wrapInList,
splitListItem,
liftListItem,
sinkListItem,
} from 'prosemirror-schema-list'
import {
wrappingInputRule,
textblockTypeInputRule,
} from 'prosemirror-inputrules'
import removeMark from './helpers/removeMark'
import toggleBlockType from './helpers/toggleBlockType'
import toggleList from './helpers/toggleList'
import updateMark from './helpers/updateMark'
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-schema-list
addListNodes,
wrapInList,
splitListItem,
liftListItem,
sinkListItem,
// prosemirror-inputrules
wrappingInputRule,
textblockTypeInputRule,
// custom
removeMark,
toggleBlockType,
toggleList,
updateMark,
}

View File

@@ -0,0 +1,26 @@
{
"name": "tiptap-utils",
"version": "0.1.0",
"description": "Utility functions for tiptap",
"homepage": "https://tiptap.scrumpy.io",
"license": "MIT",
"main": "dist/utils.common.js",
"module": "dist/utils.esm.js",
"unpkg": "dist/utils.js",
"jsdelivr": "dist/utils.js",
"files": [
"src",
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/heyscrumpy/tiptap.git"
},
"bugs": {
"url": "https://github.com/heyscrumpy/tiptap/issues"
},
"dependencies": {
"prosemirror-utils": "^0.6.5",
"prosemirror-tables": "^0.7.6"
}
}

View File

@@ -0,0 +1,2 @@
export { default as markIsActive } from './utils/markIsActive'
export { default as nodeIsActive } from './utils/nodeIsActive'

View File

@@ -0,0 +1,14 @@
export default function (state, type) {
const {
from,
$from,
to,
empty,
} = state.selection
if (empty) {
return !!type.isInSet(state.storedMarks || $from.marks())
}
return !!state.doc.rangeHasMark(from, to, type)
}

View File

@@ -0,0 +1,12 @@
import { findParentNode } from 'prosemirror-utils'
export default function (state, type, attrs) {
const predicate = node => node.type === type
const parent = findParentNode(predicate)(state.selection)
if (attrs === {} || !parent) {
return !!parent
}
return parent.node.hasMarkup(type, attrs)
}

File diff suppressed because it is too large Load Diff