JS: Converted a few extra services to TS

This commit is contained in:
Dan Brown 2024-10-04 14:36:20 +01:00
parent 2766c76491
commit 346b88ae43
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
12 changed files with 39 additions and 71 deletions

View File

@ -1,6 +1,6 @@
import {EditorView, keymap} from '@codemirror/view'; import {EditorView, keymap} from '@codemirror/view';
import {copyTextToClipboard} from '../services/clipboard'; import {copyTextToClipboard} from '../services/clipboard.ts';
import {viewerExtensions, editorExtensions} from './setups'; import {viewerExtensions, editorExtensions} from './setups';
import {createView} from './views'; import {createView} from './views';
import {SimpleEditorInterface} from './simple-editor-interface'; import {SimpleEditorInterface} from './simple-editor-interface';

View File

@ -1,5 +1,5 @@
import {Component} from './component'; import {Component} from './component';
import {Clipboard} from '../services/clipboard'; import {Clipboard} from '../services/clipboard.ts';
import { import {
elem, getLoading, onSelect, removeLoading, elem, getLoading, onSelect, removeLoading,
} from '../services/dom'; } from '../services/dom';

View File

@ -1,7 +1,7 @@
import * as Dates from '../services/dates';
import {onSelect} from '../services/dom'; import {onSelect} from '../services/dom';
import {debounce} from '../services/util'; import {debounce} from '../services/util';
import {Component} from './component'; import {Component} from './component';
import {utcTimeStampToLocalTime} from '../services/dates.ts';
export class PageEditor extends Component { export class PageEditor extends Component {
@ -129,7 +129,7 @@ export class PageEditor extends Component {
this.deleteDraftWrap.toggleAttribute('hidden', false); this.deleteDraftWrap.toggleAttribute('hidden', false);
} }
this.draftNotifyChange(`${resp.data.message} ${Dates.utcTimeStampToLocalTime(resp.data.timestamp)}`); this.draftNotifyChange(`${resp.data.message} ${utcTimeStampToLocalTime(resp.data.timestamp)}`);
this.autoSave.last = Date.now(); this.autoSave.last = Date.now();
if (resp.data.warning && !this.shownWarningsCache.has(resp.data.warning)) { if (resp.data.warning && !this.shownWarningsCache.has(resp.data.warning)) {
window.$events.emit('warning', resp.data.warning); window.$events.emit('warning', resp.data.warning);

View File

@ -1,6 +1,6 @@
import * as DOM from '../services/dom'; import * as DOM from '../services/dom';
import {Component} from './component'; import {Component} from './component';
import {copyTextToClipboard} from '../services/clipboard'; import {copyTextToClipboard} from '../services/clipboard.ts';
export class Pointer extends Component { export class Pointer extends Component {

View File

@ -1,6 +1,6 @@
import {provideKeyBindings} from './shortcuts'; import {provideKeyBindings} from './shortcuts';
import {debounce} from '../services/util'; import {debounce} from '../services/util';
import {Clipboard} from '../services/clipboard'; import {Clipboard} from '../services/clipboard.ts';
/** /**
* Initiate the codemirror instance for the markdown editor. * Initiate the codemirror instance for the markdown editor.

View File

@ -1,4 +1,4 @@
import {patchDomFromHtmlString} from '../services/vdom'; import {patchDomFromHtmlString} from '../services/vdom.ts';
export class Display { export class Display {

View File

@ -1,62 +1,43 @@
export class Clipboard { export class Clipboard {
/** protected data: DataTransfer;
* Constructor
* @param {DataTransfer} clipboardData constructor(clipboardData: DataTransfer) {
*/
constructor(clipboardData) {
this.data = clipboardData; this.data = clipboardData;
} }
/** /**
* Check if the clipboard has any items. * Check if the clipboard has any items.
*/ */
hasItems() { hasItems(): boolean {
return Boolean(this.data) && Boolean(this.data.types) && this.data.types.length > 0; return Boolean(this.data) && Boolean(this.data.types) && this.data.types.length > 0;
} }
/** /**
* Check if the given event has tabular-looking data in the clipboard. * Check if the given event has tabular-looking data in the clipboard.
* @return {boolean}
*/ */
containsTabularData() { containsTabularData(): boolean {
const rtfData = this.data.getData('text/rtf'); const rtfData = this.data.getData('text/rtf');
return rtfData && rtfData.includes('\\trowd'); return !!rtfData && rtfData.includes('\\trowd');
} }
/** /**
* Get the images that are in the clipboard data. * Get the images that are in the clipboard data.
* @return {Array<File>}
*/ */
getImages() { getImages(): File[] {
const {types} = this.data; return this.getFiles().filter(f => f.type.includes('image'));
const images = [];
for (const type of types) {
if (type.includes('image')) {
const item = this.data.getData(type);
images.push(item.getAsFile());
}
}
const imageFiles = this.getFiles().filter(f => f.type.includes('image'));
images.push(...imageFiles);
return images;
} }
/** /**
* Get the files included in the clipboard data. * Get the files included in the clipboard data.
* @return {File[]}
*/ */
getFiles() { getFiles(): File[] {
const {files} = this.data; const {files} = this.data;
return [...files]; return [...files];
} }
} }
export async function copyTextToClipboard(text) { export async function copyTextToClipboard(text: string) {
if (window.isSecureContext && navigator.clipboard) { if (window.isSecureContext && navigator.clipboard) {
await navigator.clipboard.writeText(text); await navigator.clipboard.writeText(text);
return; return;
@ -64,7 +45,7 @@ export async function copyTextToClipboard(text) {
// Backup option where we can't use the navigator.clipboard API // Backup option where we can't use the navigator.clipboard API
const tempInput = document.createElement('textarea'); const tempInput = document.createElement('textarea');
tempInput.style = 'position: absolute; left: -1000px; top: -1000px;'; tempInput.setAttribute('style', 'position: absolute; left: -1000px; top: -1000px;');
tempInput.value = text; tempInput.value = text;
document.body.appendChild(tempInput); document.body.appendChild(tempInput);
tempInput.select(); tempInput.select();

View File

@ -1,23 +0,0 @@
export function getCurrentDay() {
const date = new Date();
const month = date.getMonth() + 1;
const day = date.getDate();
return `${date.getFullYear()}-${(month > 9 ? '' : '0') + month}-${(day > 9 ? '' : '0') + day}`;
}
export function utcTimeStampToLocalTime(timestamp) {
const date = new Date(timestamp * 1000);
const hours = date.getHours();
const mins = date.getMinutes();
return `${(hours > 9 ? '' : '0') + hours}:${(mins > 9 ? '' : '0') + mins}`;
}
export function formatDateTime(date) {
const month = date.getMonth() + 1;
const day = date.getDate();
const hours = date.getHours();
const mins = date.getMinutes();
return `${date.getFullYear()}-${(month > 9 ? '' : '0') + month}-${(day > 9 ? '' : '0') + day} ${(hours > 9 ? '' : '0') + hours}:${(mins > 9 ? '' : '0') + mins}`;
}

View File

@ -0,0 +1,14 @@
export function getCurrentDay(): string {
const date = new Date();
const month = date.getMonth() + 1;
const day = date.getDate();
return `${date.getFullYear()}-${(month > 9 ? '' : '0') + month}-${(day > 9 ? '' : '0') + day}`;
}
export function utcTimeStampToLocalTime(timestamp: number): string {
const date = new Date(timestamp * 1000);
const hours = date.getHours();
const mins = date.getMinutes();
return `${(hours > 9 ? '' : '0') + hours}:${(mins > 9 ? '' : '0') + mins}`;
}

View File

@ -3,13 +3,13 @@ import {
attributesModule, attributesModule,
toVNode, toVNode,
} from 'snabbdom'; } from 'snabbdom';
import {VNode} from "snabbdom/build/vnode";
let patcher; type vDomPatcher = (oldVnode: VNode | Element | DocumentFragment, vnode: VNode) => VNode;
/** let patcher: vDomPatcher;
* @returns {Function}
*/ function getPatcher(): vDomPatcher {
function getPatcher() {
if (patcher) return patcher; if (patcher) return patcher;
patcher = init([ patcher = init([
@ -19,11 +19,7 @@ function getPatcher() {
return patcher; return patcher;
} }
/** export function patchDomFromHtmlString(domTarget: Element, html: string): void {
* @param {Element} domTarget
* @param {String} html
*/
export function patchDomFromHtmlString(domTarget, html) {
const contentDom = document.createElement('div'); const contentDom = document.createElement('div');
contentDom.innerHTML = html; contentDom.innerHTML = html;
getPatcher()(toVNode(domTarget), toVNode(contentDom)); getPatcher()(toVNode(domTarget), toVNode(contentDom));

View File

@ -1,4 +1,4 @@
import {Clipboard} from '../services/clipboard'; import {Clipboard} from '../services/clipboard.ts';
let wrap; let wrap;
let draggedContentEditable; let draggedContentEditable;