diff --git a/.travis.yml b/.travis.yml index 3bca039..fbf5b9e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,9 @@ node_js: - '6' # EOL: April 2019 before_script: - npm install -g gulp -script: gulp +script: +- gulp +- npm run test:types deploy: - provider: npm email: info@saturnserver.org diff --git a/package-lock.json b/package-lock.json index 7558d81..5abacbc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,6 +24,30 @@ "js-tokens": "^4.0.0" } }, + "@types/codemirror": { + "version": "0.0.71", + "resolved": "https://registry.npmjs.org/@types/codemirror/-/codemirror-0.0.71.tgz", + "integrity": "sha512-b2oEEnno1LIGKMR7uBEsr40al1UijF1HEpRn0+Yf1xOLl24iQgB7DBpZVMM7y54G5wCNoclDrRO65E6KHPNO2w==", + "dev": true, + "requires": { + "@types/tern": "*" + } + }, + "@types/estree": { + "version": "0.0.39", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz", + "integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==", + "dev": true + }, + "@types/tern": { + "version": "0.22.1", + "resolved": "https://registry.npmjs.org/@types/tern/-/tern-0.22.1.tgz", + "integrity": "sha512-CRzPRkg8hYLwunsj61r+rqPJQbiCIEQqlMMY/0k7krgIsoSaFgGg1ZH2f9qaR1YpenaMl6PnlTtUkCbNH/uo+A==", + "dev": true, + "requires": { + "@types/estree": "*" + } + }, "JSONStream": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.5.tgz", @@ -5198,6 +5222,12 @@ "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", "dev": true }, + "typescript": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.2.2.tgz", + "integrity": "sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==", + "dev": true + }, "typo-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/typo-js/-/typo-js-1.0.3.tgz", diff --git a/package.json b/package.json index ae333d9..da5ea7d 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "javascript", "fontawesome" ], - "main": "./src/js/easymde.js", + "main": "src/js/easymde.js", + "types": "types/easymde.d.ts", "license": "MIT", "author": "Jeroen Akkerman", "bugs": { @@ -21,6 +22,7 @@ "marked": "^0.5.1" }, "devDependencies": { + "@types/codemirror": "0.0.71", "browserify": "^16.2.3", "gulp": "^4.0.0", "gulp-clean-css": "^3.10.0", @@ -29,6 +31,7 @@ "gulp-header": "^2.0.5", "gulp-rename": "^1.4.0", "gulp-uglify": "^3.0.1", + "typescript": "^3.2.2", "vinyl-buffer": "^1.0.0", "vinyl-source-stream": "^2.0.0" }, @@ -37,6 +40,7 @@ "url": "https://github.com/ionaru/easy-markdown-editor" }, "scripts": { - "prepare": "gulp" + "prepare": "gulp", + "test:types": "tsc --project types/tsconfig.json" } } diff --git a/types/easymde-test.ts b/types/easymde-test.ts new file mode 100644 index 0000000..e448e4c --- /dev/null +++ b/types/easymde-test.ts @@ -0,0 +1,26 @@ +// Create new instance +const editor = new EasyMDE({ + autoDownloadFontAwesome: false, + element: document.getElementById("mdEditor")!, + hideIcons: ["side-by-side", "fullscreen"], + shortcuts: { + drawTable: "Cmd-Alt-T", + toggleFullScreen: null + }, + spellChecker: false, + onToggleFullScreen: (full: boolean) => { console.log('FullscreenToggled', full); }, + theme: 'someOtherTheme', +}); + +// Editor functions +const value = editor.value() as string; +editor.value(value.toUpperCase()); + +const sbs = editor.isSideBySideActive() as boolean; +const fullscreen = editor.isFullscreenActive() as boolean; + +// Access to codemirror object +editor.codemirror.setOption('readOnly', true); + +// Static properties +EasyMDE.toggleItalic = (editor: EasyMDE) => { console.log('SomeButtonOverride'); }; diff --git a/types/easymde.d.ts b/types/easymde.d.ts new file mode 100644 index 0000000..6eadffe --- /dev/null +++ b/types/easymde.d.ts @@ -0,0 +1,152 @@ +// This file is based on https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/simplemde/index.d.ts, +// which is written by Scalesoft and licensed under the MIT license: +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +/// + +declare namespace EasyMDE { + interface AutoSaveOptions { + enabled?: boolean; + delay?: number; + uniqueId: string; + } + + interface BlockStyleOptions { + bold?: string; + code?: string; + italic?: string; + } + + interface InsertTextOptions { + horizontalRule?: ReadonlyArray; + image?: ReadonlyArray; + link?: ReadonlyArray; + table?: ReadonlyArray; + } + + interface ParsingOptions { + allowAtxHeaderWithoutSpace?: boolean; + strikethrough?: boolean; + underscoresBreakWords?: boolean; + } + + interface RenderingOptions { + singleLineBreaks?: boolean; + codeSyntaxHighlighting: boolean; + } + + interface Shortcuts { + [action: string]: string | undefined | null; + toggleBlockquote?: string | null; + toggleBold?: string | null; + cleanBlock?: string | null; + toggleHeadingSmaller?: string | null; + toggleItalic?: string | null; + drawLink?: string | null; + toggleUnorderedList?: string | null; + togglePreview?: string | null; + toggleCodeBlock?: string | null; + drawImage?: string | null; + toggleOrderedList?: string | null; + toggleHeadingBigger?: string | null; + toggleSideBySide?: string | null; + toggleFullScreen?: string | null; + } + + interface StatusBarItem { + className: string; + defaultValue: (element: HTMLElement) => void; + onUpdate: (element: HTMLElement) => void; + } + + interface ToolbarIcon { + name: string; + action: string|((editor: EasyMDE) => void); + className: string; + title: string; + } + + interface Options { + autoDownloadFontAwesome?: boolean; + autofocus?: boolean; + autosave?: AutoSaveOptions; + blockStyles?: BlockStyleOptions; + element?: HTMLElement; + forceSync?: boolean; + hideIcons?: ReadonlyArray; + indentWithTabs?: boolean; + initialValue?: string; + insertTexts?: InsertTextOptions; + lineWrapping?: boolean; + parsingConfig?: ParsingOptions; + placeholder?: string; + previewRender?: (markdownPlaintext: string, previewElement: HTMLElement) => string; + promptURLs?: boolean; + renderingConfig?: RenderingOptions; + shortcuts?: Shortcuts; + showIcons?: ReadonlyArray; + spellChecker?: boolean; + status?: boolean|ReadonlyArray; + styleSelectedText?: boolean; + tabSize?: number; + toolbar?: boolean|ReadonlyArray; + toolbarTips?: boolean; + onToggleFullScreen?: (goingIntoFullScreen: boolean) => void; + theme?: string; + } +} + +declare class EasyMDE { + constructor(options?: EasyMDE.Options); + value(): string; + value(val: string): void; + codemirror: CodeMirror.Editor; + toTextArea(): void; + isPreviewActive(): boolean; + isSideBySideActive(): boolean; + isFullscreenActive(): boolean; + clearAutosavedValue(): void; + + static toggleBold: (editor: EasyMDE) => void; + static toggleItalic: (editor: EasyMDE) => void; + static toggleStrikethrough: (editor: EasyMDE) => void; + static toggleHeadingSmaller: (editor: EasyMDE) => void; + static toggleHeadingBigger: (editor: EasyMDE) => void; + static toggleHeading1: (editor: EasyMDE) => void; + static toggleHeading2: (editor: EasyMDE) => void; + static toggleHeading3: (editor: EasyMDE) => void; + static toggleCodeBlock: (editor: EasyMDE) => void; + static toggleBlockquote: (editor: EasyMDE) => void; + static toggleUnorderedList: (editor: EasyMDE) => void; + static toggleOrderedList: (editor: EasyMDE) => void; + static cleanBlock: (editor: EasyMDE) => void; + static drawLink: (editor: EasyMDE) => void; + static drawImage: (editor: EasyMDE) => void; + static drawTable: (editor: EasyMDE) => void; + static drawHorizontalRule: (editor: EasyMDE) => void; + static togglePreview: (editor: EasyMDE) => void; + static toggleSideBySide: (editor: EasyMDE) => void; + static toggleFullScreen: (editor: EasyMDE) => void; + static undo: (editor: EasyMDE) => void; + static redo: (editor: EasyMDE) => void; +} + +export as namespace EasyMDE; +export = EasyMDE; diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..8449997 --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "target": "es3", + "strict": true, + "noImplicitReturns": true, + "noEmit": true + } +}