From fe8bcfd3ee0d844c935de26d5e1af233dcb98a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20S=C3=A1nchez?= Date: Sun, 28 Aug 2016 20:51:25 -0600 Subject: [PATCH] Added typing definitions for jsoneditor project --- jsoneditor/jsoneditor-tests.ts | 50 ++++++++++++++++++++++++++++++++ jsoneditor/jsoneditor.d.ts | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 jsoneditor/jsoneditor-tests.ts create mode 100644 jsoneditor/jsoneditor.d.ts diff --git a/jsoneditor/jsoneditor-tests.ts b/jsoneditor/jsoneditor-tests.ts new file mode 100644 index 0000000000..99bbb51b92 --- /dev/null +++ b/jsoneditor/jsoneditor-tests.ts @@ -0,0 +1,50 @@ +/// + +import JSONEditor, {JSONEditorMode, JSONEditorNode, JSONEditorOptions} from 'jsoneditor'; + +let options: JSONEditorOptions; +options = { + ace: ace, + //ajv: Ajv({allErrors: true, verbose: true}) + onChange() {}, + onEditable(node: JSONEditorNode) { + return true; + }, + onError(error: Error) {}, + onModeChange(newMode: JSONEditorMode, oldMode: JSONEditorMode) {}, + escapeUnicode: false, + sortObjectKeys: true, + history: true, + mode: 'tree', + modes: ['tree', 'view', 'form', 'code', 'text'], + name: 'foo', + schema: {}, + search: false, + indentation: 2, + theme: 'default' +}; +options = { + onEditable(node: JSONEditorNode) { + return {field: true, value: false}; + } +}; + +let jsonEditor: JSONEditor; +jsonEditor = new JSONEditor(document.body); +jsonEditor = new JSONEditor(document.body, {}); +jsonEditor = new JSONEditor(document.body, options, {foo: 'bar'}); + +jsonEditor.collapseAll(); +jsonEditor.destroy(); +jsonEditor.expandAll(); +jsonEditor.focus(); +jsonEditor.set({foo: 'bar'}); +jsonEditor.setMode('text'); +jsonEditor.setName('foo'); +jsonEditor.setName(); +jsonEditor.setSchema({}); +jsonEditor.setText('{foo: 1}'); + +const json: any = jsonEditor.get(); +const name: string = jsonEditor.getName(); +const jsonString: string = jsonEditor.getText(); diff --git a/jsoneditor/jsoneditor.d.ts b/jsoneditor/jsoneditor.d.ts new file mode 100644 index 0000000000..ea68a751a4 --- /dev/null +++ b/jsoneditor/jsoneditor.d.ts @@ -0,0 +1,52 @@ +// Type definitions for jsoneditor v5.5.7 +// Project: https://github.com/josdejong/jsoneditor +// Definitions by: Alejandro Sánchez +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module 'jsoneditor' { + export interface JSONEditorNode { + field: string; + value: string; + path: Array; + } + + export type JSONEditorMode = 'tree' | 'view' | 'form' | 'code' | 'text'; + + export interface JSONEditorOptions { + ace?: AceAjax.Ace; + ajv?: any; // Any for now, since ajv typings aren't A-Ok + onChange?: () => void; + onEditable?: (node: JSONEditorNode) => boolean | {field: boolean, value: boolean}; + onError?: (error: Error) => void; + onModeChange?: (newMode: JSONEditorMode, oldMode: JSONEditorMode) => void; + escapeUnicode?: boolean; + sortObjectKeys?: boolean; + history?: boolean; + mode?: JSONEditorMode; + modes?: Array; + name?: string; + schema?: Object; + search?: boolean; + indentation?: number; + theme?: string; + } + + export default class JSONEditor { + constructor(container: HTMLElement, options?: JSONEditorOptions, json?: Object); + collapseAll(): void; + destroy(): void; + expandAll(): void; + focus(): void; + set(json: Object): void; + setMode(mode: JSONEditorMode): void; + setName(name?: string): void; + setSchema(schema: Object): void; + setText(jsonString: string): void; + get(): any; + getMode(): JSONEditorMode; + getName(): string; + getText(): string; + } +}