Skip to content

Commit 346b88a

Browse files
committed
JS: Converted a few extra services to TS
1 parent 2766c76 commit 346b88a

File tree

12 files changed

+39
-71
lines changed

12 files changed

+39
-71
lines changed

resources/js/code/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {EditorView, keymap} from '@codemirror/view';
22

3-
import {copyTextToClipboard} from '../services/clipboard';
3+
import {copyTextToClipboard} from '../services/clipboard.ts';
44
import {viewerExtensions, editorExtensions} from './setups';
55
import {createView} from './views';
66
import {SimpleEditorInterface} from './simple-editor-interface';

resources/js/components/dropzone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component} from './component';
2-
import {Clipboard} from '../services/clipboard';
2+
import {Clipboard} from '../services/clipboard.ts';
33
import {
44
elem, getLoading, onSelect, removeLoading,
55
} from '../services/dom';

resources/js/components/page-editor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as Dates from '../services/dates';
21
import {onSelect} from '../services/dom';
32
import {debounce} from '../services/util';
43
import {Component} from './component';
4+
import {utcTimeStampToLocalTime} from '../services/dates.ts';
55

66
export class PageEditor extends Component {
77

@@ -129,7 +129,7 @@ export class PageEditor extends Component {
129129
this.deleteDraftWrap.toggleAttribute('hidden', false);
130130
}
131131

132-
this.draftNotifyChange(`${resp.data.message} ${Dates.utcTimeStampToLocalTime(resp.data.timestamp)}`);
132+
this.draftNotifyChange(`${resp.data.message} ${utcTimeStampToLocalTime(resp.data.timestamp)}`);
133133
this.autoSave.last = Date.now();
134134
if (resp.data.warning && !this.shownWarningsCache.has(resp.data.warning)) {
135135
window.$events.emit('warning', resp.data.warning);

resources/js/components/pointer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as DOM from '../services/dom';
22
import {Component} from './component';
3-
import {copyTextToClipboard} from '../services/clipboard';
3+
import {copyTextToClipboard} from '../services/clipboard.ts';
44

55
export class Pointer extends Component {
66

resources/js/markdown/codemirror.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {provideKeyBindings} from './shortcuts';
22
import {debounce} from '../services/util';
3-
import {Clipboard} from '../services/clipboard';
3+
import {Clipboard} from '../services/clipboard.ts';
44

55
/**
66
* Initiate the codemirror instance for the markdown editor.

resources/js/markdown/display.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {patchDomFromHtmlString} from '../services/vdom';
1+
import {patchDomFromHtmlString} from '../services/vdom.ts';
22

33
export class Display {
44

resources/js/services/clipboard.js renamed to resources/js/services/clipboard.ts

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,51 @@
11
export class Clipboard {
22

3-
/**
4-
* Constructor
5-
* @param {DataTransfer} clipboardData
6-
*/
7-
constructor(clipboardData) {
3+
protected data: DataTransfer;
4+
5+
constructor(clipboardData: DataTransfer) {
86
this.data = clipboardData;
97
}
108

119
/**
1210
* Check if the clipboard has any items.
1311
*/
14-
hasItems() {
12+
hasItems(): boolean {
1513
return Boolean(this.data) && Boolean(this.data.types) && this.data.types.length > 0;
1614
}
1715

1816
/**
1917
* Check if the given event has tabular-looking data in the clipboard.
20-
* @return {boolean}
2118
*/
22-
containsTabularData() {
19+
containsTabularData(): boolean {
2320
const rtfData = this.data.getData('text/rtf');
24-
return rtfData && rtfData.includes('\\trowd');
21+
return !!rtfData && rtfData.includes('\\trowd');
2522
}
2623

2724
/**
2825
* Get the images that are in the clipboard data.
29-
* @return {Array<File>}
3026
*/
31-
getImages() {
32-
const {types} = this.data;
33-
const images = [];
34-
35-
for (const type of types) {
36-
if (type.includes('image')) {
37-
const item = this.data.getData(type);
38-
images.push(item.getAsFile());
39-
}
40-
}
41-
42-
const imageFiles = this.getFiles().filter(f => f.type.includes('image'));
43-
images.push(...imageFiles);
44-
45-
return images;
27+
getImages(): File[] {
28+
return this.getFiles().filter(f => f.type.includes('image'));
4629
}
4730

4831
/**
4932
* Get the files included in the clipboard data.
50-
* @return {File[]}
5133
*/
52-
getFiles() {
34+
getFiles(): File[] {
5335
const {files} = this.data;
5436
return [...files];
5537
}
56-
5738
}
5839

59-
export async function copyTextToClipboard(text) {
40+
export async function copyTextToClipboard(text: string) {
6041
if (window.isSecureContext && navigator.clipboard) {
6142
await navigator.clipboard.writeText(text);
6243
return;
6344
}
6445

6546
// Backup option where we can't use the navigator.clipboard API
6647
const tempInput = document.createElement('textarea');
67-
tempInput.style = 'position: absolute; left: -1000px; top: -1000px;';
48+
tempInput.setAttribute('style', 'position: absolute; left: -1000px; top: -1000px;');
6849
tempInput.value = text;
6950
document.body.appendChild(tempInput);
7051
tempInput.select();

resources/js/services/dates.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

resources/js/services/dates.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function getCurrentDay(): string {
2+
const date = new Date();
3+
const month = date.getMonth() + 1;
4+
const day = date.getDate();
5+
6+
return `${date.getFullYear()}-${(month > 9 ? '' : '0') + month}-${(day > 9 ? '' : '0') + day}`;
7+
}
8+
9+
export function utcTimeStampToLocalTime(timestamp: number): string {
10+
const date = new Date(timestamp * 1000);
11+
const hours = date.getHours();
12+
const mins = date.getMinutes();
13+
return `${(hours > 9 ? '' : '0') + hours}:${(mins > 9 ? '' : '0') + mins}`;
14+
}
File renamed without changes.

0 commit comments

Comments
 (0)