-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: heading not updating with realtime
- Loading branch information
1 parent
f71ca58
commit e3a8eab
Showing
11 changed files
with
118 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Extension } from "@tiptap/core"; | ||
import { Plugin, PluginKey } from "prosemirror-state"; | ||
|
||
export interface IMarking { | ||
type: "heading"; | ||
level: number; | ||
text: string; | ||
sequence: number; | ||
} | ||
|
||
export const HeadingListExtension = Extension.create({ | ||
name: "headingList", | ||
|
||
addStorage() { | ||
return { | ||
headings: [] as IMarking[], | ||
}; | ||
}, | ||
|
||
addProseMirrorPlugins() { | ||
const plugin = new Plugin({ | ||
key: new PluginKey("heading-list"), | ||
appendTransaction: (_, __, newState) => { | ||
const headings: IMarking[] = []; | ||
let h1Sequence = 0; | ||
let h2Sequence = 0; | ||
let h3Sequence = 0; | ||
|
||
newState.doc.descendants((node) => { | ||
if (node.type.name === "heading") { | ||
const level = node.attrs.level; | ||
const text = node.textContent; | ||
|
||
headings.push({ | ||
type: "heading", | ||
level: level, | ||
text: text, | ||
sequence: level === 1 ? ++h1Sequence : level === 2 ? ++h2Sequence : ++h3Sequence, | ||
}); | ||
} | ||
}); | ||
|
||
this.storage.headings = headings; | ||
|
||
this.editor.emit("update", { editor: this.editor, transaction: newState.tr }); | ||
|
||
return null; | ||
}, | ||
}); | ||
|
||
return [plugin]; | ||
}, | ||
|
||
getHeadings() { | ||
return this.storage.headings; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.