-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Fix dynamic content loading init problem #33748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,89 @@ | ||
import {isDocumentFragmentOrElementNode} from '../utils/dom.ts'; | ||
|
||
type DirElement = HTMLInputElement | HTMLTextAreaElement; | ||
|
||
// for performance considerations, it only uses performant syntax | ||
function attachDirAuto(el: Partial<DirElement>) { | ||
if (el.type !== 'hidden' && | ||
el.type !== 'checkbox' && | ||
el.type !== 'radio' && | ||
el.type !== 'range' && | ||
el.type !== 'color') { | ||
el.dir = 'auto'; | ||
} | ||
} | ||
|
||
type GlobalInitFunc<T extends HTMLElement> = (el: T) => void | Promise<void>; | ||
const globalInitFuncs: Record<string, GlobalInitFunc<HTMLElement>> = {}; | ||
function attachGlobalInit(el: HTMLElement) { | ||
const initFunc = el.getAttribute('data-global-init'); | ||
const func = globalInitFuncs[initFunc]; | ||
if (!func) throw new Error(`Global init function "${initFunc}" not found`); | ||
func(el); | ||
} | ||
|
||
type GlobalEventFunc<T extends HTMLElement, E extends Event> = (el: T, e: E) => (void | Promise<void>); | ||
const globalEventFuncs: Record<string, GlobalEventFunc<HTMLElement, Event>> = {}; | ||
export function registerGlobalEventFunc<T extends HTMLElement, E extends Event>(event: string, name: string, func: GlobalEventFunc<T, E>) { | ||
globalEventFuncs[`${event}:${name}`] = func as any; | ||
} | ||
|
||
type SelectorHandler = { | ||
selector: string, | ||
handler: (el: HTMLElement) => void, | ||
}; | ||
|
||
const selectorHandlers: SelectorHandler[] = [ | ||
{selector: 'input, textarea', handler: attachDirAuto}, | ||
{selector: '[data-global-init]', handler: attachGlobalInit}, | ||
]; | ||
|
||
export function observeAddedElement(selector: string, handler: (el: HTMLElement) => void) { | ||
selectorHandlers.push({selector, handler}); | ||
const docNodes = document.querySelectorAll<HTMLElement>(selector); | ||
for (const el of docNodes) { | ||
handler(el); | ||
} | ||
} | ||
|
||
export function initAddedElementObserver(): void { | ||
const observer = new MutationObserver((mutationList) => { | ||
const len = mutationList.length; | ||
for (let i = 0; i < len; i++) { | ||
const mutation = mutationList[i]; | ||
const len = mutation.addedNodes.length; | ||
for (let i = 0; i < len; i++) { | ||
const addedNode = mutation.addedNodes[i] as HTMLElement; | ||
if (!isDocumentFragmentOrElementNode(addedNode)) continue; | ||
|
||
for (const {selector, handler} of selectorHandlers) { | ||
if (addedNode.matches(selector)) { | ||
handler(addedNode); | ||
} | ||
const children = addedNode.querySelectorAll<HTMLElement>(selector); | ||
for (const el of children) { | ||
handler(el); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
for (const {selector, handler} of selectorHandlers) { | ||
const docNodes = document.querySelectorAll<HTMLElement>(selector); | ||
for (const el of docNodes) { | ||
handler(el); | ||
} | ||
} | ||
|
||
observer.observe(document, {subtree: true, childList: true}); | ||
|
||
document.addEventListener('click', (e) => { | ||
const elem = (e.target as HTMLElement).closest<HTMLElement>('[data-global-click]'); | ||
if (!elem) return; | ||
const funcName = elem.getAttribute('data-global-click'); | ||
const func = globalEventFuncs[`click:${funcName}`]; | ||
if (!func) throw new Error(`Global event function "click:${funcName}" not found`); | ||
func(elem, e); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's supposed to be
direction
, notdirectory
.I was confused for a second.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's from old "dirauto.js"
This file is a rewritten of "dirauto.js",
more thanabout a half of code is from old code.