-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-literate.ts
88 lines (79 loc) · 2.57 KB
/
be-literate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {BE, propDefaults, propInfo} from 'be-enhanced/BE.js';
import {BEConfig, EnhancementInfo} from 'be-enhanced/types';
import {XE} from 'xtal-element/XE.js';
import {Actions, AllProps, AP, PAP, ProPAP, POA} from './types';
import {register} from 'be-hive/register.js';
export class BeLiterate extends BE<AP, Actions, HTMLInputElement> implements Actions{
static override get beConfig(){
return {
parse: true,
} as BEConfig;
}
#abortController: AbortController | undefined;
override async attach(enhancedElement: HTMLInputElement, enhancementInfo: EnhancementInfo): Promise<void> {
await super.attach(enhancedElement, enhancementInfo);
this.#abortController = new AbortController();
enhancedElement.addEventListener('change', e => {
this.#readFile(this);
}, {
signal: this.#abortController.signal,
});
this.#readFile(this);
}
#readFile(self: this){
const {enhancedElement, readVerb} = self;
if(!enhancedElement.checkValidity()) return;
const {files} = enhancedElement;
if(files === null) return;
const fileContents: any[] = [];
let finishedCount = 0;
const fileReader = new FileReader();
fileReader.onload = (e: Event) => {
fileContents.push(fileReader.result);
finishedCount++;
if(finishedCount === files.length){
self.fileContents = fileContents;
}
}
fileReader.onerror = (e: Event) => {
console.error(e);
console.error(fileReader.error);
}
for(const file of files){
fileReader[readVerb](file);
}
self.resolved = true;
}
#disconnect(){
if(this.#abortController !== undefined) this.#abortController.abort();
}
override detach(detachedElement: HTMLInputElement) {
super.detach(detachedElement);
this.#disconnect();
}
}
export interface BeLiterate extends AllProps{}
const tagName = 'be-literate';
const ifWantsToBe = 'literate';
const upgrade = '*';
const xe = new XE<AP, Actions>({
config:{
tagName,
propDefaults:{
...propDefaults,
readVerb: 'readAsText',
},
propInfo:{
...propInfo,
fileContents:{
notify:{
dispatch: true,
dispatchFromEnhancedElement: true,
}
}
},
actions:{}
},
superclass: BeLiterate
});
register(ifWantsToBe, upgrade, tagName);