add list option to nodes
This commit is contained in:
@@ -12,6 +12,11 @@ export interface NodeExtensionSpec<Options = {}, Commands = {}> extends Overwrit
|
|||||||
*/
|
*/
|
||||||
topNode?: boolean,
|
topNode?: boolean,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List
|
||||||
|
*/
|
||||||
|
list?: boolean,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Content
|
* Content
|
||||||
*/
|
*/
|
||||||
@@ -158,6 +163,7 @@ const defaultNode: NodeExtension = {
|
|||||||
type: 'node',
|
type: 'node',
|
||||||
name: 'node',
|
name: 'node',
|
||||||
topNode: false,
|
topNode: false,
|
||||||
|
list: false,
|
||||||
content: null,
|
content: null,
|
||||||
marks: null,
|
marks: null,
|
||||||
group: null,
|
group: null,
|
||||||
|
|||||||
@@ -1,23 +1,19 @@
|
|||||||
import { wrapInList, liftListItem } from 'prosemirror-schema-list'
|
import { wrapInList, liftListItem } from 'prosemirror-schema-list'
|
||||||
import { findParentNode } from 'prosemirror-utils'
|
import { findParentNode } from 'prosemirror-utils'
|
||||||
import { Node, NodeType, Schema } from 'prosemirror-model'
|
import { NodeType } from 'prosemirror-model'
|
||||||
import { Command } from '../Editor'
|
import { Command } from '../Editor'
|
||||||
import { createExtension } from '../Extension'
|
import { createExtension } from '../Extension'
|
||||||
import getNodeType from '../utils/getNodeType'
|
import getNodeType from '../utils/getNodeType'
|
||||||
|
import isList from '../utils/isList'
|
||||||
function isList(node: Node, schema: Schema) {
|
|
||||||
return (node.type === schema.nodes.bullet_list
|
|
||||||
|| node.type === schema.nodes.ordered_list
|
|
||||||
|| node.type === schema.nodes.task_list)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ToggleList = createExtension({
|
export const ToggleList = createExtension({
|
||||||
addCommands() {
|
addCommands() {
|
||||||
return {
|
return {
|
||||||
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType): Command => ({ tr, state, dispatch }) => {
|
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType): Command => ({ tr, state, dispatch }) => {
|
||||||
|
const { extensions } = this.editor.options
|
||||||
const listType = getNodeType(listTypeOrName, state.schema)
|
const listType = getNodeType(listTypeOrName, state.schema)
|
||||||
const itemType = getNodeType(itemTypeOrName, state.schema)
|
const itemType = getNodeType(itemTypeOrName, state.schema)
|
||||||
const { schema, selection } = state
|
const { selection } = state
|
||||||
const { $from, $to } = selection
|
const { $from, $to } = selection
|
||||||
const range = $from.blockRange($to)
|
const range = $from.blockRange($to)
|
||||||
|
|
||||||
@@ -25,14 +21,14 @@ export const ToggleList = createExtension({
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentList = findParentNode(node => isList(node, schema))(selection)
|
const parentList = findParentNode(node => isList(node.type.name, extensions))(selection)
|
||||||
|
|
||||||
if (range.depth >= 1 && parentList && range.depth - parentList.depth <= 1) {
|
if (range.depth >= 1 && parentList && range.depth - parentList.depth <= 1) {
|
||||||
if (parentList.node.type === listType) {
|
if (parentList.node.type === listType) {
|
||||||
return liftListItem(itemType)(state, dispatch)
|
return liftListItem(itemType)(state, dispatch)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isList(parentList.node, schema) && listType.validContent(parentList.node.content)) {
|
if (isList(parentList.node.type.name, extensions) && listType.validContent(parentList.node.content)) {
|
||||||
tr.setNodeMarkup(parentList.pos, listType)
|
tr.setNodeMarkup(parentList.pos, listType)
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|||||||
13
packages/core/src/utils/isList.ts
Normal file
13
packages/core/src/utils/isList.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { Extensions } from '../types'
|
||||||
|
import splitExtensions from './splitExtensions'
|
||||||
|
|
||||||
|
export default function isList(name: string, extensions: Extensions) {
|
||||||
|
const { nodeExtensions } = splitExtensions(extensions)
|
||||||
|
const extension = nodeExtensions.find(item => item.name === name)
|
||||||
|
|
||||||
|
if (!extension) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return extension.list
|
||||||
|
}
|
||||||
@@ -4,6 +4,8 @@ import { wrappingInputRule } from 'prosemirror-inputrules'
|
|||||||
const BulletList = createNode({
|
const BulletList = createNode({
|
||||||
name: 'bullet_list',
|
name: 'bullet_list',
|
||||||
|
|
||||||
|
list: true,
|
||||||
|
|
||||||
group: 'block',
|
group: 'block',
|
||||||
|
|
||||||
content: 'list_item+',
|
content: 'list_item+',
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { wrappingInputRule } from 'prosemirror-inputrules'
|
|||||||
const OrderedList = createNode({
|
const OrderedList = createNode({
|
||||||
name: 'ordered_list',
|
name: 'ordered_list',
|
||||||
|
|
||||||
|
list: true,
|
||||||
|
|
||||||
group: 'block',
|
group: 'block',
|
||||||
|
|
||||||
content: 'list_item+',
|
content: 'list_item+',
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { wrappingInputRule } from 'prosemirror-inputrules'
|
|||||||
const TaskList = createNode({
|
const TaskList = createNode({
|
||||||
name: 'task_list',
|
name: 'task_list',
|
||||||
|
|
||||||
|
list: true,
|
||||||
|
|
||||||
group: 'block',
|
group: 'block',
|
||||||
|
|
||||||
content: 'task_item+',
|
content: 'task_item+',
|
||||||
|
|||||||
Reference in New Issue
Block a user