Skip to content

Commit bea73fa

Browse files
committed
Add placeholder to last empty line (to ensure alignment) if template specifies it
1 parent 2fd5d48 commit bea73fa

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

code-input.d.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ export class Template {
295295
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled as well as the `<code>` element? If true, `<pre>` element's scrolling is synchronised; if false, `<code>` element's scrolling is synchronised.
296296
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
297297
* @param {false} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
298-
* @param {boolean} autoDisableDuplicateSearching - Leaving this as true uses code-input's default fix for preventing duplicate results in Ctrl+F searching from the input and result elements, and setting this to false indicates your highlighting function implements its own fix. The default fix works by moving text content from elements to CSS `::before` pseudo-elements after highlighting.
299298
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.Plugin`
299+
* @param {boolean} addPlaceholderToLastEmptyLine - Setting this to true adds a space character to the end of the `<code>` element before highlighting when its last line is empty, ensuring that last line is displayed and aligns with the editing.
300300
* @returns template object
301301
*/
302-
constructor(highlight?: (codeElement: HTMLElement) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: false, autoDisableDuplicateSearching?: boolean, plugins?: Plugin[])
302+
constructor(highlight?: (codeElement: HTMLElement) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: false, plugins?: Plugin[], addPlaceholderToLastEmptyLine?: boolean)
303303
/**
304304
* **When `includeCodeInputInHighlightFunc` is `true`, `highlight` takes two parameters: the `<pre><code>` element, and the `<code-input>` element.**
305305
*
@@ -309,25 +309,17 @@ export class Template {
309309
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled as well as the `<code>` element? If true, `<pre>` element's scrolling is synchronised; if false, `<code>` element's scrolling is synchronised.
310310
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
311311
* @param {true} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
312-
* @param {boolean} autoDisableDuplicateSearching - Leaving this as true uses code-input's default fix for preventing duplicate results in Ctrl+F searching from the input and result elements, and setting this to false indicates your highlighting function implements its own fix. The default fix works by moving text content from elements to CSS `::before` pseudo-elements after highlighting.
313312
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.Plugin`
313+
* @param {boolean} addPlaceholderToLastEmptyLine - Setting this to true adds a space character to the end of the `<code>` element before highlighting when its last line is empty, ensuring that last line is displayed and aligns with the editing.
314314
* @returns template object
315315
*/
316-
constructor(highlight?: (codeElement: HTMLElement, codeInput: CodeInput) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: true, autoDisableDuplicateSearching?: boolean, plugins?: Plugin[])
316+
constructor(highlight?: (codeElement: HTMLElement, codeInput: CodeInput) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: true, plugins?: Plugin[], addPlaceholderToLastEmptyLine?: boolean)
317317
highlight: Function
318318
preElementStyled: boolean
319319
isCode: boolean
320320
includeCodeInputInHighlightFunc: boolean
321-
autoDisableDuplicateSearching: boolean
322321
plugins: Plugin[]
323-
/**
324-
* @deprecated Please give a value for the `autoDisableDuplicateSearching` parameter.
325-
*/
326-
constructor(highlight?: (code: HTMLElement) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: false, plugins?: Plugin[])
327-
/**
328-
* @deprecated Please give a value for the `autoDisableDuplicateSearching` parameter.
329-
*/
330-
constructor(highlight?: (code: HTMLElement, codeInput: CodeInput) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: true, plugins?: Plugin[])
322+
addPlaceholderToLastEmptyLine: boolean
331323
}
332324

333325
/**

code-input.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,17 @@ var codeInput = {
163163
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
164164
* @param {boolean} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
165165
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.Plugin`
166+
* @param {boolean} addPlaceholderToLastEmptyLine - Setting this to true adds a space character to the end of the `<code>` element before highlighting when its last line is empty, ensuring that last line is displayed and aligns with the editing.
166167
* @returns {codeInput.Template} template object
167168
*/
168-
constructor(highlight = function (codeElement) { }, preElementStyled = true, isCode = true, includeCodeInputInHighlightFunc = false, plugins = []) {
169+
constructor(highlight = function (codeElement) { }, preElementStyled = true, isCode = true, includeCodeInputInHighlightFunc = false, plugins = [], addPlaceholderToLastEmptyLine = false) {
170+
169171
this.highlight = highlight;
170172
this.preElementStyled = preElementStyled;
171173
this.isCode = isCode;
172174
this.includeCodeInputInHighlightFunc = includeCodeInputInHighlightFunc;
173175
this.plugins = plugins;
176+
this.addPlaceholderToLastEmptyLine = addPlaceholderToLastEmptyLine;
174177
}
175178

176179
/**
@@ -207,6 +210,13 @@ var codeInput = {
207210
* see `codeInput.Plugin`.
208211
*/
209212
plugins = [];
213+
214+
/**
215+
* Setting this to true adds a space character to the end of the
216+
* `<code>` element before highlighting when its last line is empty, ensuring that last
217+
* line is displayed and aligns with the editing.
218+
*/
219+
addPlaceholderToLastEmptyLine = false;
210220
},
211221

