Skip to content

Commit

Permalink
Move block types into an element which opens on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed May 26, 2024
1 parent 48fe3f7 commit ec03148
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Next version
- Dropped the dependency on ``admin/js/jquery.init.js``. We're using our own
DOM-ready handler and therefore can still access ``django.jQuery`` to hook up
the inline events handler if running inside the Django admin.
- Moved the paragraph formats into a popover.


0.3 (2024-04-09)
Expand Down
2 changes: 1 addition & 1 deletion django_prose_editor/static/django_prose_editor/editor.css

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

18 changes: 9 additions & 9 deletions django_prose_editor/static/django_prose_editor/editor.js

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,31 @@
font-size: 14px;
background: var(--body-bg);
display: inline-flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
margin-bottom: 2px;
}

.prose-menubar__dropdown {
display: flex;

> span {
border-radius: 4px;
}

> div {
display: none;
position: absolute;
background: var(--body-bg);
z-index: 1;
}

&:hover > div {
display: flex;
}
}

.prose-menubar__group {
display: flex;
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ export function createEditor(textarea, config) {
gapCursor(),
history(),
menuPlugin(
blockTypeMenuItems(schema),
[
blockTypeMenuItems(schema),
listMenuItems(schema),
linkMenuItems(schema),
markMenuItems(schema),
Expand Down
22 changes: 17 additions & 5 deletions src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
} from "./commands.js"
import { crel } from "./utils.js"

export function menuPlugin(items) {
export function menuPlugin(blockTypeItems, otherItems) {
return new Plugin({
view(editorView) {
const menuView = new MenuView(items, editorView)
const menuView = new MenuView(editorView, blockTypeItems, otherItems)
editorView.dom.parentNode.insertBefore(menuView.dom, editorView.dom)

return menuView
Expand Down Expand Up @@ -68,6 +68,8 @@ export function blockTypeMenuItems(schema) {
heading(1),
heading(2),
heading(3),
heading(4),
heading(5),
{
command: setBlockType(schema.nodes.paragraph),
dom: materialButton("notes", "paragraph"),
Expand Down Expand Up @@ -187,19 +189,29 @@ export function htmlMenuItem() {
}

class MenuView {
constructor(itemGroups, editorView) {
this.items = itemGroups.flatMap((group) => group)
constructor(editorView, blockTypeItems, otherItemGroups) {
this.items = [blockTypeItems, ...otherItemGroups].flatMap((group) => group)
this.editorView = editorView

this.dom = crel("div", { className: "prose-menubar" })

itemGroups
if (blockTypeItems.length) {
const groupDOM = crel("div", { className: "prose-menubar__dropdown" })
const dropdownElements = crel("div")
this.dom.append(groupDOM)
groupDOM.append(materialButton("notes", "paragraph"))
groupDOM.append(dropdownElements)
blockTypeItems.forEach(({ dom }) => dropdownElements.append(dom))
}

otherItemGroups
.filter((group) => group.length)
.forEach((group) => {
const groupDOM = crel("div", { className: "prose-menubar__group" })
this.dom.append(groupDOM)
group.forEach(({ dom }) => groupDOM.append(dom))
})

this.update()

this.dom.addEventListener("mousedown", (e) => {
Expand Down

0 comments on commit ec03148

Please sign in to comment.