prosemirror-flat-list
Advanced tools
Comparing version 0.0.10 to 0.0.11
import { Command, Plugin } from 'prosemirror-state'; | ||
import { Attrs, NodeRange, Schema, ParseRule, NodeSpec, Node, DOMOutputSpec, NodeType, DOMSerializer, Fragment } from 'prosemirror-model'; | ||
import { ProsemirrorNode } from '@remirror/core'; | ||
import { EditorView, NodeViewConstructor } from 'prosemirror-view'; | ||
@@ -10,2 +11,4 @@ import { InputRule } from 'prosemirror-inputrules'; | ||
declare function createMoveListCommand(direction: 'up' | 'down'): Command; | ||
declare function createSplitListCommand(): Command; | ||
@@ -41,6 +44,6 @@ | ||
declare function createMoveListCommand(direction: 'up' | 'down'): Command; | ||
declare function handleListMarkerMouseDown(view: EditorView, event: MouseEvent, onListClick?: ListClickHandler): boolean; | ||
type ListClickHandler = (node: ProsemirrorNode) => ListAttributes; | ||
declare const defaultListClickHandler: ListClickHandler; | ||
declare function handleListMarkerMouseDown(view: EditorView, event: MouseEvent): boolean; | ||
declare function wrappingListInputRule<T extends Attrs = ListAttributes>(re: RegExp, getAttrs: T | ((matches: RegExpMatchArray) => T)): InputRule; | ||
@@ -61,5 +64,8 @@ declare function createListInputRules(): InputRule[]; | ||
declare const flatListGroup = "flatList"; | ||
declare function createListSpec(): NodeSpec; | ||
declare function listToDOM(node: Node, nativeList: boolean): DOMOutputSpec; | ||
declare function listToDOM(node: Node, nativeList: boolean, markerToDOM?: MarkerToDOM): DOMOutputSpec; | ||
type MarkerToDOM = (attrs: ListAttributes) => DOMOutputSpec | null; | ||
declare const defaultMarkerToDOM: MarkerToDOM; | ||
@@ -88,2 +94,2 @@ /** | ||
export { ListAttributes, ListDOMSerializer, ListType, alwaysTrue, createDedentListCommand, createIndentListCommand, createListInputRules, createListNodeView, createListPlugin, createListSpec, createMoveListCommand, createParseDomRules, createSplitListCommand, createWrapInListCommand, getListType, handleListMarkerMouseDown, isListNode, isListType, listToDOM, migrateDocJSON, wrappingListInputRule }; | ||
export { ListAttributes, ListClickHandler, ListDOMSerializer, ListType, MarkerToDOM, alwaysTrue, createDedentListCommand, createIndentListCommand, createListInputRules, createListNodeView, createListPlugin, createListSpec, createMoveListCommand, createParseDomRules, createSplitListCommand, createWrapInListCommand, defaultListClickHandler, defaultMarkerToDOM, flatListGroup, getListType, handleListMarkerMouseDown, isListNode, isListType, listToDOM, migrateDocJSON, wrappingListInputRule }; |
@@ -76,23 +76,7 @@ // src/commands/dedent-list.ts | ||
// src/schema/to-dom.ts | ||
function listToDOM(node, nativeList) { | ||
function listToDOM(node, nativeList, markerToDOM = defaultMarkerToDOM) { | ||
var _a; | ||
const attrs = node.attrs; | ||
const markerHidden = ((_a = node.firstChild) == null ? void 0 : _a.type) === node.type; | ||
let marker = null; | ||
if (!markerHidden) { | ||
switch (attrs.type) { | ||
case "task": | ||
marker = [ | ||
"label", | ||
[ | ||
"input", | ||
{ type: "checkbox", checked: attrs.checked ? "" : void 0 } | ||
] | ||
]; | ||
break; | ||
case "toggle": | ||
marker = ["span"]; | ||
break; | ||
} | ||
} | ||
const marker = markerHidden ? null : markerToDOM(attrs); | ||
const markerType = markerHidden ? void 0 : attrs.type || "bullet"; | ||
@@ -102,9 +86,9 @@ const domAttrs = { | ||
"data-list-type": markerType, | ||
"data-list-order": markerType === "ordered" && attrs.order != null ? String(attrs.order) : void 0, | ||
"data-list-checked": markerType === "task" && attrs.checked ? "" : void 0, | ||
"data-list-collapsed": markerType === "toggle" && attrs.collapsed ? "" : void 0, | ||
"data-list-disabled": markerType === "toggle" && node.childCount < 2 ? "" : void 0 | ||
"data-list-order": attrs.order != null ? String(attrs.order) : void 0, | ||
"data-list-checked": attrs.checked ? "" : void 0, | ||
"data-list-collapsed": attrs.collapsed ? "" : void 0, | ||
"data-list-collapsable": node.childCount >= 2 ? "" : void 0 | ||
}; | ||
const contentContainer = ["div", { class: "list-content" }, 0]; | ||
if (marker) { | ||
if (marker != null) { | ||
const markerContainer = [ | ||
@@ -129,2 +113,18 @@ "div", | ||
} | ||
var defaultMarkerToDOM = (attrs) => { | ||
switch (attrs.type) { | ||
case "task": | ||
return [ | ||
"label", | ||
[ | ||
"input", | ||
{ type: "checkbox", checked: attrs.checked ? "" : void 0 } | ||
] | ||
]; | ||
case "toggle": | ||
return ["span"]; | ||
default: | ||
return null; | ||
} | ||
}; | ||
@@ -587,2 +587,62 @@ // src/schema/spec.ts | ||
// src/utils/cut-by-index.ts | ||
function cutByIndex(fragment, from, to) { | ||
return fragment.cutByIndex(from, to); | ||
} | ||
// src/commands/move-list.ts | ||
function createMoveListCommand(direction) { | ||
const moveList = (state, dispatch) => { | ||
const { $from, $to } = state.selection; | ||
const range = findListsRange($from, $to); | ||
if (!range) | ||
return false; | ||
const { parent, depth, startIndex, endIndex } = range; | ||
if (direction === "up") { | ||
if (startIndex > 0) { | ||
const before = cutByIndex(parent.content, startIndex - 1, startIndex); | ||
const selected = cutByIndex(parent.content, startIndex, endIndex); | ||
if (parent.canReplace(startIndex - 1, endIndex, selected.append(before))) { | ||
if (dispatch) { | ||
const tr = state.tr; | ||
tr.insert($from.posAtIndex(endIndex, depth), before); | ||
tr.delete( | ||
$from.posAtIndex(startIndex - 1, depth), | ||
$from.posAtIndex(startIndex, depth) | ||
); | ||
dispatch(tr); | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
if (endIndex < parent.childCount) { | ||
const selected = cutByIndex(parent.content, startIndex, endIndex); | ||
const after = cutByIndex(parent.content, endIndex, endIndex + 1); | ||
if (parent.canReplace(startIndex, endIndex + 1, after.append(selected))) { | ||
if (dispatch) { | ||
const tr = state.tr; | ||
tr.delete( | ||
$from.posAtIndex(endIndex, depth), | ||
$from.posAtIndex(endIndex + 1, depth) | ||
); | ||
tr.insert($from.posAtIndex(startIndex, depth), after); | ||
dispatch(tr); | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
}; | ||
return moveList; | ||
} | ||
// src/commands/split-list.ts | ||
@@ -719,64 +779,4 @@ import { canSplit } from "prosemirror-transform"; | ||
// src/utils/cut-by-index.ts | ||
function cutByIndex(fragment, from, to) { | ||
return fragment.cutByIndex(from, to); | ||
} | ||
// src/commands/move-list.ts | ||
function createMoveListCommand(direction) { | ||
const moveList = (state, dispatch) => { | ||
const { $from, $to } = state.selection; | ||
const range = findListsRange($from, $to); | ||
if (!range) | ||
return false; | ||
const { parent, depth, startIndex, endIndex } = range; | ||
if (direction === "up") { | ||
if (startIndex > 0) { | ||
const before = cutByIndex(parent.content, startIndex - 1, startIndex); | ||
const selected = cutByIndex(parent.content, startIndex, endIndex); | ||
if (parent.canReplace(startIndex - 1, endIndex, selected.append(before))) { | ||
if (dispatch) { | ||
const tr = state.tr; | ||
tr.insert($from.posAtIndex(endIndex, depth), before); | ||
tr.delete( | ||
$from.posAtIndex(startIndex - 1, depth), | ||
$from.posAtIndex(startIndex, depth) | ||
); | ||
dispatch(tr); | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
if (endIndex < parent.childCount) { | ||
const selected = cutByIndex(parent.content, startIndex, endIndex); | ||
const after = cutByIndex(parent.content, endIndex, endIndex + 1); | ||
if (parent.canReplace(startIndex, endIndex + 1, after.append(selected))) { | ||
if (dispatch) { | ||
const tr = state.tr; | ||
tr.delete( | ||
$from.posAtIndex(endIndex, depth), | ||
$from.posAtIndex(endIndex + 1, depth) | ||
); | ||
tr.insert($from.posAtIndex(startIndex, depth), after); | ||
dispatch(tr); | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
}; | ||
return moveList; | ||
} | ||
// src/dom-events.ts | ||
function handleListMarkerMouseDown(view, event) { | ||
function handleListMarkerMouseDown(view, event, onListClick = defaultListClickHandler) { | ||
const target = event.target; | ||
@@ -792,10 +792,7 @@ if (target == null ? void 0 : target.classList.contains("list-marker")) { | ||
} | ||
const attrs = list.attrs; | ||
const listPos = $pos.before($pos.depth); | ||
if (attrs.type === "task") { | ||
tr.setNodeAttribute(listPos, "checked", !attrs.checked); | ||
} else if (attrs.type === "toggle") { | ||
tr.setNodeAttribute(listPos, "collapsed", !attrs.collapsed); | ||
const attrs = onListClick(list); | ||
if (setNodeAttributes(tr, listPos, list.attrs, attrs)) { | ||
view.dispatch(tr); | ||
} | ||
view.dispatch(tr); | ||
return true; | ||
@@ -805,2 +802,12 @@ } | ||
} | ||
var defaultListClickHandler = (node) => { | ||
const attrs = node.attrs; | ||
if (attrs.type === "task") { | ||
return { ...attrs, checked: !attrs.checked }; | ||
} else if (attrs.type === "toggle") { | ||
return { ...attrs, collapsed: !attrs.collapsed }; | ||
} else { | ||
return attrs; | ||
} | ||
}; | ||
@@ -1005,2 +1012,5 @@ // src/input-rule.ts | ||
createWrapInListCommand, | ||
defaultListClickHandler, | ||
defaultMarkerToDOM, | ||
flatListGroup, | ||
getListType, | ||
@@ -1007,0 +1017,0 @@ handleListMarkerMouseDown, |
{ | ||
"name": "prosemirror-flat-list", | ||
"type": "module", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "", | ||
@@ -6,0 +6,0 @@ "author": "ocavue <ocavue@gmail.com>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69923
2145