Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
SupClient: Add SupClient.readFile for reading local files (from drag'…
Browse files Browse the repository at this point in the history
…n'drop / file input)
  • Loading branch information
elisee committed Feb 24, 2016
1 parent 371341b commit 87e7462
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion SupClient/SupClient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ declare namespace SupClient {
export const query: { project: string, asset: string; [key: string]: string; };
export const cookies: Cookies.CookiesStatic;

export function fetch(url: string, responseType: string, callback: (err: Error, data: any) => any): void;
export function fetch(url: string, responseType: string, callback: (err: Error, data: any) => void): void;
export function readFile(file: File, type: string, callback: (err: Error, data: any) => void): void;

export function registerPlugin<T>(contextName: string, pluginName: string, plugin: T): void;
export function getPlugins<T>(contextName: string): { [pluginName: string]: { path: string; content: T; } };
Expand Down
2 changes: 1 addition & 1 deletion SupClient/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function fetch(url: string, type: string, callback: (err: Error, data?: any) => any) {
export default function fetch(url: string, type: string, callback: (err: Error, data?: any) => void) {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = type;
Expand Down
3 changes: 2 additions & 1 deletion SupClient/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as cookies from "js-cookie";

/* tslint:disable:no-unused-variable */
import fetch from "./fetch";
import readFile from "./readFile";
import ProjectClient from "./ProjectClient";
import setupHotkeys, { setupHelpCallback } from "./setupHotkeys";
import * as table from "./table";
Expand All @@ -15,7 +16,7 @@ import * as i18n from "./i18n";
import * as ResizeHandle from "resize-handle";
import * as TreeView from "dnd-tree-view";

export { fetch, cookies, ProjectClient, setupHotkeys, setupHelpCallback, table, Dialogs, i18n };
export { fetch, readFile, cookies, ProjectClient, setupHotkeys, setupHelpCallback, table, Dialogs, i18n };

export const isApp = window.navigator.userAgent.indexOf("Electron") !== -1;
export const query = querystring.parse(window.location.search.slice(1));
Expand Down
30 changes: 30 additions & 0 deletions SupClient/src/readFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export default function readFile(file: File, type: string, callback: (err: Error, data?: any) => void) {
const reader = new FileReader;

reader.onload = (event) => {
let data: any;

if (type === "json") {
try { data = JSON.parse((event.target as FileReader).result); }
catch (err) { callback(err, null); return; }
} else{
data = (event.target as FileReader).result;
}

callback(null, data);
};

switch (type) {
case "text":
case "json":
reader.readAsText(file);
break;

case "arraybuffer":
reader.readAsArrayBuffer(file);
break;

default:
callback(new Error(`Unsupported readFile type: ${type}`));
}
}

0 comments on commit 87e7462

Please sign in to comment.