diff --git a/docs/src/_documentation/components/light-editor.md b/docs/src/_documentation/components/light-editor.md index 9c594ad6..8b027774 100644 --- a/docs/src/_documentation/components/light-editor.md +++ b/docs/src/_documentation/components/light-editor.md @@ -1,6 +1,7 @@ --- title: permalink: /components/light-editor/ +component: light-editor --- @@ -11,3 +12,45 @@ permalink: /components/light-editor/ + +## With a template + + + + + +## Changing the language + + + + diff --git a/docs/src/_documentation/components/light-preview.md b/docs/src/_documentation/components/light-preview.md index 87602b7b..065613f8 100644 --- a/docs/src/_documentation/components/light-preview.md +++ b/docs/src/_documentation/components/light-preview.md @@ -1,6 +1,7 @@ --- title: permalink: /components/light-preview/ +component: light-preview --- ## Typical Usage diff --git a/exports/light-editor.js b/exports/light-editor.js index 2bc21f7d..d75fbfba 100644 --- a/exports/light-editor.js +++ b/exports/light-editor.js @@ -11,6 +11,7 @@ import { baseStyles } from "./base-styles.js"; import { styles } from "./light-editor.styles.js"; import { theme } from "./default-theme.styles.js"; import { LightResizeEvent } from "./events/light-resize-event.js" +import { dedent } from "../internal/dedent.js"; HighlightJS.registerLanguage('javascript', JavaScript); HighlightJS.registerLanguage('xml', HTML); @@ -41,7 +42,7 @@ export default class LightEditor extends BaseElement { static properties = { label: {}, value: {}, - language: {reflect: true} + language: {reflect: true}, } constructor () { @@ -130,10 +131,10 @@ export default class LightEditor extends BaseElement { }} @scroll=${this.syncScroll} .value=${this.value} - >${this.value} + > - + ` } @@ -170,10 +171,20 @@ export default class LightEditor extends BaseElement { * @param {Event} e */ handleSlotChangeEvent (e) { - const target = /** @type {HTMLSlotElement} */ (e.target) - - this.currentWatchedElements = target.assignedElements({ flatten: true }) // @TODO: Attach mutations observers to update value. + /** + * @type {HTMLSlotElement} + */ + // @ts-expect-error + const slot = e.target + + const templates = slot.assignedElements({flatten: true}) + + const code = dedent(this.unescapeCharacters(templates.map((template) => template.innerHTML).join("\n"))) + + if (code.trim()) { + this.value = code + } } /** @@ -231,7 +242,7 @@ export default class LightEditor extends BaseElement { let { code, language } = options code = this.unescapeCharacters(code) - // Dedent is nice, but we don't want to do it on user type data. + // Dedent is nice, but we don't want to do it on user typed data. // code = dedent(code) code = this.injectNewLine(code) diff --git a/exports/light-editor.styles.js b/exports/light-editor.styles.js index 5b375377..32f9563f 100644 --- a/exports/light-editor.styles.js +++ b/exports/light-editor.styles.js @@ -34,6 +34,11 @@ export const styles = css` overflow: auto; position: relative; + height: var(--textarea-height, auto); + width: var(--textarea-width, auto); + max-width: 100%; + max-height: 100%; + /* this creates line-wrapping. */ word-break: break-word; white-space: pre-wrap; @@ -50,12 +55,6 @@ export const styles = css` background-color: #f7f7f7; } - [part~="pre"] { - height: var(--textarea-height, auto); - max-height: var(--textarea-height, auto); - width: var(--textarea-width, auto); - max-width: var(--textarea-width, auto); - } [part~="pre"], [part~="code"], diff --git a/exports/light-pen.js b/exports/light-pen.js index 0c7578f8..334f2b02 100644 --- a/exports/light-pen.js +++ b/exports/light-pen.js @@ -209,11 +209,16 @@ export default class LightPen extends BaseElement { } updateIframeContent () { - const iframeElem= this.iframeElem + const iframeElem = this.iframeElem if (iframeElem == null) return // this.setupIframeLogging(); + console.log({ + cssCode: this.cssCode, + htmlCode: this.htmlCode, + jsCode: this.jsCode, + }) let page = ` @@ -298,7 +303,7 @@ export default class LightPen extends BaseElement { resetValues () { this.htmlCode = this.htmlEditor?.getAttribute("value") || "" this.cssCode = this.cssEditor?.getAttribute("value") || "" - this.jsCode = this.cssEditor?.getAttribute("value") || "" + this.jsCode = this.jsEditor?.getAttribute("value") || "" this.requestUpdate() } @@ -330,47 +335,6 @@ export default class LightPen extends BaseElement { // ) // } - /** - * @param {Event} e - */ - handleTemplate (e) { - /** - * @type {HTMLSlotElement} - */ - // @ts-expect-error - const slot = e.target - - const slotName = slot.getAttribute("name") - if (slotName == null) return - - if (!this.languages.includes(/** @type {SupportedLanguages} */ (slotName))) { - return - } - - const codeType = /** @type {SupportedLanguages} */ (slotName) - - const templates = slot.assignedElements({flatten: true}) - - const code = dedent(this.unescapeCharacters(templates.map((template) => template.innerHTML).join("\n"))) - - this[`${codeType}Code`] = code - - const textArea = this[`${codeType}Editor`] - - if (textArea && code) { - textArea.setAttribute("value", code) - textArea.value = code - } - } - - /** - * @param {string} text - */ - unescapeCharacters (text) { - // Update code - return text.replaceAll("</script>", "") - } - // Rendering renderConsole () { return html`
` @@ -382,12 +346,6 @@ export default class LightPen extends BaseElement { render () { return html` -
- - - -
-
@@ -515,14 +473,7 @@ export default class LightPen extends BaseElement { textarea:sandbox-editor__textarea " language=${highlightLang} - .value=${this[`${language}Code`]} - @light-input=${/** @param {Event} e */ (e) => { - this[`${language}Code`] = /** @type {LightEditor} */ (e.currentTarget).value - }} - @light-change=${/** @param {Event} e */ (e) => { - this[`${language}Code`] = /** @type {LightEditor} */ (e.currentTarget).value - }} - > + > ` } diff --git a/exports/light-pen.styles.js b/exports/light-pen.styles.js index 9a4c0eee..d2f3a55c 100644 --- a/exports/light-pen.styles.js +++ b/exports/light-pen.styles.js @@ -23,9 +23,11 @@ export const styles = css` outline: 2px solid blue; } +/* [part~="sandbox-editor"] { height: var(--textarea-height, 33%); } +*/ [part~="base"] { word-wrap: break-word;