Skip to content

Commit a69b837

Browse files
authored
Merge branch 'main' into add-plugins
2 parents 314d4c0 + ab30c56 commit a69b837

File tree

5 files changed

+86
-9
lines changed

5 files changed

+86
-9
lines changed

.github/workflows/minify.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Auto-minify
2+
on:
3+
push:
4+
branches:
5+
- main
6+
jobs:
7+
Minify:
8+
runs-on: ubuntu-latest
9+
steps:
10+
# Checks-out your repository under $GITHUB_WORKSPACE, so auto-minify job can access it
11+
- uses: actions/checkout@v2
12+
13+
- name: Auto Minify
14+
uses: nizarmah/[email protected]
15+
16+
# Auto commits minified files to the repository
17+
# Ignore it if you don't want to commit the files to the repository
18+
- name: Auto committing minified files
19+
uses: stefanzweifel/git-auto-commit-action@v4
20+
with:
21+
commit_message: "Auto Minified JS and CSS files"
22+
branch: ${{ github.ref }}

code-input.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,11 @@ code-input textarea, code-input pre {
8989
code-input textarea {
9090
resize: none;
9191
outline: none!important;
92+
}
93+
94+
code-input:not(.code-input_registered)::before {
95+
/* Display message to register */
96+
content: "Use codeInput.registerTemplate to set up.";
97+
display: block;
98+
color: grey;
9299
}

code-input.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var codeInput = {
66
usedTemplates: {
77
},
88
defaultTemplate: undefined,
9+
templateQueue: {}, // lists of elements for each unrecognised template
910
plugins: { // Import a plugin from the plugins folder and it will be saved here.
1011
},
1112
Plugin: class {
@@ -78,10 +79,32 @@ var codeInput = {
7879
return text.replace(new RegExp("&", "g"), "&amp;").replace(new RegExp("<", "g"), "&lt;"); /* Global RegExp */
7980
}
8081

81-
/* Callbacks */
82-
connectedCallback() {
83-
// Added to document
84-
this.template = codeInput.usedTemplates[this.getAttribute("template") || codeInput.defaultTemplate];
82+
/* Get the template for this element or add to the unrecognised template queue. */
83+
get_template() {
84+
// Get name of template
85+
let template_name;
86+
if(this.getAttribute("template") == undefined) {
87+
// Default
88+
template_name = codeInput.defaultTemplate;
89+
} else {
90+
template_name = this.getAttribute("template");
91+
}
92+
// Get template
93+
if(template_name in codeInput.usedTemplates) {
94+
return codeInput.usedTemplates[template_name];
95+
} else {
96+
// Doesn't exist - add to queue
97+
if( !(template_name in codeInput.templateQueue)) {
98+
codeInput.templateQueue[template_name] = [];
99+
}
100+
codeInput.templateQueue[template_name].push(this);
101+
return undefined;
102+
}
103+
codeInput.usedTemplates[codeInput.defaultTemplate]
104+
}
105+
/* Set up element when a template is added */
106+
setup() {
107+
this.classList.add("code-input_registered"); // Remove register message
85108
if(this.template.preElementStyled) this.classList.add("code-input_pre-element-styled");
86109

87110
this.plugin_evt("beforeElementsAdded");
@@ -126,7 +149,14 @@ var codeInput = {
126149
/* Add code from value attribute - useful for loading from backend */
127150
this.update(value, this);
128151
}
129-
get observedAttributes() {
152+
153+
/* Callbacks */
154+
connectedCallback() {
155+
// Added to document
156+
this.template = this.get_template();
157+
if(this.template != undefined) this.setup();
158+
}
159+
static get observedAttributes() {
130160
let attrs = ["value", "placeholder", "lang", "template"]; // Attributes to monitor
131161

132162
/* Add from plugins */
@@ -135,6 +165,7 @@ var codeInput = {
135165
}
136166
return attrs;
137167
}
168+
138169
attributeChangedCallback(name, oldValue, newValue) {
139170
if(this.isConnected) {
140171
// This will sometimes be called before the element has been created, so trying to update an attribute causes an error.
@@ -207,7 +238,25 @@ var codeInput = {
207238
registerTemplate: function(template_name, template) {
208239
// Set default class
209240
codeInput.usedTemplates[template_name] = template;
210-
codeInput.defaultTemplate = template_name;
241+
// Add elements w/ template from queue
242+
if(template_name in codeInput.templateQueue) {
243+
for(let i in codeInput.templateQueue[template_name]) {
244+
elem = codeInput.templateQueue[template_name][i];
245+
elem.template = template;
246+
elem.setup();
247+
}
248+
}
249+
if(codeInput.defaultTemplate == undefined) {
250+
codeInput.defaultTemplate = template_name;
251+
// Add elements w/ default template from queue
252+
if(undefined in codeInput.templateQueue) {
253+
for(let i in codeInput.templateQueue[undefined]) {
254+
elem = codeInput.templateQueue[undefined][i];
255+
elem.template = template;
256+
elem.setup();
257+
}
258+
}
259+
}
211260
},
212261
templates: {
213262
custom(highlight=function() {}, preElementStyled=true, isCode=true, includeCodeInputInHighlightFunc=false, plugins=[]) {

code-input.min.css

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code-input.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)