-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use decorations for heading anchors
This avoids transactions that actually change the document state. Fixes #5861. Signed-off-by: Max <[email protected]>
- Loading branch information
1 parent
9523e0a
commit a1d2552
Showing
12 changed files
with
270 additions
and
230 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import TipTapHeading from '@tiptap/extension-heading' | ||
import headingAnchor, { headingAnchorPluginKey } from '../plugins/headingAnchor.js' | ||
import store from '../store/index.js' | ||
|
||
const setHeadings = (val) => store.dispatch('text/setHeadings', val) | ||
|
||
const Heading = TipTapHeading.extend({ | ||
|
||
addKeyboardShortcuts() { | ||
return this.options.levels.reduce((items, level) => ({ | ||
...items, | ||
[`Mod-Shift-${level}`]: () => this.editor.commands.toggleHeading({ level }), | ||
}), {}) | ||
}, | ||
|
||
// sync heading data structure to the vuex store | ||
onUpdate({ editor }) { | ||
const headings = headingAnchorPluginKey | ||
.getState(editor.state)?.headings ?? [] | ||
setHeadings(headings) | ||
}, | ||
|
||
addProseMirrorPlugins() { | ||
return [headingAnchor()] | ||
}, | ||
|
||
}) | ||
|
||
export default Heading |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,48 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { slugify } from './slug.js' | ||
|
||
/** | ||
* Extract heading data structure from doc | ||
* | ||
* @param {Document} doc - the prosemirror doc | ||
* @return {Array} headings found in the doc | ||
*/ | ||
export default function extractHeadings(doc) { | ||
const counter = new Map() | ||
const headings = [] | ||
|
||
const getId = text => { | ||
const id = slugify(text) | ||
if (counter.has(id)) { | ||
const next = counter.get(id) | ||
// increment counter | ||
counter.set(id, next + 1) | ||
return `h-${id}--${next}` | ||
} | ||
// define counter | ||
counter.set(id, 1) | ||
return 'h-' + id | ||
} | ||
|
||
doc.descendants((node, offset) => { | ||
if (node.type.name !== 'heading') { | ||
return | ||
} | ||
const text = node.textContent | ||
// ignore empty headings | ||
if (!text) return | ||
const id = getId(text) | ||
headings.push(Object.freeze({ | ||
level: node.attrs.level, | ||
text, | ||
id, | ||
offset, | ||
})) | ||
}) | ||
|
||
return headings | ||
} |
Oops, something went wrong.