tiptap: update preload

This commit is contained in:
fantasticit 2022-06-03 16:35:17 +08:00
parent e382686b3b
commit 4966dfc509
2 changed files with 3 additions and 24 deletions

View File

@ -10,7 +10,7 @@ import App from 'next/app';
import Head from 'next/head';
import React from 'react';
import { Hydrate, QueryClient, QueryClientProvider } from 'react-query';
import { preload } from 'tiptap/preload';
import { preloadTiptapResources } from 'tiptap/preload';
class MyApp extends App<{ isMobile: boolean }> {
state = {
@ -45,7 +45,7 @@ class MyApp extends App<{ isMobile: boolean }> {
// @ts-ignore
import('requestidlecallback-polyfill'),
]).then(() => {
preload();
preloadTiptapResources();
});
}

View File

@ -1,34 +1,13 @@
import { DOCUMENT_COVERS } from '@think/constants';
const preloadImage = (url) => {
// @ts-ignore
window.requestIdleCallback =
window.requestIdleCallback ||
function (cb) {
const start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start));
},
});
}, 1);
};
window.cancelIdleCallback =
window.cancelIdleCallback ||
function (id) {
clearTimeout(id);
};
requestIdleCallback(() => {
const image = document.createElement('img');
image.src = url;
});
};
export function preload() {
export function preloadTiptapResources() {
if (typeof window === 'undefined') return;
DOCUMENT_COVERS.forEach(preloadImage);
}