Skip to content

Commit

Permalink
move the stabler component to it's own file
Browse files Browse the repository at this point in the history
  • Loading branch information
knzai committed Aug 10, 2024
1 parent 5bf9d9e commit 4c260e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
24 changes: 24 additions & 0 deletions src/webc/file-byte-reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class FileByteReader extends HTMLInputElement {
constructor() {
super();
}
connectedCallback() {
this.addEventListener('change', this.onChange);
}

emit (type, detail = {}) {
let event = new CustomEvent(`file-byte-reader:${type}`, {
bubbles: false,
cancelable: false,
detail: detail
});
return this.dispatchEvent(event);
}

onChange() {
const fileReader = new FileReader();
fileReader.addEventListener('loadend', e => this.emit('loaded', new Int8Array(fileReader.result)));
fileReader.readAsArrayBuffer(this.files[0]);
}
}
customElements.define("file-byte-reader", FileByteReader, { extends: 'input'});
35 changes: 5 additions & 30 deletions src/webc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,25 @@
data-cargo-no-default-features
data-cargo-features="webc"
/>
<script data-trunk src="./file-byte-reader.js"></script>
<script>
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
let $ = (query) => document.querySelector(query);

function fileLoaded(array) {
const container = document.createElement("preview-area");
let images = window.wasmBindings.previews(array);
images.forEach(function(src) {
container.appendChild(new PreviewItem(src));
});
$('preview-wrapper').appendChild(container)
$('preview-wrapper').appendChild(container);
}

document.addEventListener("DOMContentLoaded", () => {
$('#file-input').addEventListener("file-reader-input:loaded",
$('#file-input').addEventListener("file-byte-reader:loaded",
e => fileLoaded(e.detail)
);
});

class FileReaderInput extends HTMLInputElement {
constructor() {
super();
}
connectedCallback() {
this.addEventListener('change', this.onChange);
}

emit (type, detail = {}) {
let event = new CustomEvent(`file-reader-input:${type}`, {
bubbles: false,
cancelable: false,
detail: detail
});
return this.dispatchEvent(event);
}

onChange() {
const fileReader = new FileReader();
fileReader.addEventListener('loadend', e => this.emit('loaded', new Int8Array(fileReader.result)));
fileReader.readAsArrayBuffer(this.files[0]);
}
}
customElements.define("file-reader-input", FileReaderInput, { extends: 'input'});

class PreviewItem extends HTMLElement {
constructor(src) {
super();
Expand All @@ -73,7 +48,7 @@
</head>
<body>
<h1>Process your CGA/EGAs</h1>
<input is="file-reader-input" id="file-input" multiple="false" type="file" accept=".bin,.cga,.ega,.cega" />
<input is="file-byte-reader" id="file-input" multiple="false" type="file" accept=".bin,.cga,.ega,.cega" />
<preview-wrapper></preview-wrapper>
</body>
</html>

0 comments on commit 4c260e8

Please sign in to comment.