212222
/**
@@ -258,6 +268,7 @@ var codeInput = {
258268
preElementStyled: true,
259269
isCode: false,
260270
plugins: plugins,
271+
addPlaceholderToLastEmptyLine: false,
261272
}
262273
},
263274

@@ -282,6 +293,7 @@ var codeInput = {
282293
delimiter: delimiter,
283294

284295
plugins: plugins,
296+
addPlaceholderToLastEmptyLine: false,
285297
}
286298
},
287299

@@ -502,7 +514,9 @@ var codeInput = {
502514
update() {
503515
let resultElement = this.codeElement;
504516
let value = this.value;
505-
value += "\n"; // Placeholder for next line
517+
if(this.template.addPlaceholderToLastEmptyLine && (value[value.length-1] == "\n" || value.length == 0)) { // If last line empty
518+
value += " "; // Add a placeholder space character to the final line
519+
}
506520

507521
// Update code
508522
resultElement.innerHTML = this.escapeHtml(value);
@@ -614,11 +628,6 @@ var codeInput = {
614628
this.classList.remove("code-input_mouse-focused");
615629
});
616630

617-
// @deprecated Right now it's better to use the textarea element directly once it's loaded.
618-
textarea.addEventListener("scroll", (evt) => {
619-
this.dispatchEvent("scroll", evt);
620-
});
621-
622631
this.innerHTML = ""; // Clear Content
623632

624633
// Synchronise attributes to textarea
@@ -1104,7 +1113,8 @@ var codeInput = {
11041113
true, // preElementStyled
11051114
true, // isCode
11061115
false, // includeCodeInputInHighlightFunc
1107-
plugins
1116+
plugins,
1117+
true // addPlaceholderToLastEmptyLine
11081118
);
11091119
}
11101120
};
@@ -1131,7 +1141,8 @@ var codeInput = {
11311141
false, // preElementStyled
11321142
true, // isCode
11331143
false, // includeCodeInputInHighlightFunc
1134-
plugins
1144+
plugins,
1145+
true // addPlaceholderToLastEmptyLine
11351146
);
11361147
}
11371148
};

tests/tester.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ async function startTests(textarea, isHLJS) {
194194
let renderedValue = codeInputElement.codeElement.innerHTML.replace(/<[^>]+>/g, "");
195195
assertEqual("Core", "Initial Rendered Value", renderedValue, `console.log("Hello, World!");
196196
// A second line
197-
// A third line with &lt;html&gt; tags
198-
`); // Extra newline so line numbers visible if enabled
197+
// A third line with &lt;html&gt; tags`);
199198

200199

201200
// Update code-input value with JavaScript, new value and num events should be correct.
@@ -214,8 +213,7 @@ console.log("I've got another line!", 2 < 3, "should be true.");`);
214213
assertEqual("Core", "JS-updated Rendered Value", renderedValue, `console.log("Hello, World!");
215214
// A second line
216215
// A third line with &lt;html&gt; tags
217-
console.log("I've got another line!", 2 &lt; 3, "should be true.");
218-
`); // Extra newline so line numbers visible if enabled
216+
console.log("I've got another line!", 2 &lt; 3, "should be true.");`);
219217

220218
// Event Listener Tests
221219
// Function type listeners
@@ -323,8 +321,7 @@ console.log("I've got another line!", 2 &lt; 3, "should be true.");
323321
renderedValue = codeInputElement.codeElement.innerHTML.replace(/<[^>]+>/g, "");
324322
assertEqual("Core", "Form Reset resets Rendered Value", renderedValue, `console.log("Hello, World!");
325323
// A second line
326-
// A third line with &lt;html&gt; tags
327-
`); // Extra newline so line numbers visible if enabled.
324+
// A third line with &lt;html&gt; tags`);
328325

329326
/*--- Tests for plugins ---*/
330327
// AutoCloseBrackets

0 commit comments

Comments
 (0)