remove prosemirror-utils dependency
This commit is contained in:
@@ -22,7 +22,6 @@
|
||||
"dependencies": {
|
||||
"prosemirror-model": "^1.13.1",
|
||||
"prosemirror-state": "^1.3.3",
|
||||
"prosemirror-tables": "^1.1.1",
|
||||
"prosemirror-utils": "^0.9.6"
|
||||
"prosemirror-tables": "^1.1.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
export { default as createCell } from './utils/createCell'
|
||||
export { default as createTable } from './utils/createTable'
|
||||
export { default as equalNodeType } from './utils/equalNodeType'
|
||||
export { default as findBlockNodes } from './utils/findBlockNodes'
|
||||
export { default as findChildren } from './utils/findChildren'
|
||||
export { default as findParentNode } from './utils/findParentNode'
|
||||
export { default as findParentNodeClosestToPos } from './utils/findParentNodeClosestToPos'
|
||||
export { default as findSelectedNodeOfType } from './utils/findSelectedNodeOfType'
|
||||
export { default as flatten } from './utils/flatten'
|
||||
export { default as getMarkAttrs } from './utils/getMarkAttrs'
|
||||
export { default as getNodeAttrs } from './utils/getNodeAttrs'
|
||||
export { default as getMarkRange } from './utils/getMarkRange'
|
||||
export { default as getNodeAttrs } from './utils/getNodeAttrs'
|
||||
export { default as getTableNodeTypes } from './utils/getTableNodeTypes'
|
||||
export { default as isNodeSelection } from './utils/isNodeSelection'
|
||||
export { default as markIsActive } from './utils/markIsActive'
|
||||
export { default as nodeEqualsType } from './utils/nodeEqualsType'
|
||||
export { default as nodeIsActive } from './utils/nodeIsActive'
|
||||
|
||||
7
packages/tiptap-utils/src/utils/createCell.js
Normal file
7
packages/tiptap-utils/src/utils/createCell.js
Normal file
@@ -0,0 +1,7 @@
|
||||
export default function createCell(cellType, cellContent) {
|
||||
if (cellContent) {
|
||||
return cellType.createChecked(null, cellContent)
|
||||
}
|
||||
|
||||
return cellType.createAndFill()
|
||||
}
|
||||
33
packages/tiptap-utils/src/utils/createTable.js
Normal file
33
packages/tiptap-utils/src/utils/createTable.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import createCell from './createCell'
|
||||
import getTableNodeTypes from './getTableNodeTypes'
|
||||
|
||||
export default function createTable(schema, rowsCount, colsCount, withHeaderRow, cellContent) {
|
||||
const types = getTableNodeTypes(schema)
|
||||
|
||||
const headerCells = []
|
||||
const cells = []
|
||||
|
||||
for (let index = 0; index < colsCount; index += 1) {
|
||||
const cell = createCell(types.cell, cellContent)
|
||||
|
||||
if (cell) {
|
||||
cells.push(cell)
|
||||
}
|
||||
|
||||
if (withHeaderRow) {
|
||||
const headerCell = createCell(types.header_cell, cellContent)
|
||||
|
||||
if (headerCell) {
|
||||
headerCells.push(headerCell)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const rows = []
|
||||
|
||||
for (let index = 0; index < rowsCount; index += 1) {
|
||||
rows.push(types.row.createChecked(null, withHeaderRow && index === 0 ? headerCells : cells))
|
||||
}
|
||||
|
||||
return types.table.createChecked(null, rows)
|
||||
}
|
||||
3
packages/tiptap-utils/src/utils/equalNodeType.js
Normal file
3
packages/tiptap-utils/src/utils/equalNodeType.js
Normal file
@@ -0,0 +1,3 @@
|
||||
export default function equalNodeType(nodeType, node) {
|
||||
return Array.isArray(nodeType) && (nodeType.indexOf(node.type) > -1 || node.type === nodeType)
|
||||
}
|
||||
5
packages/tiptap-utils/src/utils/findBlockNodes.js
Normal file
5
packages/tiptap-utils/src/utils/findBlockNodes.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import findChildren from './findChildren'
|
||||
|
||||
export default function findBlockNodes(node, descend) {
|
||||
return findChildren(node, child => child.isBlock, descend)
|
||||
}
|
||||
10
packages/tiptap-utils/src/utils/findChildren.js
Normal file
10
packages/tiptap-utils/src/utils/findChildren.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import flatten from './flatten'
|
||||
|
||||
export default function findChildren(node, predicate, descend) {
|
||||
if (!node) {
|
||||
throw new Error('Invalid "node" parameter')
|
||||
} else if (!predicate) {
|
||||
throw new Error('Invalid "predicate" parameter')
|
||||
}
|
||||
return flatten(node, descend).filter(child => predicate(child.node))
|
||||
}
|
||||
5
packages/tiptap-utils/src/utils/findParentNode.js
Normal file
5
packages/tiptap-utils/src/utils/findParentNode.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import findParentNodeClosestToPos from './findParentNodeClosestToPos'
|
||||
|
||||
export default function findParentNode(predicate) {
|
||||
return selection => findParentNodeClosestToPos(selection.$from, predicate)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// eslint-disable-next-line
|
||||
export default function findParentNodeClosestToPos($pos, predicate) {
|
||||
for (let i = $pos.depth; i > 0; i -= 1) {
|
||||
const node = $pos.node(i)
|
||||
|
||||
if (predicate(node)) {
|
||||
return {
|
||||
pos: i > 0 ? $pos.before(i) : 0,
|
||||
start: $pos.start(i),
|
||||
depth: i,
|
||||
node,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
16
packages/tiptap-utils/src/utils/findSelectedNodeOfType.js
Normal file
16
packages/tiptap-utils/src/utils/findSelectedNodeOfType.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import isNodeSelection from './isNodeSelection'
|
||||
import equalNodeType from './equalNodeType'
|
||||
|
||||
export default function findSelectedNodeOfType(nodeType) {
|
||||
// eslint-disable-next-line
|
||||
return function (selection) {
|
||||
if (isNodeSelection(selection)) {
|
||||
const { node } = selection
|
||||
const { $from } = selection
|
||||
|
||||
if (equalNodeType(nodeType, node)) {
|
||||
return { node, pos: $from.pos, depth: $from.depth }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
17
packages/tiptap-utils/src/utils/flatten.js
Normal file
17
packages/tiptap-utils/src/utils/flatten.js
Normal file
@@ -0,0 +1,17 @@
|
||||
export default function flatten(node) {
|
||||
// eslint-disable-next-line
|
||||
const descend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true
|
||||
|
||||
if (!node) {
|
||||
throw new Error('Invalid "node" parameter')
|
||||
}
|
||||
const result = []
|
||||
// eslint-disable-next-line
|
||||
node.descendants((child, pos) => {
|
||||
result.push({ node: child, pos })
|
||||
if (!descend) {
|
||||
return false
|
||||
}
|
||||
})
|
||||
return result
|
||||
}
|
||||
20
packages/tiptap-utils/src/utils/getTableNodeTypes.js
Normal file
20
packages/tiptap-utils/src/utils/getTableNodeTypes.js
Normal file
@@ -0,0 +1,20 @@
|
||||
export default function getTableNodeTypes(schema) {
|
||||
if (schema.cached.tableNodeTypes) {
|
||||
return schema.cached.tableNodeTypes
|
||||
}
|
||||
|
||||
const roles = {}
|
||||
|
||||
Object.keys(schema.nodes).forEach(type => {
|
||||
const nodeType = schema.nodes[type]
|
||||
|
||||
if (nodeType.spec.tableRole) {
|
||||
roles[nodeType.spec.tableRole] = nodeType
|
||||
}
|
||||
})
|
||||
|
||||
// eslint-disable-next-line
|
||||
schema.cached.tableNodeTypes = roles
|
||||
|
||||
return roles
|
||||
}
|
||||
5
packages/tiptap-utils/src/utils/isNodeSelection.js
Normal file
5
packages/tiptap-utils/src/utils/isNodeSelection.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import { NodeSelection } from 'prosemirror-state'
|
||||
|
||||
export default function isNodeSelection(selection) {
|
||||
return selection instanceof NodeSelection
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
import {
|
||||
findParentNode,
|
||||
findSelectedNodeOfType,
|
||||
} from 'prosemirror-utils'
|
||||
import findSelectedNodeOfType from './findSelectedNodeOfType'
|
||||
import findParentNode from './findParentNode'
|
||||
|
||||
export default function nodeIsActive(state, type, attrs = {}) {
|
||||
const predicate = node => node.type === type
|
||||
|
||||
Reference in New Issue
Block a user