Skip to content

Commit dc4faa2

Browse files
authored
Merge pull request #170 from WebCoder49/pre-or-code-styled
Allow which of pre/code element styled to be overriden for prism/hljs
2 parents 6a53600 + d0789d6 commit dc4faa2

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

code-input.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ code-input:not(.code-input_pre-element-styled) pre code, code-input.code-input_p
5858

5959
code-input:not(.code-input_pre-element-styled) pre, code-input.code-input_pre-element-styled pre code {
6060
/* Remove all margin and padding from others */
61-
margin: 0px!important;
62-
padding: 0px!important;
61+
margin: 0!important;
62+
padding: 0!important;
63+
border: 0!important;
6364
}
6465

6566
code-input textarea, code-input pre, code-input pre * {

code-input.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export class Template {
298298
* Constructor to create a custom template instance. Pass this into `codeInput.registerTemplate` to use it.
299299
* I would strongly recommend using the built-in simpler template `codeInput.templates.prism` or `codeInput.templates.hljs`.
300300
* @param {(codeElement: HTMLElement) => void} highlight - a callback to highlight the code, that takes an HTML `<code>` element inside a `<pre>` element as a parameter
301-
* @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.
301+
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled (if so set to true), or the `<code>` element (false)?
302302
* @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]'.
303303
* @param {false} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
304304
* @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.
@@ -312,7 +312,7 @@ export class Template {
312312
* Constructor to create a custom template instance. Pass this into `codeInput.registerTemplate` to use it.
313313
* I would strongly recommend using the built-in simpler template `codeInput.templates.prism` or `codeInput.templates.hljs`.
314314
* @param {(codeElement: HTMLElement, codeInput: CodeInput) => void} highlight - a callback to highlight the code, that takes an HTML `<code>` element inside a `<pre>` element as a parameter
315-
* @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.
315+
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled (if so set to true), or the `<code>` element (false)?
316316
* @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]'.
317317
* @param {true} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
318318
* @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.
@@ -360,9 +360,10 @@ export namespace templates {
360360
* Constructor to create a template that uses Prism.js syntax highlighting (https://prismjs.com/)
361361
* @param {Object} prism Import Prism.js, then after that import pass the `Prism` object as this parameter.
362362
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
363+
* @param {boolean} preElementStyled - Defaults to true, which should be right for most themes. If the styling is broken, change to false. (See `codeInput.Template` constructor's definition.)
363364
* @returns template object
364365
*/
365-
constructor(prism: Object, plugins?: Plugin[])
366+
constructor(prism: Object, plugins?: Plugin[], preElementStyled?: boolean)
366367
}
367368
// ESM-SUPPORT-END-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation
368369
/**
@@ -378,9 +379,10 @@ export namespace templates {
378379
* Constructor to create a template that uses highlight.js syntax highlighting (https://highlightjs.org/)
379380
* @param {Object} hljs Import highlight.js, then after that import pass the `hljs` object as this parameter.
380381
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
382+
* @param {boolean} preElementStyled - Defaults to false, which should be right for most themes. If the styling is broken, change to true. (See `codeInput.Template` constructor's definition.)
381383
* @returns template object
382384
*/
383-
constructor(hljs: Object, plugins?: Plugin[])
385+
constructor(hljs: Object, plugins?: Plugin[], preElementStyled?: boolean)
384386
}
385387
// ESM-SUPPORT-END-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation
386388
/**

code-input.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ var codeInput = {
162162
* Constructor to create a custom template instance. Pass this into `codeInput.registerTemplate` to use it.
163163
* I would strongly recommend using the built-in simpler template `codeInput.templates.prism` or `codeInput.templates.hljs`.
164164
* @param {(codeElement: HTMLCodeElement, codeInput?: codeInput.CodeInput) => void} highlight - a callback to highlight the code, that takes an HTML `<code>` element inside a `<pre>` element as a parameter
165-
* @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.
165+
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled (if so set to true), or the `<code>` element (false)?
166166
* @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]'.
167167
* @param {boolean} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
168168
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.Plugin`
@@ -1049,12 +1049,13 @@ var codeInput = {
10491049
* Constructor to create a template that uses Prism.js syntax highlighting (https://prismjs.com/)
10501050
* @param {Object} prism Import Prism.js, then after that import pass the `Prism` object as this parameter.
10511051
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
1052+
* @param {boolean} preElementStyled - Defaults to true, which should be right for most themes. If the styling is broken, change to false. (See `codeInput.Template` constructor's definition.)
10521053
* @returns {codeInput.Template} template object
10531054
*/
1054-
constructor(prism, plugins = []) {
1055+
constructor(prism, plugins = [], preElementStyled = true) {
10551056
super(
10561057
prism.highlightElement, // highlight
1057-
true, // preElementStyled
1058+
preElementStyled, // preElementStyled
10581059
true, // isCode
10591060
false, // includeCodeInputInHighlightFunc
10601061
plugins
@@ -1073,15 +1074,16 @@ var codeInput = {
10731074
* Constructor to create a template that uses highlight.js syntax highlighting (https://highlightjs.org/)
10741075
* @param {Object} hljs Import highlight.js, then after that import pass the `hljs` object as this parameter.
10751076
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
1077+
* @param {boolean} preElementStyled - Defaults to false, which should be right for most themes. If the styling is broken, change to true. (See `codeInput.Template` constructor's definition.)
10761078
* @returns {codeInput.Template} template object
10771079
*/
1078-
constructor(hljs, plugins = []) {
1080+
constructor(hljs, plugins = [], preElementStyled = false) {
10791081
super(
10801082
function(codeElement) {
10811083
codeElement.removeAttribute("data-highlighted");
10821084
hljs.highlightElement(codeElement);
10831085
}, // highlight
1084-
false, // preElementStyled
1086+
preElementStyled, // preElementStyled
10851087
true, // isCode
10861088
false, // includeCodeInputInHighlightFunc
10871089
plugins

plugins/prism-line-numbers.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* https://prismjs.com/plugins/line-numbers/
55
* Files: prism-line-numbers.css
66
*/
7-
/* Update padding to match line-numbers plugin, and decrease width likewise */
8-
code-input.line-numbers textarea, code-input.line-numbers.code-input_pre-element-styled pre,
9-
.line-numbers code-input textarea, .line-numbers code-input.code-input_pre-element-styled pre {
7+
/* Update padding to match line-numbers plugin */
8+
code-input.line-numbers textarea, code-input.line-numbers.code-input_pre-element-styled pre, code-input.line-numbers:not(.code-input_pre-element-styled) pre code,
9+
.line-numbers code-input textarea, .line-numbers code-input.code-input_pre-element-styled pre, .line-numbers code-input:not(.code-input_pre-element-styled) pre code {
1010
padding-left: max(3.8em, var(--padding, 16px))!important;
1111
}
1212

tests/i18n.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
<code-input dir="ltr" template="hljs+" language="markdown"><textarea code-input-fallback placeholder="hljs ltr"></textarea></code-input>
5959

6060
<script>
61-
codeInput.registerTemplate("hljs", codeInput.templates.hljs(hljs, []));
62-
codeInput.registerTemplate("prism", codeInput.templates.prism(Prism, []));
61+
codeInput.registerTemplate("hljs", new codeInput.templates.Hljs(hljs, []));
62+
codeInput.registerTemplate("prism", new codeInput.templates.Prism(Prism, []));
6363

6464
// Attribution: Translated by Oliver Geer with some help from English Wiktionary
6565
let findAndReplaceTranslations = {
@@ -96,7 +96,7 @@
9696
tabForNavigation: "Tabulador y Mayús-Tabulador actualmente para la navegación por el teclado. Tecla para activar la indentación.",
9797
};
9898

99-
codeInput.registerTemplate("hljs+", codeInput.templates.hljs(hljs, [
99+
codeInput.registerTemplate("hljs+", new codeInput.templates.Hljs(hljs, [
100100
new codeInput.plugins.AutoCloseBrackets(),
101101
new codeInput.plugins.Autocomplete(function(popupElem, textarea, selectionEnd) {
102102
if(textarea.value.substring(selectionEnd-5, selectionEnd) == "popup") {
@@ -114,7 +114,7 @@
114114
new codeInput.plugins.SelectTokenCallbacks(codeInput.plugins.SelectTokenCallbacks.TokenSelectorCallbacks.createClassSynchronisation("in-selection"), false, true, true, true, true, false),
115115
//new codeInput.plugins.SpecialChars(true),
116116
]));
117-
codeInput.registerTemplate("prism+", codeInput.templates.prism(Prism, [
117+
codeInput.registerTemplate("prism+", new codeInput.templates.Prism(Prism, [
118118
new codeInput.plugins.AutoCloseBrackets(),
119119
new codeInput.plugins.Autocomplete(function(popupElem, textarea, selectionEnd) {
120120
if(textarea.value.substring(selectionEnd-5, selectionEnd) == "popup") {

tests/prism.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>code-input Tester</title>
77
<!--Import Prism-->
88
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css">
9-
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
9+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js" data-manual></script>
1010
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
1111
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.js"></script>
1212
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.css">

0 commit comments

Comments
 (0)