Skip to content

Commit d53c19b

Browse files
committed
Use classes for templates; keep but deprecate use of function template creators
1 parent cb99464 commit d53c19b

File tree

2 files changed

+88
-34
lines changed

2 files changed

+88
-34
lines changed

code-input.d.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,35 @@ export class Template {
345345
*/
346346
export namespace templates {
347347
/**
348-
* Constructor to create a template that uses Prism.js syntax highlighting (https://prismjs.com/)
349-
* @param {Object} prism Import Prism.js, then after that import pass the `Prism` object as this parameter.
350-
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
351-
* @returns template object
348+
* A template that uses Prism.js syntax highlighting (https://prismjs.com/).
349+
*/
350+
class Prism extends Template {
351+
/**
352+
* Constructor to create a template that uses Prism.js syntax highlighting (https://prismjs.com/)
353+
* @param {Object} prism Import Prism.js, then after that import pass the `Prism` object as this parameter.
354+
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
355+
* @returns template object
356+
*/
357+
constructor(prism: Object, plugins?: Plugin[])
358+
}
359+
/**
360+
* @deprecated Please use `new codeInput.templates.Prism(...)`
352361
*/
353362
function prism(prism: Object, plugins?: Plugin[]): Template
354363
/**
355-
* Constructor to create a template that uses highlight.js syntax highlighting (https://highlightjs.org/)
356-
* @param {Object} hljs Import highlight.js, then after that import pass the `hljs` object as this parameter.
357-
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
358-
* @returns template object
364+
* A template that uses highlight.js syntax highlighting (https://highlightjs.org/).
365+
*/
366+
class Hljs extends Template {
367+
/**
368+
* Constructor to create a template that uses highlight.js syntax highlighting (https://highlightjs.org/)
369+
* @param {Object} hljs Import highlight.js, then after that import pass the `hljs` object as this parameter.
370+
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
371+
* @returns template object
372+
*/
373+
constructor(hljs: Object, plugins?: Plugin[])
374+
}
375+
/**
376+
* @deprecated Please use `new codeInput.templates.Hljs(...)`
359377
*/
360378
function hljs(hljs: Object, plugins?: Plugin[]): Template
361379
/**

code-input.js

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -222,38 +222,19 @@ var codeInput = {
222222
* For adding small pieces of functionality, please see `codeInput.plugins`.
223223
*/
224224
templates: {
225+
// (Source code for class templates after var codeInput = ... so they can extend the codeInput.Template class)
225226
/**
226-
* Constructor to create a template that uses Prism.js syntax highlighting (https://prismjs.com/)
227-
* @param {Object} prism Import Prism.js, then after that import pass the `Prism` object as this parameter.
228-
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
229-
* @returns {codeInput.Template} template object
227+
* @deprecated Please use `new codeInput.templates.Prism(...)`
230228
*/
231229
prism(prism, plugins = []) { // Dependency: Prism.js (https://prismjs.com/)
232-
return new codeInput.Template(
233-
prism.highlightElement, // highlight
234-
true, // preElementStyled
235-
true, // isCode
236-
false, // includeCodeInputInHighlightFunc
237-
plugins
238-
);
230+
return new codeInput.templates.Prism(prism, plugins);
239231
},
232+
240233
/**
241-
* Constructor to create a template that uses highlight.js syntax highlighting (https://highlightjs.org/)
242-
* @param {Object} hljs Import highlight.js, then after that import pass the `hljs` object as this parameter.
243-
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
244-
* @returns {codeInput.Template} template object
234+
* @deprecated Please use `new codeInput.templates.Hljs(...)`
245235
*/
246236
hljs(hljs, plugins = []) { // Dependency: Highlight.js (https://highlightjs.org/)
247-
return new codeInput.Template(
248-
function(codeElement) {
249-
codeElement.removeAttribute("data-highlighted");
250-
hljs.highlightElement(codeElement);
251-
}, // highlight
252-
false, // preElementStyled
253-
true, // isCode
254-
false, // includeCodeInputInHighlightFunc
255-
plugins
256-
);
237+
return new codeInput.templates.Hljs(hljs, plugins);
257238
},
258239

259240
/**
@@ -318,7 +299,7 @@ var codeInput = {
318299
},
319300

320301
/**
321-
* @deprecated Please use `new codeInput.Template()`
302+
* @deprecated Please use `new codeInput.Template(...)`
322303
*/
323304
custom(highlight = function () { }, preElementStyled = true, isCode = true, includeCodeInputInHighlightFunc = false, plugins = []) {
324305
return {
@@ -1058,4 +1039,59 @@ var codeInput = {
10581039
}
10591040
}
10601041

1042+
{
1043+
// Templates are defined here after the codeInput variable is set, because they reference it by extending codeInput.Template.
1044+
1045+
// ESM-SUPPORT-START-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation!
1046+
/**
1047+
* A template that uses Prism.js syntax highlighting (https://prismjs.com/).
1048+
*/
1049+
class Prism extends codeInput.Template { // Dependency: Prism.js (https://prismjs.com/)
1050+
/**
1051+
* Constructor to create a template that uses Prism.js syntax highlighting (https://prismjs.com/)
1052+
* @param {Object} prism Import Prism.js, then after that import pass the `Prism` object as this parameter.
1053+
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
1054+
* @returns {codeInput.Template} template object
1055+
*/
1056+
constructor(prism, plugins = []) {
1057+
super(
1058+
prism.highlightElement, // highlight
1059+
true, // preElementStyled
1060+
true, // isCode
1061+
false, // includeCodeInputInHighlightFunc
1062+
plugins
1063+
);
1064+
}
1065+
};
1066+
// ESM-SUPPORT-END-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation!
1067+
codeInput.templates.Prism = Prism;
1068+
1069+
// ESM-SUPPORT-START-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
1070+
/**
1071+
* A template that uses highlight.js syntax highlighting (https://highlightjs.org/).
1072+
*/
1073+
class Hljs extends codeInput.Template { // Dependency: Highlight.js (https://highlightjs.org/)
1074+
/**
1075+
* Constructor to create a template that uses highlight.js syntax highlighting (https://highlightjs.org/)
1076+
* @param {Object} hljs Import highlight.js, then after that import pass the `hljs` object as this parameter.
1077+
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
1078+
* @returns {codeInput.Template} template object
1079+
*/
1080+
constructor(hljs, plugins = []) {
1081+
super(
1082+
function(codeElement) {
1083+
codeElement.removeAttribute("data-highlighted");
1084+
hljs.highlightElement(codeElement);
1085+
}, // highlight
1086+
false, // preElementStyled
1087+
true, // isCode
1088+
false, // includeCodeInputInHighlightFunc
1089+
plugins
1090+
);
1091+
}
1092+
};
1093+
// ESM-SUPPORT-END-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
1094+
codeInput.templates.Hljs = Hljs;
1095+
}
1096+
10611097
customElements.define("code-input", codeInput.CodeInput);

0 commit comments

Comments
 (0)