@tiptap/extension-bullet-list
Advanced tools
Comparing version 2.0.0-beta.218 to 2.0.0-beta.219
@@ -1,3 +0,69 @@ | ||
import { Node, mergeAttributes, wrappingInputRule } from '@tiptap/core'; | ||
import { Node, mergeAttributes, Mark, getMarkAttributes, wrappingInputRule } from '@tiptap/core'; | ||
const ListItem = Node.create({ | ||
name: 'listItem', | ||
addOptions() { | ||
return { | ||
HTMLAttributes: {}, | ||
}; | ||
}, | ||
content: 'paragraph block*', | ||
defining: true, | ||
parseHTML() { | ||
return [ | ||
{ | ||
tag: 'li', | ||
}, | ||
]; | ||
}, | ||
renderHTML({ HTMLAttributes }) { | ||
return ['li', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; | ||
}, | ||
addKeyboardShortcuts() { | ||
return { | ||
Enter: () => this.editor.commands.splitListItem(this.name), | ||
Tab: () => this.editor.commands.sinkListItem(this.name), | ||
'Shift-Tab': () => this.editor.commands.liftListItem(this.name), | ||
}; | ||
}, | ||
}); | ||
const TextStyle = Mark.create({ | ||
name: 'textStyle', | ||
addOptions() { | ||
return { | ||
HTMLAttributes: {}, | ||
}; | ||
}, | ||
parseHTML() { | ||
return [ | ||
{ | ||
tag: 'span', | ||
getAttrs: element => { | ||
const hasStyles = element.hasAttribute('style'); | ||
if (!hasStyles) { | ||
return false; | ||
} | ||
return {}; | ||
}, | ||
}, | ||
]; | ||
}, | ||
renderHTML({ HTMLAttributes }) { | ||
return ['span', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; | ||
}, | ||
addCommands() { | ||
return { | ||
removeEmptyTextStyle: () => ({ state, commands }) => { | ||
const attributes = getMarkAttributes(state, this.type); | ||
const hasStyles = Object.entries(attributes).some(([, value]) => !!value); | ||
if (hasStyles) { | ||
return true; | ||
} | ||
return commands.unsetMark(this.name); | ||
}, | ||
}; | ||
}, | ||
}); | ||
const inputRegex = /^\s*([-+*])\s$/; | ||
@@ -10,2 +76,4 @@ const BulletList = Node.create({ | ||
HTMLAttributes: {}, | ||
keepMarks: false, | ||
keepAttributes: false, | ||
}; | ||
@@ -27,4 +95,7 @@ }, | ||
return { | ||
toggleBulletList: () => ({ commands }) => { | ||
return commands.toggleList(this.name, this.options.itemTypeName); | ||
toggleBulletList: () => ({ commands, chain }) => { | ||
if (this.options.keepAttributes) { | ||
return chain().toggleList(this.name, this.options.itemTypeName, this.options.keepMarks).updateAttributes(ListItem.name, this.editor.getAttributes(TextStyle.name)).run(); | ||
} | ||
return commands.toggleList(this.name, this.options.itemTypeName, this.options.keepMarks); | ||
}, | ||
@@ -39,7 +110,18 @@ }; | ||
addInputRules() { | ||
return [ | ||
wrappingInputRule({ | ||
let inputRule = wrappingInputRule({ | ||
find: inputRegex, | ||
type: this.type, | ||
}); | ||
if (this.options.keepMarks || this.options.keepAttributes) { | ||
inputRule = wrappingInputRule({ | ||
find: inputRegex, | ||
type: this.type, | ||
}), | ||
keepMarks: this.options.keepMarks, | ||
keepAttributes: this.options.keepAttributes, | ||
getAttributes: () => { return this.editor.getAttributes(TextStyle.name); }, | ||
editor: this.editor, | ||
}); | ||
} | ||
return [ | ||
inputRule, | ||
]; | ||
@@ -46,0 +128,0 @@ }, |
@@ -7,2 +7,68 @@ (function (global, factory) { | ||
const ListItem = core.Node.create({ | ||
name: 'listItem', | ||
addOptions() { | ||
return { | ||
HTMLAttributes: {}, | ||
}; | ||
}, | ||
content: 'paragraph block*', | ||
defining: true, | ||
parseHTML() { | ||
return [ | ||
{ | ||
tag: 'li', | ||
}, | ||
]; | ||
}, | ||
renderHTML({ HTMLAttributes }) { | ||
return ['li', core.mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; | ||
}, | ||
addKeyboardShortcuts() { | ||
return { | ||
Enter: () => this.editor.commands.splitListItem(this.name), | ||
Tab: () => this.editor.commands.sinkListItem(this.name), | ||
'Shift-Tab': () => this.editor.commands.liftListItem(this.name), | ||
}; | ||
}, | ||
}); | ||
const TextStyle = core.Mark.create({ | ||
name: 'textStyle', | ||
addOptions() { | ||
return { | ||
HTMLAttributes: {}, | ||
}; | ||
}, | ||
parseHTML() { | ||
return [ | ||
{ | ||
tag: 'span', | ||
getAttrs: element => { | ||
const hasStyles = element.hasAttribute('style'); | ||
if (!hasStyles) { | ||
return false; | ||
} | ||
return {}; | ||
}, | ||
}, | ||
]; | ||
}, | ||
renderHTML({ HTMLAttributes }) { | ||
return ['span', core.mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; | ||
}, | ||
addCommands() { | ||
return { | ||
removeEmptyTextStyle: () => ({ state, commands }) => { | ||
const attributes = core.getMarkAttributes(state, this.type); | ||
const hasStyles = Object.entries(attributes).some(([, value]) => !!value); | ||
if (hasStyles) { | ||
return true; | ||
} | ||
return commands.unsetMark(this.name); | ||
}, | ||
}; | ||
}, | ||
}); | ||
const inputRegex = /^\s*([-+*])\s$/; | ||
@@ -15,2 +81,4 @@ const BulletList = core.Node.create({ | ||
HTMLAttributes: {}, | ||
keepMarks: false, | ||
keepAttributes: false, | ||
}; | ||
@@ -32,4 +100,7 @@ }, | ||
return { | ||
toggleBulletList: () => ({ commands }) => { | ||
return commands.toggleList(this.name, this.options.itemTypeName); | ||
toggleBulletList: () => ({ commands, chain }) => { | ||
if (this.options.keepAttributes) { | ||
return chain().toggleList(this.name, this.options.itemTypeName, this.options.keepMarks).updateAttributes(ListItem.name, this.editor.getAttributes(TextStyle.name)).run(); | ||
} | ||
return commands.toggleList(this.name, this.options.itemTypeName, this.options.keepMarks); | ||
}, | ||
@@ -44,7 +115,18 @@ }; | ||
addInputRules() { | ||
return [ | ||
core.wrappingInputRule({ | ||
let inputRule = core.wrappingInputRule({ | ||
find: inputRegex, | ||
type: this.type, | ||
}); | ||
if (this.options.keepMarks || this.options.keepAttributes) { | ||
inputRule = core.wrappingInputRule({ | ||
find: inputRegex, | ||
type: this.type, | ||
}), | ||
keepMarks: this.options.keepMarks, | ||
keepAttributes: this.options.keepAttributes, | ||
getAttributes: () => { return this.editor.getAttributes(TextStyle.name); }, | ||
editor: this.editor, | ||
}); | ||
} | ||
return [ | ||
inputRule, | ||
]; | ||
@@ -51,0 +133,0 @@ }, |
@@ -5,2 +5,4 @@ import { Node } from '@tiptap/core'; | ||
HTMLAttributes: Record<string, any>; | ||
keepMarks: boolean; | ||
keepAttributes: boolean; | ||
} | ||
@@ -7,0 +9,0 @@ declare module '@tiptap/core' { |
{ | ||
"name": "@tiptap/extension-bullet-list", | ||
"description": "bullet list extension for tiptap", | ||
"version": "2.0.0-beta.218", | ||
"version": "2.0.0-beta.219", | ||
"homepage": "https://tiptap.dev", | ||
@@ -35,3 +35,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"@tiptap/core": "^2.0.0-beta.218" | ||
"@tiptap/core": "^2.0.0-beta.219" | ||
}, | ||
@@ -38,0 +38,0 @@ "repository": { |
import { mergeAttributes, Node, wrappingInputRule } from '@tiptap/core' | ||
import ListItem from '../../extension-list-item/src' | ||
import TextStyle from '../../extension-text-style/src' | ||
export interface BulletListOptions { | ||
itemTypeName: string, | ||
HTMLAttributes: Record<string, any>, | ||
keepMarks: boolean, | ||
keepAttributes: boolean, | ||
} | ||
@@ -28,2 +33,4 @@ | ||
HTMLAttributes: {}, | ||
keepMarks: false, | ||
keepAttributes: false, | ||
} | ||
@@ -50,4 +57,7 @@ }, | ||
return { | ||
toggleBulletList: () => ({ commands }) => { | ||
return commands.toggleList(this.name, this.options.itemTypeName) | ||
toggleBulletList: () => ({ commands, chain }) => { | ||
if (this.options.keepAttributes) { | ||
return chain().toggleList(this.name, this.options.itemTypeName, this.options.keepMarks).updateAttributes(ListItem.name, this.editor.getAttributes(TextStyle.name)).run() | ||
} | ||
return commands.toggleList(this.name, this.options.itemTypeName, this.options.keepMarks) | ||
}, | ||
@@ -64,9 +74,21 @@ } | ||
addInputRules() { | ||
return [ | ||
wrappingInputRule({ | ||
let inputRule = wrappingInputRule({ | ||
find: inputRegex, | ||
type: this.type, | ||
}) | ||
if (this.options.keepMarks || this.options.keepAttributes) { | ||
inputRule = wrappingInputRule({ | ||
find: inputRegex, | ||
type: this.type, | ||
}), | ||
keepMarks: this.options.keepMarks, | ||
keepAttributes: this.options.keepAttributes, | ||
getAttributes: () => { return this.editor.getAttributes(TextStyle.name) }, | ||
editor: this.editor, | ||
}) | ||
} | ||
return [ | ||
inputRule, | ||
] | ||
}, | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
43270
16
515