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 {copyTextToClipboard} from '../services/clipboard';
import {copyTextToClipboard} from '../services/clipboard.ts';
import {viewerExtensions, editorExtensions} from './setups';
import {createView} from './views';
import {SimpleEditorInterface} from './simple-editor-interface';

View File

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

View File

@ -1,7 +1,7 @@
import * as Dates from '../services/dates';
import {onSelect} from '../services/dom';
import {debounce} from '../services/util';
import {Component} from './component';
import {utcTimeStampToLocalTime} from '../services/dates.ts';
export class PageEditor extends Component {
@ -129,7 +129,7 @@ export class PageEditor extends Component {
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();
if (resp.data.warning && !this.shownWarningsCache.has(resp.data.warning)) {
window.$events.emit('warning', resp.data.warning);

View File

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

View File

@ -1,6 +1,6 @@
import {provideKeyBindings} from './shortcuts';
import {debounce} from '../services/util';
import {Clipboard} from '../services/clipboard';
import {Clipboard} from '../services/clipboard.ts';
/**
* 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 {

View File

@ -1,62 +1,43 @@
export class Clipboard {
/**
* Constructor
* @param {DataTransfer} clipboardData
*/
constructor(clipboardData) {
protected data: DataTransfer;
constructor(clipboardData: DataTransfer) {
this.data = clipboardData;
}
/**
* Check if the clipboard has any items.
*/
hasItems() {
hasItems(): boolean {
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.
* @return {boolean}
*/
containsTabularData() {
containsTabularData(): boolean {
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.
* @return {Array<File>}
*/
getImages() {
const {types} = this.data;
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;
getImages(): File[] {
return this.getFiles().filter(f => f.type.includes('image'));
}
/**
* Get the files included in the clipboard data.
* @return {File[]}
*/
getFiles() {
getFiles(): File[] {
const {files} = this.data;
return [...files];
}
}
export async function copyTextToClipboard(text) {
export async function copyTextToClipboard(text: string) {
if (window.isSecureContext && navigator.clipboard) {
await navigator.clipboard.writeText(text);
return;
@ -64,7 +45,7 @@ export async function copyTextToClipboard(text) {
// Backup option where we can't use the navigator.clipboard API
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;
document.body.appendChild(tempInput);
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,
toVNode,
} from 'snabbdom';
import {VNode} from "snabbdom/build/vnode";
let patcher;
type vDomPatcher = (oldVnode: VNode | Element | DocumentFragment, vnode: VNode) => VNode;
/**
* @returns {Function}
*/
function getPatcher() {
let patcher: vDomPatcher;
function getPatcher(): vDomPatcher {
if (patcher) return patcher;
patcher = init([
@ -19,11 +19,7 @@ function getPatcher() {
return patcher;
}
/**
* @param {Element} domTarget
* @param {String} html
*/
export function patchDomFromHtmlString(domTarget, html) {
export function patchDomFromHtmlString(domTarget: Element, html: string): void {
const contentDom = document.createElement('div');
contentDom.innerHTML = html;
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 draggedContentEditable;