Skip to content

Commit

Permalink
Add Shadow DOM support for sendComment action (#4843)
Browse files Browse the repository at this point in the history
  • Loading branch information
the1812 committed Sep 12, 2024
1 parent 98d8c57 commit 1801764
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"cntr",
"compositionend",
"compositionstart",
"contenteditable",
"csrf",
"ctnr",
"Danmaku",
Expand Down
15 changes: 12 additions & 3 deletions registry/lib/components/utils/keymap/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getComponentSettings } from '@/core/settings'
import { registerAndGetData } from '@/plugins/data'
import { Options } from '.'
import { KeyBindingAction, KeyBindingActionContext } from './bindings'
import { simulateClick } from '@/core/utils'
import { getActiveElement, simulateClick } from '@/core/utils'

export const keyboardEventToPointer = (event: KeyboardEvent): PointerEventInit => {
return {
Expand Down Expand Up @@ -239,15 +239,24 @@ export const builtInActions: Record<string, KeyBindingAction> = {
sendComment: {
displayName: '发送评论',
ignoreTyping: false,
prevent: true,
run: () => {
const { activeElement } = document
if (!activeElement || !(activeElement instanceof HTMLTextAreaElement)) {
const activeElement = getActiveElement()
if (!activeElement) {
return null
}
const isEditable =
activeElement instanceof HTMLTextAreaElement ||
activeElement.hasAttribute('contenteditable')
if (!isEditable) {
return null
}
const getShadowRoot = (node: Node) => node.getRootNode() as ShadowRoot | null
const sendButton = (() => {
const candidates = [
() => activeElement.nextElementSibling,
() => activeElement.parentElement.nextElementSibling,
() => getShadowRoot(getShadowRoot(activeElement)?.host)?.querySelector('#pub button'),
() => dq('.reply-box:focus-within .reply-box-send'),
]
const match = candidates.find(fn => fn() !== null)
Expand Down
10 changes: 8 additions & 2 deletions registry/lib/components/utils/keymap/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isTyping, matchUrlPattern } from '@/core/utils'
import { mediaListUrls, watchlaterUrls } from '@/core/utils/urls'
import { clickElement, changeVideoTime, showTip } from './actions'
import { shadowDomObserver } from '@/core/shadow-root'

export interface KeyBindingActionContext {
binding: KeyBinding
Expand Down Expand Up @@ -29,7 +30,7 @@ export const loadKeyBindings = lodash.once((bindings: KeyBinding[]) => {
enable: true,
bindings,
}
document.body.addEventListener('keydown', (e: KeyboardEvent & { [key: string]: boolean }) => {
const keyboardHandler = (e: KeyboardEvent & { [key: string]: boolean }) => {
if (!config.enable) {
return
}
Expand Down Expand Up @@ -87,10 +88,15 @@ export const loadKeyBindings = lodash.once((bindings: KeyBinding[]) => {

const actionSuccess = !lodash.isNil(actionResult)
if (binding.action.prevent ?? actionSuccess) {
e.stopPropagation()
e.stopImmediatePropagation()
e.preventDefault()
}
})
}
document.body.addEventListener('keydown', keyboardHandler, { capture: true })
shadowDomObserver.watchShadowDom({
added: shadowDom =>
shadowDom.shadowRoot.addEventListener('keydown', keyboardHandler, { capture: true }),
})
return config
})
Expand Down

0 comments on commit 1801764

Please sign in to comment.