diff --git a/.eslintignore b/.eslintignore index 1ee720d4763..b90a794501d 100644 --- a/.eslintignore +++ b/.eslintignore @@ -289,7 +289,6 @@ packages/app-desktop/gui/NoteEditor/utils/useEffectiveNoteId.js packages/app-desktop/gui/NoteEditor/utils/useFolder.js packages/app-desktop/gui/NoteEditor/utils/useFormNote.test.js packages/app-desktop/gui/NoteEditor/utils/useFormNote.js -packages/app-desktop/gui/NoteEditor/utils/useMarkupToHtml.js packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.js packages/app-desktop/gui/NoteEditor/utils/useNoteSearchBar.js packages/app-desktop/gui/NoteEditor/utils/usePluginEditorView.test.js @@ -480,6 +479,7 @@ packages/app-desktop/gui/hooks/useDocument.js packages/app-desktop/gui/hooks/useEffectDebugger.js packages/app-desktop/gui/hooks/useElementHeight.js packages/app-desktop/gui/hooks/useImperativeHandlerDebugger.js +packages/app-desktop/gui/hooks/useMarkupToHtml.js packages/app-desktop/gui/hooks/usePrevious.js packages/app-desktop/gui/hooks/usePropsDebugger.js packages/app-desktop/gui/lib/SearchInput/SearchInput.js diff --git a/.gitignore b/.gitignore index d6eadbd0d9f..0a2290d2a4c 100644 --- a/.gitignore +++ b/.gitignore @@ -264,7 +264,6 @@ packages/app-desktop/gui/NoteEditor/utils/useEffectiveNoteId.js packages/app-desktop/gui/NoteEditor/utils/useFolder.js packages/app-desktop/gui/NoteEditor/utils/useFormNote.test.js packages/app-desktop/gui/NoteEditor/utils/useFormNote.js -packages/app-desktop/gui/NoteEditor/utils/useMarkupToHtml.js packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.js packages/app-desktop/gui/NoteEditor/utils/useNoteSearchBar.js packages/app-desktop/gui/NoteEditor/utils/usePluginEditorView.test.js @@ -455,6 +454,7 @@ packages/app-desktop/gui/hooks/useDocument.js packages/app-desktop/gui/hooks/useEffectDebugger.js packages/app-desktop/gui/hooks/useElementHeight.js packages/app-desktop/gui/hooks/useImperativeHandlerDebugger.js +packages/app-desktop/gui/hooks/useMarkupToHtml.js packages/app-desktop/gui/hooks/usePrevious.js packages/app-desktop/gui/hooks/usePropsDebugger.js packages/app-desktop/gui/lib/SearchInput/SearchInput.js diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v5/CodeMirror.tsx b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v5/CodeMirror.tsx index 5b1bd0e683f..03014784c40 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v5/CodeMirror.tsx +++ b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v5/CodeMirror.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import { useState, useEffect, useRef, forwardRef, useCallback, useImperativeHandle, ForwardedRef, useContext } from 'react'; // eslint-disable-next-line no-unused-vars -import { EditorCommand, MarkupToHtmlOptions, NoteBodyEditorProps, NoteBodyEditorRef } from '../../../utils/types'; +import { EditorCommand, NoteBodyEditorProps, NoteBodyEditorRef } from '../../../utils/types'; import { commandAttachFileToBody, getResourcesFromPasteEvent } from '../../../utils/resourceHandling'; import { ScrollOptions, ScrollOptionTypes } from '../../../utils/types'; import { CommandValue } from '../../../utils/types'; @@ -34,6 +34,7 @@ import useWebviewIpcMessage from '../utils/useWebviewIpcMessage'; import useEditorSearchHandler from '../utils/useEditorSearchHandler'; import { focus } from '@joplin/lib/utils/focusHandler'; import { WindowIdContext } from '../../../../NewWindowOrIFrame'; +import { MarkupToHtmlOptions } from '../../../../hooks/useMarkupToHtml'; function markupRenderOptions(override: MarkupToHtmlOptions = null): MarkupToHtmlOptions { return { ...override }; diff --git a/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx b/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx index a6f38acb749..a8e16f2e965 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx +++ b/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx @@ -9,7 +9,7 @@ import useNoteSearchBar from './utils/useNoteSearchBar'; import useMessageHandler from './utils/useMessageHandler'; import useWindowCommandHandler from './utils/useWindowCommandHandler'; import useDropHandler from './utils/useDropHandler'; -import useMarkupToHtml from './utils/useMarkupToHtml'; +import useMarkupToHtml from '../hooks/useMarkupToHtml'; import useFormNote, { OnLoadEvent, OnSetFormNote } from './utils/useFormNote'; import useEffectiveNoteId from './utils/useEffectiveNoteId'; import useFolder from './utils/useFolder'; diff --git a/packages/app-desktop/gui/NoteEditor/utils/types.ts b/packages/app-desktop/gui/NoteEditor/utils/types.ts index 4d8de318f02..79f79b014a2 100644 --- a/packages/app-desktop/gui/NoteEditor/utils/types.ts +++ b/packages/app-desktop/gui/NoteEditor/utils/types.ts @@ -9,6 +9,7 @@ import { DropHandler } from './useDropHandler'; import { SearchMarkers } from './useSearchMarkers'; import { ParseOptions } from '@joplin/lib/HtmlToMd'; import { ScrollStrategy } from '@joplin/editor/CodeMirror/CodeMirrorControl'; +import { MarkupToHtmlOptions } from '../../hooks/useMarkupToHtml'; export interface AllAssetsOptions { contentMaxWidthTarget?: string; @@ -73,23 +74,7 @@ export interface NoteBodyEditorRef { execCommand(command: CommandValue): Promise; } -export interface MarkupToHtmlOptions { - replaceResourceInternalToExternalLinks?: boolean; - resourceInfos?: ResourceInfos; - contentMaxWidth?: number; - increaseControlsSize?: boolean; - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - plugins?: Record; - bodyOnly?: boolean; - mapsToLine?: boolean; - useCustomPdfViewer?: boolean; - noteId?: string; - vendorDir?: string; - platformName?: string; - allowedFilePrefixes?: string[]; - whiteBackgroundNoteRendering?: boolean; -} - +export { MarkupToHtmlOptions }; export type MarkupToHtmlHandler = (markupLanguage: MarkupLanguage, markup: string, options: MarkupToHtmlOptions)=> Promise; export type HtmlToMarkdownHandler = (markupLanguage: number, html: string, originalCss: string, parseOptions?: ParseOptions)=> Promise; diff --git a/packages/app-desktop/gui/NoteEditor/utils/useMarkupToHtml.ts b/packages/app-desktop/gui/hooks/useMarkupToHtml.ts similarity index 89% rename from packages/app-desktop/gui/NoteEditor/utils/useMarkupToHtml.ts rename to packages/app-desktop/gui/hooks/useMarkupToHtml.ts index 89fc0fc24fd..c876d69b504 100644 --- a/packages/app-desktop/gui/NoteEditor/utils/useMarkupToHtml.ts +++ b/packages/app-desktop/gui/hooks/useMarkupToHtml.ts @@ -6,8 +6,14 @@ import shim from '@joplin/lib/shim'; const { themeStyle } = require('@joplin/lib/theme'); import Note from '@joplin/lib/models/Note'; -import { MarkupToHtmlOptions, ResourceInfos } from './types'; +import { ResourceInfos } from '../NoteEditor/utils/types'; import { resourceFullPath } from '@joplin/lib/models/utils/resourceUtils'; +import { RenderOptions } from '@joplin/renderer/types'; + +export interface MarkupToHtmlOptions extends RenderOptions { + resourceInfos?: ResourceInfos; + replaceResourceInternalToExternalLinks?: boolean; +} interface HookDependencies { themeId: number; @@ -33,8 +39,7 @@ export default function useMarkupToHtml(deps: HookDependencies) { }); }, [plugins, customCss, resourceBaseUrl]); - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - return useCallback(async (markupLanguage: number, md: string, options: MarkupToHtmlOptions = null): Promise => { + return useCallback(async (markupLanguage: number, md: string, options: MarkupToHtmlOptions|null = null) => { options = { replaceResourceInternalToExternalLinks: false, resourceInfos: {}, diff --git a/packages/lib/services/interop/InteropService_Exporter_Html.ts b/packages/lib/services/interop/InteropService_Exporter_Html.ts index 2a2e424ef27..c7c3d680348 100644 --- a/packages/lib/services/interop/InteropService_Exporter_Html.ts +++ b/packages/lib/services/interop/InteropService_Exporter_Html.ts @@ -6,7 +6,7 @@ import Folder from '../../models/Folder'; import Note from '../../models/Note'; import Setting from '../../models/Setting'; import { MarkupToHtml } from '@joplin/renderer'; -import { NoteEntity, ResourceEntity } from '../database/types'; +import { NoteEntity, ResourceEntity, ResourceLocalStateEntity } from '../database/types'; import { contentScriptsToRendererRules } from '../plugins/utils/loadContentScripts'; import { basename, friendlySafeFilename, rtrimSlashes, dirname } from '../../path-utils'; import htmlpack from '@joplin/htmlpack'; @@ -17,6 +17,8 @@ import getPluginSettingValue from '../plugins/utils/getPluginSettingValue'; import { LinkRenderingType } from '@joplin/renderer/MdToHtml'; import Logger from '@joplin/utils/Logger'; import { parseRenderedNoteMetadata } from './utils'; +import ResourceLocalState from '../../models/ResourceLocalState'; +import { ResourceInfos } from '@joplin/renderer/types'; const logger = Logger.create('InteropService_Exporter_Html'); @@ -28,7 +30,7 @@ export default class InteropService_Exporter_Html extends InteropService_Exporte private createdDirs_: string[] = []; private resourceDir_: string; private markupToHtml_: MarkupToHtml; - private resources_: ResourceEntity[] = []; + private resources_: ResourceInfos = {}; // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied private style_: any; private packIntoSingleFile_ = false; @@ -174,10 +176,14 @@ export default class InteropService_Exporter_Html extends InteropService_Exporte } // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - public async processResource(resource: any, filePath: string) { + public async processResource(resource: ResourceEntity, filePath: string) { const destResourcePath = `${this.resourceDir_}/${basename(filePath)}`; await shim.fsDriver().copy(filePath, destResourcePath); - this.resources_.push(resource); + const localState: ResourceLocalStateEntity = await ResourceLocalState.load(resource.id); + this.resources_[resource.id] = { + localState, + item: resource, + }; } public async close() { diff --git a/packages/renderer/MdToHtml.ts b/packages/renderer/MdToHtml.ts index 8f7d79b2b08..88255233609 100644 --- a/packages/renderer/MdToHtml.ts +++ b/packages/renderer/MdToHtml.ts @@ -4,7 +4,7 @@ import { fileExtension } from '@joplin/utils/path'; import setupLinkify from './MdToHtml/setupLinkify'; import validateLinks from './MdToHtml/validateLinks'; import { Options as NoteStyleOptions } from './noteStyle'; -import { FsDriver, ItemIdToUrlHandler, MarkupRenderer, OptionsResourceModel, RenderOptions, RenderResult, RenderResultPluginAsset } from './types'; +import { FsDriver, ItemIdToUrlHandler, MarkupRenderer, OptionsResourceModel, RenderOptions, RenderResult, RenderResultPluginAsset, ResourceInfos } from './types'; import hljs from './highlight'; import * as MarkdownIt from 'markdown-it'; @@ -161,8 +161,7 @@ export interface RuleOptions { postMessageSyntax: string; ResourceModel: OptionsResourceModel; resourceBaseUrl: string; - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - resources: any; // resourceId: Resource + resources: ResourceInfos; // resourceId: Resource // Used by checkboxes to specify how it should be rendered checkboxRenderingType?: number; diff --git a/packages/renderer/MdToHtml/linkReplacement.test.ts b/packages/renderer/MdToHtml/linkReplacement.test.ts index feb6d394166..4c12505c452 100644 --- a/packages/renderer/MdToHtml/linkReplacement.test.ts +++ b/packages/renderer/MdToHtml/linkReplacement.test.ts @@ -28,7 +28,7 @@ describe('linkReplacement', () => { ResourceModel: defaultResourceModel, resources: { [resourceId]: { - item: {}, + item: { id: 'test' }, localState: { fetch_status: 2, // FETCH_STATUS_DONE }, @@ -46,7 +46,7 @@ describe('linkReplacement', () => { ResourceModel: defaultResourceModel, resources: { [resourceId]: { - item: {}, + item: { id: 'test' }, localState: { fetch_status: 0, // FETCH_STATUS_IDLE }, @@ -66,7 +66,7 @@ describe('linkReplacement', () => { ResourceModel: defaultResourceModel, resources: { [resourceId]: { - item: {}, + item: { id: 'test' }, localState: { fetch_status: 2, // FETCH_STATUS_DONE }, diff --git a/packages/renderer/MdToHtml/linkReplacement.ts b/packages/renderer/MdToHtml/linkReplacement.ts index ca48ee17ec8..e6dcc2ceb8f 100644 --- a/packages/renderer/MdToHtml/linkReplacement.ts +++ b/packages/renderer/MdToHtml/linkReplacement.ts @@ -1,5 +1,5 @@ import { LinkRenderingType } from '../MdToHtml'; -import { ItemIdToUrlHandler, OptionsResourceModel } from '../types'; +import { ItemIdToUrlHandler, OptionsResourceModel, ResourceInfos } from '../types'; import * as utils from '../utils'; import createEventHandlingAttrs from './createEventHandlingAttrs'; const Entities = require('html-entities').AllHtmlEntities; @@ -9,8 +9,7 @@ const { getClassNameForMimeType } = require('font-awesome-filetypes'); export interface Options { title?: string; - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - resources?: any; + resources?: ResourceInfos; ResourceModel?: OptionsResourceModel; linkRenderingType?: LinkRenderingType; plainResourceRendering?: boolean; diff --git a/packages/renderer/types.ts b/packages/renderer/types.ts index e2ce80899fe..772686603d7 100644 --- a/packages/renderer/types.ts +++ b/packages/renderer/types.ts @@ -4,17 +4,27 @@ import { Options as NoteStyleOptions } from './noteStyle'; export type ItemIdToUrlHandler = (resourceId: string, urlParameters?: string)=> string; interface ResourceEntity { - id: string; + id?: string; title?: string; mime?: string; file_extension?: string; + updated_time?: number; + + encryption_applied?: number; + encryption_blob_encrypted?: number; +} + +interface ResourceLocalState { + fetch_status?: number; } -interface ResourceInfo { - localState: unknown; +export interface ResourceInfo { + localState: ResourceLocalState; item: ResourceEntity; } +export type ResourceInfos = Record; + export interface FsDriver { writeFile: (path: string, content: string, encoding: string)=> Promise; exists: (path: string)=> Promise; @@ -54,13 +64,15 @@ export interface RenderOptions { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied settingValue?: (pluginId: string, key: string)=> any; - resources?: Record; + resources?: ResourceInfos; onResourceLoaded?: ()=> void; editPopupFiletypes?: string[]; createEditPopupSyntax?: string; destroyEditPopupSyntax?: string; + platformName?: string; + // HtmlToHtml only whiteBackgroundNoteRendering?: boolean; } diff --git a/packages/renderer/utils.ts b/packages/renderer/utils.ts index 7c839c8243e..5669fc274a2 100644 --- a/packages/renderer/utils.ts +++ b/packages/renderer/utils.ts @@ -1,5 +1,5 @@ import { attributesHtml } from './htmlUtils'; -import { ItemIdToUrlHandler, OptionsResourceModel } from './types'; +import { ItemIdToUrlHandler, OptionsResourceModel, ResourceInfo, ResourceInfos } from './types'; const Entities = require('html-entities').AllHtmlEntities; const htmlentities = new Entities().encode; @@ -98,8 +98,7 @@ export const resourceStatusName = function(index: number) { throw new Error(`Unknown index: ${index}`); }; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied -export const resourceStatus = function(ResourceModel: OptionsResourceModel, resourceInfo: any) { +export const resourceStatus = function(ResourceModel: OptionsResourceModel, resourceInfo: ResourceInfo) { if (!ResourceModel) return 'ready'; let status = 'ready'; @@ -130,8 +129,7 @@ type ImageMarkupData = { title: string; }|{ src: string; before: string; after: string }; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied -export const imageReplacement = function(ResourceModel: OptionsResourceModel, markup: ImageMarkupData, resources: any, resourceBaseUrl: string, itemIdToUrl: ItemIdToUrlHandler = null) { +export const imageReplacement = function(ResourceModel: OptionsResourceModel, markup: ImageMarkupData, resources: ResourceInfos, resourceBaseUrl: string, itemIdToUrl: ItemIdToUrlHandler = null) { if (!ResourceModel || !resources) return null; const src = markup.src;