Skip to content

Commit

Permalink
add code editor and refactor forms
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHadiAhmadi committed Sep 9, 2024
1 parent 01436a7 commit 3aeecd3
Show file tree
Hide file tree
Showing 32 changed files with 2,229 additions and 283 deletions.
12 changes: 9 additions & 3 deletions public/css/components/form.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
font-weight: 600;
}

[data-input], [data-textarea], [data-select] {
[data-input], [data-textarea], [data-code-editor], [data-select] {
width: 100%;
font-size: 14px;
padding: 8px 12px;
Expand All @@ -45,16 +45,22 @@
color: var(--body-color);
transition: all 0.2s ease;
}
[data-input]:disabled, [data-textarea]:disabled, [data-select]:disabled {
[data-input]:disabled, [data-textarea]:disabled,[data-code-editor]:disabled, [data-select]:disabled {
opacity: 0.6;
}

[data-input]:focus, [data-textarea]:focus, [data-select]:focus {
[data-input]:focus, [data-textarea]:focus,[data-code-editor]:focus, [data-select]:focus {
border-color: var(--input-focus-border);
box-shadow: 0 0 0 3px var(--input-focus-shadow);
outline: none;
}

[data-code-editor] {
font-family: 'Courier New', Courier, monospace;
min-height: 300px;
max-height: 80vh;
}

[data-file-label] {
position: relative;
width: 100%;
Expand Down
127 changes: 127 additions & 0 deletions public/css/hljs.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
:root {
--background: #f3f3f3;
--text: #444;
--comment: #697070;
--punctuation-tag: #444a;
--tag-attr-name: #444;
--attribute: #800;
--deletion-number-quote-selector-class-id-string-template-tag-type: #800;
--section-title: #800;
--link-operator-regexp-selector-attr-pseudo-symbol-template-variable-variable: #ab5656;
--literal: #695;
--addition-built-in-bullet-code: #397300;
--meta: #1f7199;
--meta-string: #38a;
}

[data-theme="dark"] {
--background: #2e2e2e;
--text: #ccc;
--comment: #8c8c8c;
--punctuation-tag: #aaa;
--tag-attr-name: #ccc;
--attribute: #ff6347;
--deletion-number-quote-selector-class-id-string-template-tag-type: #ff6347;
--section-title: #ff6347;
--link-operator-regexp-selector-attr-pseudo-symbol-template-variable-variable: #ff8c00;
--literal: #bada55;
--addition-built-in-bullet-code: #90ee90;
--meta: #00bfff;
--meta-string: #1e90ff;
}

pre code.hljs {
display: block;
overflow-x: auto;
padding: 1em;
background: var(--background);
color: var(--text);
}

code.hljs {
padding: 3px 5px;
}

.hljs {
background: var(--background);
color: var(--text);
}

.hljs-comment {
color: var(--comment);
}

.hljs-punctuation,
.hljs-tag {
color: var(--punctuation-tag);
}

.hljs-tag .hljs-attr,
.hljs-tag .hljs-name {
color: var(--tag-attr-name);
}

.hljs-attribute,
.hljs-doctag,
.hljs-keyword,
.hljs-meta .hljs-keyword,
.hljs-name,
.hljs-selector-tag {
font-weight: 700;
color: var(--attribute);
}

.hljs-deletion,
.hljs-number,
.hljs-quote,
.hljs-selector-class,
.hljs-selector-id,
.hljs-string,
.hljs-template-tag,
.hljs-type {
color: var(--deletion-number-quote-selector-class-id-string-template-tag-type);
}

.hljs-section,
.hljs-title {
color: var(--section-title);
font-weight: 700;
}

.hljs-link,
.hljs-operator,
.hljs-regexp,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-symbol,
.hljs-template-variable,
.hljs-variable {
color: var(--link-operator-regexp-selector-attr-pseudo-symbol-template-variable-variable);
}

.hljs-literal {
color: var(--literal);
}

.hljs-addition,
.hljs-built_in,
.hljs-bullet,
.hljs-code {
color: var(--addition-built-in-bullet-code);
}

.hljs-meta {
color: var(--meta);
}

.hljs-meta .hljs-string {
color: var(--meta-string);
}

.hljs-emphasis {
font-style: italic;
}

.hljs-strong {
font-weight: 700;
}
2 changes: 1 addition & 1 deletion public/css/sitebuilder.edit.css
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ iframe {
flex-direction: column;
padding-left: 32px;
padding-right: 16px;
max-height: 60vh;
max-height: 40vh;
overflow: auto;
}

Expand Down
28 changes: 14 additions & 14 deletions public/js/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getFormValue, request, setFormValue } from "./form.js"
import { getParentModule, reload, reloadIframe } from "./helpers.js"
import { request } from "./request.js"
import { getFormValue, setFormValue, getParentModule, reload, reloadIframe } from "./helpers.js"
import { hydrate } from "./hydrate.js"
import { updateModules } from "./sortable.js"

Expand Down Expand Up @@ -139,13 +139,13 @@ const actions = {
const textarea = form.querySelector('[name="template"]')
const submitButton = form.querySelector('button[type="submit"]')

if(textarea.dataset.hydrated !== 'true') {
textarea.dataset.hydrated = true
if(textarea.dataset.modalTextareaHydrated !== 'true') {
textarea.dataset.modalTextareaHydrated = true
textarea.addEventListener('keydown', onTextareaKeyDown)
}

if(submitButton.dataset.hydrated !== 'true') {
submitButton.dataset.hydrated = true
if(submitButton.dataset.submitButtonHydrated !== 'true') {
submitButton.dataset.submitButtonHydrated = true
submitButton.addEventListener('click', onButtonClick)
}
},
Expand Down Expand Up @@ -378,13 +378,13 @@ const actions = {
const textarea = form.querySelector('[name="template"]')
const submitButton = form.querySelector('button[type="submit"]')

if(textarea.dataset.hydrated !== 'true') {
textarea.dataset.hydrated = true
if(textarea.dataset.modalTextareaHydrated !== 'true') {
textarea.dataset.modalTextareaHydrated = true
textarea.addEventListener('keydown', onTextareaKeyDown)
}

if(submitButton.dataset.hydrated !== 'true') {
submitButton.dataset.hydrated = true
if(submitButton.dataset.submitButtonHydrated !== 'true') {
submitButton.dataset.submitButtonHydrated = true
submitButton.addEventListener('click', onButtonClick)
}

Expand Down Expand Up @@ -543,13 +543,13 @@ const actions = {
const textarea = form.querySelector('[name="template"]')
const submitButton = form.querySelector('button[type="submit"]')

if(textarea.dataset.hydrated !== 'true') {
textarea.dataset.hydrated = true
if(textarea.dataset.modalTextareaHydrated !== 'true') {
textarea.dataset.modalTextareaHydrated = true
textarea.addEventListener('keydown', onTextareaKeyDown)
}

if(submitButton.dataset.hydrated !== 'true') {
submitButton.dataset.hydrated = true
if(submitButton.dataset.submitButtonHydrated !== 'true') {
submitButton.dataset.submitButtonHydrated = true
submitButton.addEventListener('click', onButtonClick)
}

Expand Down
79 changes: 79 additions & 0 deletions public/js/code-editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {CodeJar} from './codejar.js'
// import * as hljs from '/js/highlight.js'
hljs.configure({
ignoreUnescapedHTML: true
})

export function CodeEditor(el) {
const value = (el.getAttribute('value') ?? '').replace(/\\n/g, '\n')
const readonly = el.hasAttribute('readonly')
const disabled = el.hasAttribute('disabled')
el.classList.add('language-' + el.getAttribute('lang'))


function highlight(el) {
return hljs.highlightElement(el)
}

console.log(hljs.highlightElement)
const instance = CodeJar(el, highlight, {
tab: ' ',

// catchTab: false
})

instance.onUpdate(val => delete el.dataset.highlighted)


if(value) {
instance.updateCode(value)
}

if(readonly) {
el.setAttribute('contenteditable','false');
el.setAttribute('tabindex','0');
}

if(disabled) {
el.setAttribute('contenteditable','false');
}

el.setValue = (val) => {
instance.updateCode(val)
}
el.getValue = () => {
return instance.toString()
}
}


// let thisValue = instance.toString();
// instance.onUpdate(code => {
// thisValue = code
// this.$data.value = code
// })

// Alpine.bind(el, {
// 'u-modelable': 'value',
// 'u-data'() {
// return {
// value: el.getAttribute('value')
// }
// },

// cleanup(() => {
// instance.destroy()
// })

// this.$watch('value', (value) => {

// if(thisValue === value) return;

// console.log('calling updateCode', value)
// instance.updateCode(value)


// })
// }
// })
// })
Loading

0 comments on commit 3aeecd3

Please sign in to comment.