Skip to content

Commit c378c99

Browse files
committed
Migrating Project to Typescript & updating packages
1 parent 8cf79ee commit c378c99

18 files changed

Lines changed: 16729 additions & 678 deletions

dist/knockout-component-documentor.bundle.js

Lines changed: 14661 additions & 155 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/@types/html/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "*.html" {
2+
const value: DocumentFragment;
3+
export default value;
4+
}

js/entry.js renamed to js/entry.ts

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
1-
import CodeMirror from "codemirror/lib/codemirror.js";
2-
import "codemirror/lib/codemirror.css";
3-
import "codemirror/theme/mdn-like.css";
4-
5-
import Clipboard from "clipboard";
1+
import * as CodeMirror from 'codemirror';
2+
import 'codemirror/lib/codemirror.css';
3+
import 'codemirror/theme/mdn-like.css';
4+
import 'codemirror/mode/javascript/javascript.js';
5+
import 'codemirror/mode/xml/xml.js';
6+
import 'codemirror/mode/htmlmixed/htmlmixed.js';
7+
8+
import * as ClipboardJS from 'clipboard';
9+
10+
class knockoutType {
11+
constructor(baseType: string) {
12+
this.baseType = baseType;
13+
14+
this.observable = `${baseType} observable`;
15+
this.observableArray = `${baseType} observableArray`;
16+
this.computed = `${baseType} computed`;
17+
}
618

7-
import js from 'codemirror/mode/javascript/javascript.js';
8-
import xml from 'codemirror/mode/xml/xml.js';
9-
import htmlmixed from 'codemirror/mode/htmlmixed/htmlmixed.js';
19+
baseType: string;
20+
observable: string;
21+
observableArray: string;
22+
computed: string;
23+
}
1024

