2024-03-02 17:29:18 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
import time from '../../time';
|
2024-06-20 15:01:13 +02:00
|
|
|
import { formatMsToRelative } from '@joplin/utils/time';
|
2024-03-02 17:29:18 +02:00
|
|
|
import { TagEntity } from '../database/types';
|
|
|
|
import { ListRendererDependency, RenderNoteView } from '../plugins/api/noteListType';
|
|
|
|
|
|
|
|
const logger = Logger.create('renderViewProps');
|
|
|
|
|
|
|
|
export interface RenderViewPropsOptions {
|
|
|
|
// Note that we don't render the title here, because it requires the mark.js package which is
|
|
|
|
// only available on the `app-desktop` package. So the caller needs to pre-render the title and
|
|
|
|
// pass it as an option.
|
|
|
|
noteTitleHtml: string;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-04-05 11:32:52 +02:00
|
|
|
const renderViewProp = (name: ListRendererDependency, value: any, options: RenderViewPropsOptions): string => {
|
2024-03-02 17:29:18 +02:00
|
|
|
const renderers: Partial<Record<ListRendererDependency, ()=> string>> = {
|
2024-06-20 15:01:13 +02:00
|
|
|
'note.user_updated_time': () => formatMsToRelative(value),
|
|
|
|
'note.user_created_time': () => formatMsToRelative(value),
|
|
|
|
'note.updated_time': () => formatMsToRelative(value),
|
|
|
|
'note.created_time': () => formatMsToRelative(value),
|
2024-04-04 12:38:38 +02:00
|
|
|
'note.todo_completed': () => value ? time.formatMsToLocal(value) : '',
|
2024-04-05 11:32:52 +02:00
|
|
|
'note.todo_due': () => value ? time.formatMsToLocal(value) : '',
|
2024-03-02 17:29:18 +02:00
|
|
|
'note.tags': () => value ? value.map((t: TagEntity) => t.title).join(', ') : '',
|
|
|
|
'note.title': () => options.noteTitleHtml,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const renderer = renderers[name];
|
|
|
|
if (renderer) return renderer();
|
|
|
|
} catch (error) {
|
|
|
|
// If the input value doesn't have the expected format, it may have been changed by the
|
|
|
|
// user. In that case we return the value without rendering it.
|
|
|
|
logger.warn('Could not render property:', name, 'With value:', value, 'Error:', error);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderViewProps = async (view: RenderNoteView, parentPath: string[], options: RenderViewPropsOptions) => {
|
|
|
|
for (const [name, value] of Object.entries(view)) {
|
|
|
|
const currentPath = parentPath.concat([name]);
|
|
|
|
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
|
|
await renderViewProps(value, currentPath, options);
|
|
|
|
} else {
|
|
|
|
view[name] = renderViewProp(currentPath.join('.') as ListRendererDependency, value, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default renderViewProps;
|