adjust native

This commit is contained in:
Pete Matsyburka committed 2026-09-23 11:00:17 +03:00
1 parent a86f896694
commit d35538e2ba
17 files changed
+121 -96

No files matched your search

+1 -3
View File
@@ -1,5 +1,3 @@
let counter = 0
export default class extends HTMLElement {
connectedCallback () {
const bridge = window.webkit?.messageHandlers?.native
@@ -10,7 +8,7 @@ export default class extends HTMLElement {
if (!target && !this.dataset.native) return
this.nativeId ||= `native-action-${++counter}`
this.nativeId ||= `native-action-${Math.random().toString(32).split('.')[1]}`
this.id = this.nativeId
if (target) target.dataset.nativeId = this.nativeId
-74
View File
@@ -1,74 +0,0 @@
const handlers = window.webkit?.messageHandlers
if (handlers?.native && window === window.top) {
handlers.native.postMessage({ type: 'reset' })
const reportPathConfiguration = () => {
const element = document.getElementById('native_path_configuration')
if (element) handlers.native.postMessage({ type: 'path-configuration', json: element.textContent })
}
document.addEventListener('turbo:load', reportPathConfiguration)
reportPathConfiguration()
const reportFlash = (element) => {
if (!element) return
if (element.dataset.notice) handlers.flash.postMessage({ style: 'notice', message: element.dataset.notice })
if (element.dataset.alert) handlers.flash.postMessage({ style: 'alert', message: element.dataset.alert })
element.remove()
}
const reportPageFlash = () => reportFlash(document.getElementById('native_flash'))
new MutationObserver(reportPageFlash).observe(document.documentElement, { childList: true, subtree: true })
document.addEventListener('turbo:load', reportPageFlash)
document.addEventListener('native:flash', (event) => {
reportFlash(new DOMParser().parseFromString(event.detail.html, 'text/html').getElementById('native_flash'))
})
reportPageFlash()
document.addEventListener('click', (event) => {
const link = event.target.closest?.('a[href]')
if (!link) return
const action = link.closest('native-action, native-menu-action')
const title = link.dataset.nativeTitle || link.getAttribute('aria-label') || link.closest('[data-native-title]')?.dataset.nativeTitle || action?.dataset.label || action?.textContent.replace(/\s+/g, ' ').trim()
if (!title) return
const actions = link.closest('[data-native-actions]')?.dataset.nativeActions
handlers.native.postMessage({ type: 'title', url: link.href, title, actions: actions ? actions.split(',') : null })
}, true)
document.addEventListener('click', (event) => {
if (document.querySelector('native-modal')) return
const link = event.target.closest?.('a[data-turbo-frame="modal"], a[data-turbo-frame="drawer"]')
if (!link?.href) return
event.preventDefault()
event.stopPropagation()
handlers.modal.postMessage({ action: 'open', url: link.href, modal: link.closest('[data-native-modal]')?.dataset.nativeModal || '' })
}, true)
document.addEventListener('native:action', (event) => {
document.querySelector(`[data-native-id="${event.detail.id}"]`)?.click()
})
document.addEventListener('native:title-bottom', (event) => {
const rect = document.querySelector('h1, h2')?.getBoundingClientRect()
event.detail.result = rect?.height ? rect.bottom - document.documentElement.getBoundingClientRect().top : null
})
}
+1 -3
View File
@@ -1,5 +1,3 @@
let counter = 0
export default class extends HTMLElement {
connectedCallback () {
if (window.webkit?.messageHandlers?.native) {
@@ -28,7 +26,7 @@ export default class extends HTMLElement {
const items = [...this.querySelectorAll('native-menu-action')].map((action) => {
const target = action.querySelector('a, button, [role="button"]') || action.firstElementChild
target.dataset.nativeId ||= `native-menu-${++counter}`
target.dataset.nativeId ||= `native-menu-${Math.random().toString(32).split('.')[1]}`
return {
id: target.dataset.nativeId,