11-
function knockoutType(baseType) {
12-
this.baseType = baseType;
25+
class Generator {
26+
rand = () => {
27+
return Math.floor(Math.random() * 26) + Date.now();
28+
}
1329

14-
// knockout types
15-
this.observable = `${baseType} observable`;
16-
this.observableArray = `${baseType} observableArray`;
17-
this.computed = `${baseType} computed`;
30+
getId = () => {
31+
return 'uniqueID_' + (this.rand() + 1);
32+
}
33+
}
1834

19-
return this;
35+
declare global {
36+
interface Window {
37+
idGen: Generator;
38+
paramAsText: (property: any) => string;
39+
codeEditorFunction: (element: any, valueAccessor: any, allBindings: any, viewModel: any, bindingContext: any) => void;
40+
}
2041
}
2142

22-
window.paramAsText = function(property) {
43+
window.idGen = new Generator();
44+
45+
window.paramAsText = function(property: any) {
2346
if (property === undefined) {
2447
return "undefined";
2548
}
@@ -31,49 +54,45 @@ window.paramAsText = function(property) {
3154
return JSON.stringify(property);
3255
}
3356

34-
35-
function Generator() {};
36-
Generator.prototype.rand = Math.floor(Math.random() * 26) + Date.now();
37-
Generator.prototype.getId = function() { return 'uniqueID_' + this.rand++; };
38-
window.idGen = new Generator();
39-
40-
41-
42-
4357
// Create uniqueID and run function
4458
// pass a function that accepts the params (element, allBindings, viewModel, bindingContext)
4559
ko.bindingHandlers.uniqueIdFunction = {
46-
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
60+
init: (element, valueAccessor, allBindings, viewModel, bindingContext) => {
4761
// bind a unique ID
48-
let uniqueID = idGen.getId();
62+
let uniqueID = window.idGen.getId();
4963
element.setAttribute("id", uniqueID);
5064

5165
ko.unwrap(valueAccessor)().fn(element, valueAccessor, allBindings, viewModel, bindingContext);
5266
}
5367
};
5468

5569
ko.bindingHandlers.addUniqueID = {
56-
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
57-
let uniqueID = idGen.getId();
70+
init: (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) => {
71+
let uniqueID = window.idGen.getId();
5872
element.setAttribute("id", uniqueID);
5973
valueAccessor()(uniqueID);
6074
}
6175
};
6276

6377
ko.bindingHandlers.clipboard = {
64-
init: function(el, valueAccessor, allBindings, data, context) {
65-
new Clipboard(el, {
66-
text: function(trigger) {
67-
return ko.unwrap(valueAccessor());
68-
}
69-
}).on('success', function(e) {
70-
$(e.trigger).addClass("btn-success").find("span")
71-
.removeClass("glyphicon-copy")
72-
.addClass("glyphicon-ok");
73-
setTimeout(function(){
74-
$(e.trigger).removeClass("btn-success").find("span")
75-
.addClass("glyphicon-copy")
76-
.removeClass("glyphicon-ok");
78+
init: (el, valueAccessor, allBindings, data, context) => {
79+
new ClipboardJS(el, {
80+
text: (trigger) => ko.unwrap(valueAccessor())
81+
}).on('success', (e) => {
82+
const trigger = e.trigger;
83+
trigger.classList.add("btn-success");
84+
85+
// show copied icon
86+
const copyIcon = trigger.querySelector("span");
87+
copyIcon.classList.remove("glyphicon-copy");
88+
copyIcon.classList.add("glyphicon-ok");
89+
90+
// revert copy button
91+
setTimeout(() => {
92+
trigger.classList.remove("btn-success");
93+
94+
copyIcon.classList.add("glyphicon-copy");
95+
copyIcon.classList.remove("glyphicon-ok");
7796
}, 750);
7897
});
7998
}
@@ -99,7 +118,7 @@ ko.bindingHandlers.innerHtml = {
99118
// re-add
100119
element.innerHTML = `<div data-bind='component: { name: componentName, params: componentParamObject }'>${valueUnwrapped.value()}</div>`;
101120
// apply the binding again
102-
setTimeout(function(){
121+
setTimeout(() => {
103122
try {
104123
ko.applyBindings(viewModel, element.firstChild);
105124
}
@@ -132,7 +151,6 @@ window.codeEditorFunction = function(element, valueAccessor, allBindings, viewMo
132151
readOnly: bindingParams.readOnly,
133152
lineWrapping: true,
134153
indentWithTabs: true,
135-
matchBrackets: true,
136154
theme: "mdn-like"
137155
});
138156

@@ -145,7 +163,7 @@ window.codeEditorFunction = function(element, valueAccessor, allBindings, viewMo
145163
};
146164

147165

148-
import "./knockout-documentation-search.js";
149-
import "./knockout-component-documentor.js";
150-
import "./random-sample-component.js";
151-
import "./jsdoc-sample-component.js";
166+
import "./knockout-documentation-search.ts";
167+
import "./knockout-component-documentor.ts";
168+
import "./random-sample-component.ts";
169+
import "./jsdoc-sample-component.ts";

js/jsdoc-sample-component.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

js/jsdoc-sample-component.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import './jsdoc-sample-component.scss';
2+
import jsdocSampleComponent from './jsdoc-sample-component.html';
3+
4+
class JsDocSampleComponentVM {
5+
constructor(params: {
6+
observableString: KnockoutObservable<string>,
7+
defaultString: KnockoutObservable<string>,
8+
numParam: number,
9+
observableNumber: KnockoutObservableArray<number>,
10+
}) {
11+
this.observableString = params.observableString;
12+
this.defaultString = params.defaultString || ko.observable("default value");
13+
this.numParam = params.numParam;
14+
this.observableNumber = params.observableNumber;
15+
}
16+
17+
observableString: KnockoutObservable<string>;
18+
defaultString: KnockoutObservable<string>;
19+
numParam: number;
20+
observableNumber: KnockoutObservableArray<number>;
21+
}
22+
23+
ko.components.register('jsdoc-sample-component', {
24+
/**
25+
* @component jsdoc-sample-component
26+
* @tags ["demo", "example", "tag", "test"]
27+
* @description A quite wonderful component.
28+
* @category JSDoc Folder
29+
* @param {ko.observable(string)} params.obsString - A observable that is a string type
30+
* @param {string} [params.defaultString=default value] - This param has a default value of "default value"
31+
* @param {number} params.numParam - A param number type
32+
* @param {ko.observable(number)} params.obsNumber - A observable that is a number type
33+
*/
34+
viewModel: function(params: any) {
35+
return new JsDocSampleComponentVM(params);
36+
},
37+
template: jsdocSampleComponent
38+
});

0 commit comments

Comments
 (0)