forked from Milkdown/milkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 make placeholder of slash plugin configurable
- Loading branch information
1 parent
bf016ab
commit e14eff5
Showing
6 changed files
with
201 additions
and
167 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,49 @@ | ||
/* Copyright 2021, Milkdown by Mirone. */ | ||
import scrollIntoView from 'smooth-scroll-into-view-if-needed'; | ||
|
||
import { Action } from '../item'; | ||
import { Status } from './status'; | ||
|
||
export const renderDropdown = (status: Status, dropdownElement: HTMLElement, items: Action[]): boolean => { | ||
const { filter } = status.get(); | ||
|
||
if (!status.isSlash()) { | ||
dropdownElement.classList.add('hide'); | ||
return false; | ||
} | ||
|
||
const activeList = items | ||
.filter((item) => { | ||
item.$.classList.remove('active'); | ||
const result = item.keyword.some((key) => key.includes(filter.toLocaleLowerCase())); | ||
if (result) { | ||
return true; | ||
} | ||
item.$.classList.add('hide'); | ||
return false; | ||
}) | ||
.map((item) => { | ||
item.$.classList.remove('hide'); | ||
return item; | ||
}); | ||
|
||
status.setActions(activeList); | ||
|
||
if (activeList.length === 0) { | ||
dropdownElement.classList.add('hide'); | ||
return false; | ||
} | ||
|
||
dropdownElement.classList.remove('hide'); | ||
|
||
activeList[0].$.classList.add('active'); | ||
requestAnimationFrame(() => { | ||
scrollIntoView(activeList[0].$, { | ||
scrollMode: 'if-needed', | ||
block: 'nearest', | ||
inline: 'nearest', | ||
}); | ||
}); | ||
|
||
return true; | ||
}; |
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,120 @@ | ||
/* Copyright 2021, Milkdown by Mirone. */ | ||
|
||
import { EditorView } from 'prosemirror-view'; | ||
import scrollIntoView from 'smooth-scroll-into-view-if-needed'; | ||
|
||
import { Action } from '../item'; | ||
import { Status } from './status'; | ||
|
||
export const createMouseManager = () => { | ||
let mouseLock = false; | ||
|
||
return { | ||
isLock: () => mouseLock, | ||
lock: () => { | ||
mouseLock = true; | ||
}, | ||
unlock: () => { | ||
mouseLock = false; | ||
}, | ||
}; | ||
}; | ||
export type MouseManager = ReturnType<typeof createMouseManager>; | ||
|
||
export const handleMouseMove = (mouseManager: MouseManager) => () => { | ||
mouseManager.unlock(); | ||
}; | ||
|
||
export const handleMouseEnter = (status: Status, mouseManager: MouseManager) => (e: MouseEvent) => { | ||
if (mouseManager.isLock()) return; | ||
const active = status.get().activeActions.findIndex((x) => x.$.classList.contains('active')); | ||
if (active >= 0) { | ||
status.get().activeActions[active].$.classList.remove('active'); | ||
} | ||
const { target } = e; | ||
if (!(target instanceof HTMLElement)) return; | ||
target.classList.add('active'); | ||
}; | ||
|
||
export const handleMouseLeave = () => (e: MouseEvent) => { | ||
const { target } = e; | ||
if (!(target instanceof HTMLElement)) return; | ||
target.classList.remove('active'); | ||
}; | ||
|
||
export const handleClick = | ||
(status: Status, items: Action[], view: EditorView, dropdownElement: HTMLElement) => | ||
(e: Event): void => { | ||
const { target } = e; | ||
if (!(target instanceof HTMLElement)) return; | ||
if (!view) return; | ||
|
||
const stop = () => { | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
}; | ||
|
||
const el = Object.values(items).find(({ $ }) => $.contains(target)); | ||
if (!el) { | ||
if (status.isEmpty()) return; | ||
|
||
status.clearStatus(); | ||
dropdownElement.classList.add('hide'); | ||
stop(); | ||
|
||
return; | ||
} | ||
|
||
stop(); | ||
el.command(view.state, view.dispatch, view); | ||
}; | ||
|
||
export const handleKeydown = | ||
(status: Status, view: EditorView, dropdownElement: HTMLElement, mouseManager: MouseManager) => (e: Event) => { | ||
if (!(e instanceof KeyboardEvent)) return; | ||
if (!mouseManager.isLock()) mouseManager.lock(); | ||
|
||
const { key } = e; | ||
if (!status.isSlash()) return; | ||
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Escape'].includes(key)) return; | ||
|
||
const { activeActions } = status.get(); | ||
|
||
let active = activeActions.findIndex(({ $ }) => $.classList.contains('active')); | ||
if (active < 0) active = 0; | ||
|
||
const moveActive = (next: number) => { | ||
activeActions[active].$.classList.remove('active'); | ||
activeActions[next].$.classList.add('active'); | ||
scrollIntoView(activeActions[next].$, { | ||
scrollMode: 'if-needed', | ||
block: 'nearest', | ||
inline: 'nearest', | ||
}); | ||
}; | ||
|
||
if (key === 'ArrowDown') { | ||
const next = active === activeActions.length - 1 ? 0 : active + 1; | ||
|
||
moveActive(next); | ||
return; | ||
} | ||
|
||
if (key === 'ArrowUp') { | ||
const next = active === 0 ? activeActions.length - 1 : active - 1; | ||
|
||
moveActive(next); | ||
return; | ||
} | ||
|
||
if (key === 'Escape') { | ||
if (status.isEmpty()) return; | ||
|
||
status.clearStatus(); | ||
dropdownElement.classList.add('hide'); | ||
return; | ||
} | ||
|
||
activeActions[active].command(view.state, view.dispatch, view); | ||
activeActions[active].$.classList.remove('active'); | ||
}; |
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.