Skip to content

Commit

Permalink
Extract the menu items creation
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Dec 3, 2024
1 parent 6a687c2 commit 59b5ff3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 40 deletions.
36 changes: 18 additions & 18 deletions django_prose_editor/static/django_prose_editor/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion django_prose_editor/static/django_prose_editor/init.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function createEditor(textarea) {
Underline,
Link,
Menu,
menuItemsFromConfig,
NoSpellCheck,
Typographic,
createTextareaEditor,
Expand All @@ -42,7 +43,7 @@ function createEditor(textarea) {
Paragraph,
HardBreak,
Text,
Menu.configure({ config }),
Menu.configure({ menuItems: menuItemsFromConfig(config) }),
NoSpellCheck,
config.typographic && Typographic,
// Nodes and marks
Expand Down
3 changes: 1 addition & 2 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Underline } from "@tiptap/extension-underline"

import { updateAttrsDialog } from "./commands.js"
import { Link } from "./link.js"
import { Menu } from "./menu.js"
export * from "./menu.js"
import { NoSpellCheck } from "./nospellcheck.js"
import { Typographic } from "./typographic.js"
import { crel } from "./utils.js"
Expand Down Expand Up @@ -60,7 +60,6 @@ export {
Underline,
Link,
/* others */
Menu,
NoSpellCheck,
Typographic,
}
Expand Down
32 changes: 14 additions & 18 deletions src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,29 @@ import { undo, redo } from "@tiptap/pm/history"
import { wrapInList } from "@tiptap/pm/schema-list"
import { Plugin } from "@tiptap/pm/state"

export const menuItemsFromConfig = (config) => (editor) => {
const schema = editor.schema
return [
blockTypeMenuItems(schema, config.headingLevels),
listMenuItems(schema),
linkMenuItems(schema),
markMenuItems(schema),
config.history ? historyMenuItems() : null,
config.html ? htmlMenuItem() : null,
].filter(Boolean)
}

export const Menu = Extension.create({
name: "menu",

addProseMirrorPlugins() {
const schema = this.editor.schema
const config = this.options.config

// console.debug("this", this)
// console.debug("arguments", arguments)
// console.debug("schema", schema)
const menuItems = this.options.menuItems(this.editor)

return [
new Plugin({
view(editorView) {
const menuView = new MenuView(
editorView,
[
blockTypeMenuItems(schema, config.headingLevels),
listMenuItems(schema),
linkMenuItems(schema),
markMenuItems(schema),
config.history ? historyMenuItems() : null,
config.html ? htmlMenuItem() : null,
].filter(Boolean),
)
const menuView = new MenuView(editorView, menuItems)
editorView.dom.parentNode.insertBefore(menuView.dom, editorView.dom)

return menuView
},
}),
Expand Down

0 comments on commit 59b5ff3

Please sign in to comment.