prosemirror-flat-list
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -1,15 +0,13 @@ | ||
import { NodeType, Attrs, Schema, ParseRule, NodeSpec, DOMOutputSpec, DOMSerializer } from '@remirror/pm/model'; | ||
import { Command, Plugin } from '@remirror/pm/state'; | ||
import { EditorView, NodeType as NodeType$1, ProsemirrorNode, Fragment } from '@remirror/pm'; | ||
import { Attrs, NodeRange, Schema, ParseRule, NodeSpec, DOMOutputSpec, NodeType, DOMSerializer } from '@remirror/pm/model'; | ||
import { EditorView, ProsemirrorNode, Fragment } from '@remirror/pm'; | ||
import { InputRule } from '@remirror/pm/inputrules'; | ||
import { NodeViewConstructor } from '@remirror/pm/view'; | ||
declare function createDedentListCommand(listType: NodeType): Command; | ||
declare function createDedentListCommand(): Command; | ||
declare function createIndentListCommand(listType: NodeType): Command; | ||
declare function createIndentListCommand(): Command; | ||
declare function createSplitListCommand(listType: NodeType): Command; | ||
declare function createSplitListCommand(): Command; | ||
declare function handleListMarkerMouseDown(view: EditorView, event: MouseEvent, listType: NodeType$1): boolean; | ||
type ListType = 'bullet' | 'ordered' | 'task' | 'toggle'; | ||
@@ -41,5 +39,9 @@ interface ListAttributes { | ||
declare function wrappingListInputRule<T extends Attrs = ListAttributes>(re: RegExp, listType: NodeType, getAttrs: T | ((matches: RegExpMatchArray) => T)): InputRule; | ||
declare function createListInputRules(listType: NodeType): InputRule[]; | ||
declare function createWrapInListCommand<T extends Attrs = ListAttributes>(getAttrs: T | ((range: NodeRange) => T | null)): Command; | ||
declare function handleListMarkerMouseDown(view: EditorView, event: MouseEvent): boolean; | ||
declare function wrappingListInputRule<T extends Attrs = ListAttributes>(re: RegExp, getAttrs: T | ((matches: RegExpMatchArray) => T)): InputRule; | ||
declare function createListInputRules(): InputRule[]; | ||
declare function migrateDocJSON(docJSON: ProsemirrorNodeJSON): ProsemirrorNodeJSON; | ||
@@ -53,3 +55,3 @@ | ||
declare function createListPlugin(schema: Schema, listType: NodeType): Plugin; | ||
declare function createListPlugin(schema: Schema): Plugin; | ||
@@ -69,2 +71,8 @@ declare function createParseDomRules(): readonly ParseRule[]; | ||
declare function getListType(schema: Schema): NodeType; | ||
declare function isListNode(node: ProsemirrorNode | null | undefined): boolean; | ||
declare function isListType(type: NodeType): boolean; | ||
declare class ListDOMSerializer extends DOMSerializer { | ||
@@ -79,2 +87,2 @@ static nodesFromSchema(schema: Schema): { | ||
export { ListAttributes, ListDOMSerializer, ListType, alwaysTrue, createDedentListCommand, createIndentListCommand, createListInputRules, createListNodeView, createListPlugin, createListSpec, createParseDomRules, createSplitListCommand, handleListMarkerMouseDown, listToDOM, migrateDocJSON, wrappingListInputRule }; | ||
export { ListAttributes, ListDOMSerializer, ListType, alwaysTrue, createDedentListCommand, createIndentListCommand, createListInputRules, createListNodeView, createListPlugin, createListSpec, createParseDomRules, createSplitListCommand, createWrapInListCommand, getListType, handleListMarkerMouseDown, isListNode, isListType, listToDOM, migrateDocJSON, wrappingListInputRule }; |
@@ -7,2 +7,184 @@ // src/commands/dedent-list.ts | ||
import { canJoin } from "@remirror/pm/transform"; | ||
// src/utils/parse-integer.ts | ||
function parseInteger(attr) { | ||
if (attr == null) | ||
return null; | ||
const int = Number.parseInt(attr, 10); | ||
if (Number.isInteger(int)) | ||
return int; | ||
return null; | ||
} | ||
// src/schema/parse-dom.ts | ||
function createParseDomRules() { | ||
return [ | ||
{ | ||
tag: "div[data-list]", | ||
getAttrs: (element) => { | ||
if (typeof element === "string") { | ||
return {}; | ||
} | ||
return { | ||
type: element.getAttribute("data-list-type") || "bullet", | ||
order: parseInteger(element.getAttribute("data-list-order")), | ||
checked: element.hasAttribute("data-list-checked"), | ||
collapsed: element.hasAttribute("data-list-collapsed") | ||
}; | ||
} | ||
}, | ||
{ | ||
tag: "ul > li", | ||
getAttrs: (element) => { | ||
if (typeof element !== "string") { | ||
const checkbox = element.firstChild; | ||
if (checkbox && checkbox.nodeName === "INPUT" && checkbox.getAttribute("type") === "checkbox") { | ||
return { | ||
type: "task", | ||
checked: checkbox.hasAttribute("checked") | ||
}; | ||
} | ||
if (element.hasAttribute("data-task-list-item") || element.getAttribute("data-list-type") === "task") { | ||
return { | ||
type: "task", | ||
checked: element.hasAttribute("data-checked") | ||
}; | ||
} | ||
if (element.hasAttribute("data-toggle-list-item") || element.getAttribute("data-list-type") === "toggle") { | ||
return { | ||
type: "toggle", | ||
collapsed: element.hasAttribute("data-list-collapsed") | ||
}; | ||
} | ||
} | ||
return { | ||
type: "bullet" | ||
}; | ||
} | ||
}, | ||
{ | ||
tag: "ol > li", | ||
getAttrs: (_element) => { | ||
return { | ||
type: "ordered" | ||
}; | ||
} | ||
} | ||
]; | ||
} | ||
// src/schema/to-dom.ts | ||
function listToDOM(node, nativeList) { | ||
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 markerType = markerHidden ? void 0 : attrs.type || "bullet"; | ||
const domAttrs = { | ||
class: "prosemirror-flat-list", | ||
"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 | ||
}; | ||
const contentContainer = ["div", { class: "list-content" }, 0]; | ||
if (marker) { | ||
const markerContainer = [ | ||
"div", | ||
{ | ||
class: "list-marker", | ||
contenteditable: "false" | ||
}, | ||
marker | ||
]; | ||
return nativeList ? [ | ||
attrs.type === "ordered" ? "ol" : "ul", | ||
["li", domAttrs, markerContainer, contentContainer] | ||
] : ["div", domAttrs, markerContainer, contentContainer]; | ||
} else { | ||
return nativeList ? [ | ||
attrs.type === "ordered" ? "ol" : "ul", | ||
["li", domAttrs, contentContainer] | ||
] : ["div", domAttrs, contentContainer]; | ||
} | ||
} | ||
// src/schema/spec.ts | ||
var flatListGroup = "flatList"; | ||
function createListSpec() { | ||
return { | ||
content: "block+", | ||
group: flatListGroup, | ||
defining: true, | ||
attrs: { | ||
type: { | ||
default: "bullet" | ||
}, | ||
counter: { | ||
default: null | ||
}, | ||
checked: { | ||
default: false | ||
}, | ||
collapsed: { | ||
default: false | ||
} | ||
}, | ||
toDOM: (node) => { | ||
return listToDOM(node, false); | ||
}, | ||
parseDOM: createParseDomRules() | ||
}; | ||
} | ||
// src/utils/get-list-type.ts | ||
function getListType(schema) { | ||
let name = schema.cached["PROSEMIRROR_FLAT_LIST_LIST_TYPE_NAME"]; | ||
if (!name) { | ||
for (const type of Object.values(schema.nodes)) { | ||
if ((type.spec.group || "").split(" ").includes(flatListGroup)) { | ||
name = type.name; | ||
break; | ||
} | ||
} | ||
if (!name) { | ||
throw new TypeError( | ||
"[prosemirror-flat-list] Unable to find a flat list type in the schema" | ||
); | ||
} | ||
schema.cached["PROSEMIRROR_FLAT_LIST_LIST_TYPE_NAME"] = name; | ||
} | ||
return schema.nodes[name]; | ||
} | ||
// src/utils/is-list-type.ts | ||
function isListType(type) { | ||
return getListType(type.schema) === type; | ||
} | ||
// src/utils/is-list-node.ts | ||
function isListNode(node) { | ||
if (!node) | ||
return false; | ||
return isListType(node.type); | ||
} | ||
// src/utils/auto-join-list.ts | ||
function getTransactionRanges(tr) { | ||
@@ -46,6 +228,5 @@ const ranges = []; | ||
} | ||
function autoJoinList(tr, listType) { | ||
function autoJoinList(tr) { | ||
const isListJoinable = (before, after) => { | ||
var _a; | ||
return before.type === listType && after.type === listType && ((_a = after.firstChild) == null ? void 0 : _a.type) === listType; | ||
return isListNode(before) && isListNode(after) && isListNode(after.firstChild); | ||
}; | ||
@@ -90,9 +271,9 @@ const positions = getTransactionRanges(tr); | ||
import { NodeRange } from "@remirror/pm/model"; | ||
function findListsRange($from, $to, listType) { | ||
function findListsRange($from, $to) { | ||
if ($to.pos < $from.pos) { | ||
return findListsRange($to, $from, listType); | ||
return findListsRange($to, $from); | ||
} | ||
let range = $from.blockRange($to); | ||
while (range) { | ||
if (isListsRange(range, listType)) { | ||
if (isListsRange(range)) { | ||
return range; | ||
@@ -107,6 +288,6 @@ } | ||
} | ||
function isListsRange(range, listType) { | ||
function isListsRange(range) { | ||
const { startIndex, endIndex, parent } = range; | ||
for (let i = startIndex; i < endIndex; i++) { | ||
if (parent.child(i).type !== listType) { | ||
if (!isListNode(parent.child(i))) { | ||
return false; | ||
@@ -155,11 +336,11 @@ } | ||
// src/commands/dedent-list.ts | ||
function createDedentListCommand(listType) { | ||
function createDedentListCommand() { | ||
const dedentListCommand = (state, dispatch) => { | ||
const tr = state.tr; | ||
const { $from, $to } = tr.selection; | ||
const range = findListsRange($from, $to, listType); | ||
const range = findListsRange($from, $to); | ||
if (!range) | ||
return false; | ||
if (dedentRange(range, tr, listType)) { | ||
autoJoinList(tr, listType); | ||
if (dedentRange(range, tr)) { | ||
autoJoinList(tr); | ||
dispatch == null ? void 0 : dispatch(tr); | ||
@@ -172,3 +353,3 @@ return true; | ||
} | ||
function dedentRange(range, tr, listType, startBoundary, endBoundary) { | ||
function dedentRange(range, tr, startBoundary, endBoundary) { | ||
const { depth, $from, $to } = range; | ||
@@ -180,5 +361,5 @@ startBoundary = startBoundary || atStartBlockBoundary($from, depth + 1); | ||
const contentRange = zoomInRange(range); | ||
return contentRange ? dedentRange(contentRange, tr, listType) : false; | ||
return contentRange ? dedentRange(contentRange, tr) : false; | ||
} else { | ||
return splitAndDedentRange(range, tr, listType, startIndex + 1); | ||
return splitAndDedentRange(range, tr, startIndex + 1); | ||
} | ||
@@ -188,3 +369,3 @@ } | ||
if (!endBoundary) { | ||
fixEndBoundary(range, tr, listType); | ||
fixEndBoundary(range, tr); | ||
const endOfParent = $to.end(depth); | ||
@@ -196,10 +377,10 @@ range = new NodeRange2( | ||
); | ||
return dedentRange(range, tr, listType, void 0, true); | ||
return dedentRange(range, tr, void 0, true); | ||
} | ||
if (range.startIndex === 0 && range.endIndex === range.parent.childCount && range.parent.type === listType) { | ||
return dedentNodeRange(new NodeRange2($from, $to, depth - 1), tr, listType); | ||
if (range.startIndex === 0 && range.endIndex === range.parent.childCount && isListNode(range.parent)) { | ||
return dedentNodeRange(new NodeRange2($from, $to, depth - 1), tr); | ||
} | ||
return dedentNodeRange(range, tr, listType); | ||
return dedentNodeRange(range, tr); | ||
} | ||
function splitAndDedentRange(range, tr, listType, splitIndex) { | ||
function splitAndDedentRange(range, tr, splitIndex) { | ||
const { $from, $to, depth } = range; | ||
@@ -212,12 +393,12 @@ const splitPos = $from.posAtIndex(splitIndex, depth); | ||
const getRange2To = mapPos(tr, $to.pos); | ||
dedentRange(range1, tr, listType, void 0, true); | ||
dedentRange(range1, tr, void 0, true); | ||
let range2 = tr.doc.resolve(getRange2From()).blockRange(tr.doc.resolve(getRange2To())); | ||
if (range2 && range2.depth >= depth) { | ||
range2 = new NodeRange2(range2.$from, range2.$to, depth); | ||
dedentRange(range2, tr, listType, true, void 0); | ||
dedentRange(range2, tr, true, void 0); | ||
} | ||
return true; | ||
} | ||
function dedentNodeRange(range, tr, listType) { | ||
if (range.parent.type === listType) { | ||
function dedentNodeRange(range, tr) { | ||
if (isListNode(range.parent)) { | ||
return safeLift(tr, range); | ||
@@ -228,4 +409,4 @@ } else { | ||
} | ||
function fixEndBoundary(range, tr, listType) { | ||
var _a; | ||
function fixEndBoundary(range, tr) { | ||
const listType = getListType(tr.doc.type.schema); | ||
if (range.endIndex - range.startIndex >= 2) { | ||
@@ -242,3 +423,3 @@ range = new NodeRange2( | ||
if (contentRange) { | ||
fixEndBoundary(contentRange, tr, listType); | ||
fixEndBoundary(contentRange, tr); | ||
range = new NodeRange2( | ||
@@ -252,3 +433,3 @@ tr.doc.resolve(range.$from.pos), | ||
const endOfParent = $to.end(depth); | ||
if (end < endOfParent && ((_a = parent.maybeChild(endIndex - 1)) == null ? void 0 : _a.type) === listType) { | ||
if (end < endOfParent && isListNode(parent.maybeChild(endIndex - 1))) { | ||
tr.step( | ||
@@ -307,11 +488,11 @@ new ReplaceAroundStep( | ||
import { ReplaceAroundStep as ReplaceAroundStep2 } from "@remirror/pm/transform"; | ||
function createIndentListCommand(listType) { | ||
function createIndentListCommand() { | ||
const indentListCommand = (state, dispatch) => { | ||
const tr = state.tr; | ||
const { $from, $to } = tr.selection; | ||
const range = findListsRange($from, $to, listType) || $from.blockRange($to); | ||
const range = findListsRange($from, $to) || $from.blockRange($to); | ||
if (!range) | ||
return false; | ||
if (indentRange(range, tr, listType)) { | ||
autoJoinList(tr, listType); | ||
if (indentRange(range, tr)) { | ||
autoJoinList(tr); | ||
dispatch == null ? void 0 : dispatch(tr); | ||
@@ -324,3 +505,3 @@ return true; | ||
} | ||
function indentRange(range, tr, listType, startBoundary, endBoundary) { | ||
function indentRange(range, tr, startBoundary, endBoundary) { | ||
const { depth, $from, $to } = range; | ||
@@ -332,5 +513,5 @@ startBoundary = startBoundary || atStartBlockBoundary($from, depth + 1); | ||
const contentRange = zoomInRange(range); | ||
return contentRange ? indentRange(contentRange, tr, listType) : false; | ||
return contentRange ? indentRange(contentRange, tr) : false; | ||
} else { | ||
return splitAndIndentRange(range, tr, listType, startIndex + 1); | ||
return splitAndIndentRange(range, tr, startIndex + 1); | ||
} | ||
@@ -343,10 +524,10 @@ } | ||
const contentRange = zoomInRange(range); | ||
return contentRange ? indentRange(contentRange, tr, listType) : false; | ||
return contentRange ? indentRange(contentRange, tr) : false; | ||
} else { | ||
return splitAndIndentRange(range, tr, listType, endIndex - 1); | ||
return splitAndIndentRange(range, tr, endIndex - 1); | ||
} | ||
} | ||
return indentNodeRange(range, tr, listType); | ||
return indentNodeRange(range, tr); | ||
} | ||
function splitAndIndentRange(range, tr, listType, splitIndex) { | ||
function splitAndIndentRange(range, tr, splitIndex) { | ||
const { $from, $to, depth } = range; | ||
@@ -359,12 +540,12 @@ const splitPos = $from.posAtIndex(splitIndex, depth); | ||
const getRange2To = mapPos(tr, $to.pos); | ||
indentRange(range1, tr, listType, void 0, true); | ||
indentRange(range1, tr, void 0, true); | ||
const range2 = tr.doc.resolve(getRange2From()).blockRange(tr.doc.resolve(getRange2To())); | ||
range2 && indentRange(range2, tr, listType, true, void 0); | ||
range2 && indentRange(range2, tr, true, void 0); | ||
return true; | ||
} | ||
function indentNodeRange(range, tr, listType) { | ||
var _a; | ||
function indentNodeRange(range, tr) { | ||
const listType = getListType(tr.doc.type.schema); | ||
const { parent, startIndex } = range; | ||
const prevChild = startIndex >= 1 && parent.child(startIndex - 1); | ||
if (prevChild && prevChild.type === listType) { | ||
if (prevChild && isListNode(prevChild)) { | ||
const { start, end } = range; | ||
@@ -384,3 +565,3 @@ tr.step( | ||
} | ||
if (startIndex === 0 && parent.type === listType || ((_a = parent.maybeChild(startIndex)) == null ? void 0 : _a.type) === listType) { | ||
if (startIndex === 0 && isListNode(parent) || isListNode(parent.maybeChild(startIndex))) { | ||
const { start, end } = range; | ||
@@ -426,3 +607,3 @@ tr.step( | ||
// src/commands/split-list.ts | ||
function createSplitListCommand(listType) { | ||
function createSplitListCommand() { | ||
const splitListCommand = (state, dispatch, view) => { | ||
@@ -438,3 +619,3 @@ const { selection } = state; | ||
const listNode = $from.node(-1); | ||
if (!listNode || listNode.type !== listType) { | ||
if (!isListNode(listNode)) { | ||
return false; | ||
@@ -453,3 +634,3 @@ } | ||
{ | ||
type: listType, | ||
type: getListType(state.schema), | ||
attrs: { | ||
@@ -470,4 +651,71 @@ type: listNode.attrs.type | ||
// src/commands/wrap-in-list.ts | ||
import { findWrapping } from "@remirror/pm/transform"; | ||
import { NodeRange as NodeRange4 } from "@remirror/pm/model"; | ||
// src/utils/setNodeAttributes.ts | ||
function setNodeAttributes(tr, pos, oldAttrs, newAttrs) { | ||
let needUpdate = false; | ||
for (const key of Object.keys(newAttrs)) { | ||
if (newAttrs[key] !== oldAttrs[key]) { | ||
tr.setNodeAttribute(pos, key, newAttrs[key]); | ||
needUpdate = true; | ||
} | ||
} | ||
return needUpdate; | ||
} | ||
// src/commands/wrap-in-list.ts | ||
function createWrapInListCommand(getAttrs) { | ||
const wrapInList = (state, dispatch) => { | ||
const { $from, $to } = state.selection; | ||
let range = $from.blockRange($to); | ||
if (!range) { | ||
return false; | ||
} | ||
if (rangeAllowInlineContent(range) && isListNode(range.parent) && range.depth > 0) { | ||
range = new NodeRange4($from, $to, range.depth - 1); | ||
} | ||
const attrs = typeof getAttrs === "function" ? getAttrs(range) : getAttrs; | ||
if (!attrs) { | ||
return false; | ||
} | ||
const { parent, startIndex, endIndex, depth } = range; | ||
const tr = state.tr; | ||
const listType = getListType(state.schema); | ||
for (let i = endIndex - 1; i >= startIndex; i--) { | ||
const node = parent.child(i); | ||
if (isListNode(node)) { | ||
const oldAttrs = node.attrs; | ||
const newAttrs = { ...oldAttrs, ...attrs }; | ||
setNodeAttributes(tr, $from.posAtIndex(i, depth), oldAttrs, newAttrs); | ||
} else { | ||
const range2 = new NodeRange4( | ||
tr.doc.resolve($from.posAtIndex(i, depth) + 1), | ||
tr.doc.resolve($from.posAtIndex(i + 1, depth) - 1), | ||
depth | ||
); | ||
const wrapping = findWrapping(range2, listType, attrs); | ||
if (wrapping) { | ||
tr.wrap(range2, wrapping); | ||
} | ||
} | ||
} | ||
dispatch == null ? void 0 : dispatch(tr); | ||
return true; | ||
}; | ||
return wrapInList; | ||
} | ||
function rangeAllowInlineContent(range) { | ||
const { parent, startIndex, endIndex } = range; | ||
for (let i = startIndex; i < endIndex; i++) { | ||
if (parent.child(i).inlineContent) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
// src/dom-events.ts | ||
function handleListMarkerMouseDown(view, event, listType) { | ||
function handleListMarkerMouseDown(view, event) { | ||
const target = event.target; | ||
@@ -480,3 +728,3 @@ if (target == null ? void 0 : target.classList.contains("list-marker")) { | ||
const list = $pos.parent; | ||
if (list.type !== listType) { | ||
if (isListNode(list)) { | ||
return false; | ||
@@ -499,4 +747,4 @@ } | ||
import { InputRule } from "@remirror/pm/inputrules"; | ||
import { findWrapping } from "@remirror/pm/transform"; | ||
function wrappingListInputRule(re, listType, getAttrs) { | ||
import { findWrapping as findWrapping2 } from "@remirror/pm/transform"; | ||
function wrappingListInputRule(re, getAttrs) { | ||
return new InputRule(re, (state, match, start, end) => { | ||
@@ -508,7 +756,7 @@ const tr = state.tr; | ||
const listNode = $pos.index(-1) === 0 && $pos.node(-1); | ||
if (listNode && listNode.type === listType) { | ||
if (listNode && isListNode(listNode)) { | ||
const oldAttrs = listNode.attrs; | ||
const newAttrs = { ...oldAttrs, ...attrs }; | ||
const needUpdate = Object.keys(newAttrs).some( | ||
(key) => newAttrs[key] != oldAttrs[key] | ||
(key) => newAttrs[key] !== oldAttrs[key] | ||
); | ||
@@ -526,3 +774,3 @@ if (needUpdate) { | ||
} | ||
const wrapping = findWrapping(range, listType, attrs); | ||
const wrapping = findWrapping2(range, getListType(state.schema), attrs); | ||
if (!wrapping) { | ||
@@ -534,3 +782,3 @@ return null; | ||
} | ||
function createListInputRules(listType) { | ||
function createListInputRules() { | ||
const bulletRegexp = /^\s?([*-])\s$/; | ||
@@ -541,9 +789,9 @@ const orderedRegexp = /^\s?(\d+)\.\s$/; | ||
return [ | ||
wrappingListInputRule(bulletRegexp, listType, { type: "bullet" }), | ||
wrappingListInputRule(orderedRegexp, listType, { type: "ordered" }), | ||
wrappingListInputRule(taskRegexp, listType, (match) => ({ | ||
wrappingListInputRule(bulletRegexp, { type: "bullet" }), | ||
wrappingListInputRule(orderedRegexp, { type: "ordered" }), | ||
wrappingListInputRule(taskRegexp, (match) => ({ | ||
type: "task", | ||
checked: ["x", "X"].includes(match[1]) | ||
})), | ||
wrappingListInputRule(toggleRegexp, listType, { type: "toggle" }) | ||
wrappingListInputRule(toggleRegexp, { type: "toggle" }) | ||
]; | ||
@@ -625,57 +873,2 @@ } | ||
import { DOMSerializer as DOMSerializer2 } from "@remirror/pm/model"; | ||
// src/schema/to-dom.ts | ||
function listToDOM(node, nativeList) { | ||
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 markerType = markerHidden ? void 0 : attrs.type || "bullet"; | ||
const domAttrs = { | ||
class: "prosemirror-flat-list", | ||
"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 | ||
}; | ||
const contentContainer = ["div", { class: "list-content" }, 0]; | ||
if (marker) { | ||
const markerContainer = [ | ||
"div", | ||
{ | ||
class: "list-marker", | ||
contenteditable: "false" | ||
}, | ||
marker | ||
]; | ||
return nativeList ? [ | ||
attrs.type === "ordered" ? "ol" : "ul", | ||
["li", domAttrs, markerContainer, contentContainer] | ||
] : ["div", domAttrs, markerContainer, contentContainer]; | ||
} else { | ||
return nativeList ? [ | ||
attrs.type === "ordered" ? "ol" : "ul", | ||
["li", domAttrs, contentContainer] | ||
] : ["div", domAttrs, contentContainer]; | ||
} | ||
} | ||
// src/utils/list-serializer.ts | ||
var ListDOMSerializer = class extends DOMSerializer2 { | ||
@@ -709,3 +902,3 @@ static nodesFromSchema(schema) { | ||
// src/plugin.ts | ||
function createListPlugin(schema, listType) { | ||
function createListPlugin(schema) { | ||
return new Plugin({ | ||
@@ -715,3 +908,3 @@ props: { | ||
mousedown: (view, event) => { | ||
return handleListMarkerMouseDown(view, event, listType); | ||
return handleListMarkerMouseDown(view, event); | ||
} | ||
@@ -727,95 +920,2 @@ }, | ||
// src/utils/parse-integer.ts | ||
function parseInteger(attr) { | ||
if (attr == null) | ||
return null; | ||
const int = Number.parseInt(attr, 10); | ||
if (Number.isInteger(int)) | ||
return int; | ||
return null; | ||
} | ||
// src/schema/parse-dom.ts | ||
function createParseDomRules() { | ||
return [ | ||
{ | ||
tag: "div[data-list]", | ||
getAttrs: (element) => { | ||
if (typeof element === "string") { | ||
return {}; | ||
} | ||
return { | ||
type: element.getAttribute("data-list-type") || "bullet", | ||
order: parseInteger(element.getAttribute("data-list-order")), | ||
checked: element.hasAttribute("data-list-checked"), | ||
collapsed: element.hasAttribute("data-list-collapsed") | ||
}; | ||
} | ||
}, | ||
{ | ||
tag: "ul > li", | ||
getAttrs: (element) => { | ||
if (typeof element !== "string") { | ||
const checkbox = element.firstChild; | ||
if (checkbox && checkbox.nodeName === "INPUT" && checkbox.getAttribute("type") === "checkbox") { | ||
return { | ||
type: "task", | ||
checked: checkbox.hasAttribute("checked") | ||
}; | ||
} | ||
if (element.hasAttribute("data-task-list-item") || element.getAttribute("data-list-type") === "task") { | ||
return { | ||
type: "task", | ||
checked: element.hasAttribute("data-checked") | ||
}; | ||
} | ||
if (element.hasAttribute("data-toggle-list-item") || element.getAttribute("data-list-type") === "toggle") { | ||
return { | ||
type: "toggle", | ||
collapsed: element.hasAttribute("data-list-collapsed") | ||
}; | ||
} | ||
} | ||
return { | ||
type: "bullet" | ||
}; | ||
} | ||
}, | ||
{ | ||
tag: "ol > li", | ||
getAttrs: (_element) => { | ||
return { | ||
type: "ordered" | ||
}; | ||
} | ||
} | ||
]; | ||
} | ||
// src/schema/spec.ts | ||
function createListSpec() { | ||
return { | ||
content: "block+", | ||
defining: true, | ||
attrs: { | ||
type: { | ||
default: "bullet" | ||
}, | ||
counter: { | ||
default: null | ||
}, | ||
checked: { | ||
default: false | ||
}, | ||
collapsed: { | ||
default: false | ||
} | ||
}, | ||
toDOM: (node) => { | ||
return listToDOM(node, false); | ||
}, | ||
parseDOM: createParseDomRules() | ||
}; | ||
} | ||
// src/utils/always-true.ts | ||
@@ -839,3 +939,7 @@ function alwaysTrue(func) { | ||
createSplitListCommand, | ||
createWrapInListCommand, | ||
getListType, | ||
handleListMarkerMouseDown, | ||
isListNode, | ||
isListType, | ||
listToDOM, | ||
@@ -842,0 +946,0 @@ migrateDocJSON, |
{ | ||
"name": "prosemirror-flat-list", | ||
"type": "module", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "", | ||
@@ -6,0 +6,0 @@ "author": "ocavue <ocavue@gmail.com>", |
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
62785
1943