toggle between unordered_list and ordered_list

This commit is contained in:
Chrissi2812 2019-05-17 15:50:59 +02:00
parent b61bec8bb9
commit a24fdac9f7
No known key found for this signature in database
GPG Key ID: B4B82C7E618271DA

View File

@ -1,49 +1,31 @@
import { nodeIsActive } from 'tiptap-utils'
import { wrapInList, liftListItem } from 'prosemirror-schema-list'
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)
}
function isList(node, schema) {
return (node.type === schema.nodes.bullet_list || node.type === schema.nodes.ordered_list)
}
// https://discuss.prosemirror.net/t/list-type-toggle/948
export default function toggleList(listType) {
return (state, dispatch) => {
const { schema } = state
const lift = liftListItem(schema.nodes.list_item)
const wrap = wrapInList(listType)
const { $from, $to } = state.selection
const range = $from.blockRange($to)
if (!range) {
return false
}
// import { wrapInList, liftListItem } from 'prosemirror-schema-list'
if (range.depth >= 1 && $from.node(range.depth).type === listType) {
return lift(state, dispatch)
}
// function isList(node, schema) {
// return (node.type === schema.nodes.bullet_list || node.type === schema.nodes.ordered_list)
// }
if (range.depth >= 1 && isList($from.node(range.depth), schema)) {
const { tr } = state
tr.setNodeMarkup(range.start - 1, listType)
if (dispatch) dispatch(tr)
return false
}
// 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)
// }
// }
// }
return wrap(state, dispatch)
}
}