From 537e950d41d2258021b9faa341977b7b7fa3af39 Mon Sep 17 00:00:00 2001 From: Teemu Koivisto Date: Wed, 29 May 2024 22:07:26 +0300 Subject: [PATCH] remove experimental code --- packages/extension/src/inject/AsyncQueue.ts | 47 ------ .../extension/src/inject/findEditorViews2.ts | 156 ------------------ .../extension/src/inject/pmViewDescHack.ts | 2 + 3 files changed, 2 insertions(+), 203 deletions(-) delete mode 100644 packages/extension/src/inject/AsyncQueue.ts delete mode 100644 packages/extension/src/inject/findEditorViews2.ts diff --git a/packages/extension/src/inject/AsyncQueue.ts b/packages/extension/src/inject/AsyncQueue.ts deleted file mode 100644 index 29716b0..0000000 --- a/packages/extension/src/inject/AsyncQueue.ts +++ /dev/null @@ -1,47 +0,0 @@ -export class AsyncQueue { - private readonly queue: Promise[] = [] - private readonly timeoutSeconds: number - private timeout: ReturnType | undefined - private reject = () => {} - private resolve = (value: T | PromiseLike) => {} - - /** - * @param timeout timeout in seconds @default 25 - */ - constructor(timeout = 25) { - this.timeoutSeconds = timeout - if (timeout > 100) { - console.warn(`You are initializing AsyncQueue with over 100s timeout: ${timeout}`) - } - this.queue.push( - new Promise((resolve, reject) => { - this.resolve = resolve - this.reject = reject - this.timeout = setTimeout(() => resolve(undefined), timeout * 1000) - }) - ) - } - - next(): Promise | undefined { - return this.queue.shift() - } - - push(msg: T) { - this.resolve(msg) - this.queue.push( - new Promise((resolve, reject) => { - this.resolve = resolve - this.reject = reject - clearTimeout(this.timeout) - this.timeout = setTimeout(() => resolve(undefined), this.timeoutSeconds * 1000) - }) - ) - } - - close(msg?: T) { - if (msg) { - this.resolve(msg) - } - clearTimeout(this.timeout) - } -} diff --git a/packages/extension/src/inject/findEditorViews2.ts b/packages/extension/src/inject/findEditorViews2.ts deleted file mode 100644 index 2bcaf0b..0000000 --- a/packages/extension/src/inject/findEditorViews2.ts +++ /dev/null @@ -1,156 +0,0 @@ -import type { EditorView } from 'prosemirror-view' - -import type { InjectState } from '../types' - -import { AsyncQueue } from './AsyncQueue' -import { getEditorView } from './pmViewDescHack' -import { sleep, tryQueryIframe } from './utils' - -const MAX_ATTEMPTS = 10 - -interface FoundEditorView { - type: 'found-editor' - data: EditorView -} -interface FoundIframeEditor { - type: 'found-iframe-editor' - data: EditorView -} -interface Errored { - type: 'error' - err: string -} -interface Finished { - type: 'finished' -} -type Evt = FoundEditorView | FoundIframeEditor | Errored | Finished - -async function findIframeEditorViews(selector: string) { - const els = await Promise.all( - Array.from(document.querySelectorAll('iframe')).map(iframe => tryQueryIframe(iframe, selector)) - ) - return els.flat().map(el => el as HTMLElement) -} - -async function* findEditorViewsGen( - selector: string, - controller: AbortController -): AsyncGenerator { - const queue = new AsyncQueue(1000) - let done = false - - controller.signal.addEventListener('abort', () => { - queue.push({ type: 'finished' }) - }) - - const views = Array.from(document.querySelectorAll(selector)).map(el => - getEditorView(el as HTMLElement) - .then(res => { - if (res) { - queue.push({ type: 'found-editor', data: res }) - } - return res - }) - .catch(err => { - return undefined - }) - ) - const iframes = findIframeEditorViews(selector).then(els => - els.map(el => - getEditorView(el) - .then(res => { - if (res) { - queue.push({ type: 'found-iframe-editor', data: res }) - } - return res - }) - .catch(err => { - return undefined - }) - ) - ) - Promise.allSettled([...views, iframes]).then(() => { - queue.push({ type: 'finished' }) - }) - while (!done) { - const msg = await queue.next() - if (msg) { - yield msg - done = msg.type === 'finished' - } else { - queue.push({ - type: 'error', - err: 'Finding editor views timeouted' - }) - } - } -} - -async function find(state: InjectState, attempts = 0) { - await sleep(1000 * attempts) - try { - const { - disabled, - inject: { selector } - } = state - if (disabled) { - return [] - } - const viewEls = Array.from(document.querySelectorAll(selector)) - const iframeEls = Array.from(document.querySelectorAll('iframe')) - } catch (err) { - console.error(err) - return undefined - } -} - -export async function findEditorViews( - state: InjectState, - attempts = 0 -): Promise { - await sleep(1000 * attempts) - try { - const { - disabled, - inject: { selector } - } = state - if (disabled) { - return [] - } - console.log('start') - // const views: any[] = [] - const views = await Promise.all( - Array.from(document.querySelectorAll(selector)).map(el => - getEditorView(el as HTMLElement).catch(err => { - return undefined - }) - ) - ) - console.log('views', views) - const iframes = ( - await Promise.all( - Array.from(document.querySelectorAll('iframe')).map(iframe => - tryQueryIframe(iframe, selector) - ) - ) - ).flat() - console.log('iframes', iframes) - const iframeViews = await Promise.all( - iframes.map(el => - getEditorView(el as HTMLElement).catch(err => { - return undefined - }) - ) - ) - console.log('wut') - const filtered = views.concat(iframeViews).filter((v): v is EditorView => v !== undefined) - if (filtered.length === 0 && attempts < MAX_ATTEMPTS) { - console.log('try again?') - return findEditorViews(state, attempts + 1) - } - return filtered - } catch (err) { - console.error(err) - return undefined - } -} diff --git a/packages/extension/src/inject/pmViewDescHack.ts b/packages/extension/src/inject/pmViewDescHack.ts index 092a2ee..dd1ff61 100644 --- a/packages/extension/src/inject/pmViewDescHack.ts +++ b/packages/extension/src/inject/pmViewDescHack.ts @@ -15,6 +15,7 @@ function recurseElementsIntoHackPromises(el: HTMLElement, opts: GetEditorViewOpt if ( opts.promises.length < opts.max && child.pmViewDesc && + // Skip custom NodeViews since they seem to bug out with the hack child.pmViewDesc.constructor.name !== 'CustomNodeViewDesc' ) { opts.promises.push(runPmViewDescHack(el, child, opts)) @@ -33,6 +34,7 @@ async function runPmViewDescHack( return { err: 'Finding aborted', code: 400 } } let oldFn = parent.pmViewDesc?.updateChildren + // If the same updateChildren is patched twice, the pmViewDesc gets broken. Ups const alreadyPatched = opts.oldFns.has(parent) if (!alreadyPatched && oldFn) { opts.oldFns.set(parent, oldFn)