-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: #3540 Ability to preserve marks on lists #3541
Changes from all commits
ad70224
73c96f5
9161e1e
0ee1515
f82c85c
50b95b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { Node as ProseMirrorNode, NodeType } from '@tiptap/pm/model' | ||
import { canJoin, findWrapping } from '@tiptap/pm/transform' | ||
|
||
import { Editor } from '../Editor' | ||
import { InputRule, InputRuleFinder } from '../InputRule' | ||
import { ExtendedRegExpMatchArray } from '../types' | ||
import { callOrReturn } from '../utilities/callOrReturn' | ||
|
@@ -20,18 +21,24 @@ import { callOrReturn } from '../utilities/callOrReturn' | |
* return a boolean to indicate whether a join should happen. | ||
*/ | ||
export function wrappingInputRule(config: { | ||
find: InputRuleFinder | ||
type: NodeType | ||
find: InputRuleFinder, | ||
type: NodeType, | ||
keepMarks?: boolean, | ||
keepAttributes?: boolean, | ||
editor?: Editor | ||
getAttributes?: | ||
| Record<string, any> | ||
| ((match: ExtendedRegExpMatchArray) => Record<string, any>) | ||
| false | ||
| null | ||
joinPredicate?: (match: ExtendedRegExpMatchArray, node: ProseMirrorNode) => boolean | ||
| Record<string, any> | ||
| ((match: ExtendedRegExpMatchArray) => Record<string, any>) | ||
| false | ||
| null | ||
, | ||
joinPredicate?: (match: ExtendedRegExpMatchArray, node: ProseMirrorNode) => boolean, | ||
}) { | ||
return new InputRule({ | ||
find: config.find, | ||
handler: ({ state, range, match }) => { | ||
handler: ({ | ||
state, range, match, chain, | ||
}) => { | ||
const attributes = callOrReturn(config.getAttributes, undefined, match) || {} | ||
const tr = state.tr.delete(range.from, range.to) | ||
const $start = tr.doc.resolve(range.from) | ||
|
@@ -44,6 +51,24 @@ export function wrappingInputRule(config: { | |
|
||
tr.wrap(blockRange, wrapping) | ||
|
||
if (config.keepMarks && config.editor) { | ||
const { selection, storedMarks } = state | ||
const { splittableMarks } = config.editor.extensionManager | ||
const marks = storedMarks || (selection.$to.parentOffset && selection.$from.marks()) | ||
|
||
if (marks) { | ||
const filteredMarks = marks.filter(mark => splittableMarks.includes(mark.type.name)) | ||
|
||
tr.ensureMarks(filteredMarks) | ||
} | ||
} | ||
if (config.keepAttributes) { | ||
/** If the nodeType is `bulletList` or `orderedList` set the `nodeType` as `listItem` */ | ||
const nodeType = config.type.name === 'bulletList' || config.type.name === 'orderedList' ? 'listItem' : 'taskList' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bdbch I'm just worried about this piece of code, please let me know if there's any better way of doing this. The idea here is
|
||
|
||
chain().updateAttributes(nodeType, attributes).run() | ||
} | ||
|
||
const before = tr.doc.resolve(range.from - 1).nodeBefore | ||
|
||
if ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import { mergeAttributes, Node, wrappingInputRule } from '@tiptap/core' | ||
|
||
import ListItem from '../../extension-list-item/src' | ||
import TextStyle from '../../extension-text-style/src' | ||
|
||
export interface OrderedListOptions { | ||
itemTypeName: string, | ||
HTMLAttributes: Record<string, any>, | ||
keepMarks: boolean, | ||
keepAttributes: boolean, | ||
} | ||
|
||
declare module '@tiptap/core' { | ||
|
@@ -25,6 +30,8 @@ export const OrderedList = Node.create<OrderedListOptions>({ | |
return { | ||
itemTypeName: 'listItem', | ||
HTMLAttributes: {}, | ||
keepMarks: false, | ||
keepAttributes: false, | ||
} | ||
}, | ||
|
||
|
@@ -65,8 +72,11 @@ export const OrderedList = Node.create<OrderedListOptions>({ | |
|
||
addCommands() { | ||
return { | ||
toggleOrderedList: () => ({ commands }) => { | ||
return commands.toggleList(this.name, this.options.itemTypeName) | ||
toggleOrderedList: () => ({ 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) | ||
}, | ||
} | ||
}, | ||
|
@@ -78,13 +88,23 @@ export const OrderedList = Node.create<OrderedListOptions>({ | |
}, | ||
|
||
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, | ||
getAttributes: match => ({ start: +match[1] }), | ||
joinPredicate: (match, node) => node.childCount + node.attrs.start === +match[1], | ||
Comment on lines
-85
to
-86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe these removed lines broke support for creating ordered lists with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}), | ||
keepMarks: this.options.keepMarks, | ||
keepAttributes: this.options.keepAttributes, | ||
getAttributes: () => { return this.editor.getAttributes(TextStyle.name) }, | ||
editor: this.editor, | ||
}) | ||
} | ||
return [ | ||
inputRule, | ||
] | ||
}, | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What exactly happens with marks when list items keep their attributes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's say I have my first line with
Purple
color andItalic
mark applied. When we setkeepAttributes
totrue
I am trying to update it with whatever attributes that are there from the previous line. But while doing so, if we have any mark it is not being preserved. But even without keeping thekeepAttributes
totrue
the color from the<p>
is being preserved on the bullet list.In this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My doubt is do we need to chain the
updateAttributes
here in the above mentioned line ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so - but shouldn't be a problem. Thanks for coming up with more information! I think this should also close #3768