-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
197 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { html } from '../../vendor/lit-element/lit-html/lit-html.js' | ||
import { repeat } from '../../vendor/lit-element/lit-html/directives/repeat.js' | ||
import { createBaseClass } from './base.js' | ||
import { makeSafe } from '../lib/strings.js' | ||
|
||
const penSvg = html`<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="pen" class="svg-inline--fa fa-pen fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M290.74 93.24l128.02 128.02-277.99 277.99-114.14 12.6C11.35 513.54-1.56 500.62.14 485.34l12.7-114.22 277.9-277.88zm207.2-19.06l-60.11-60.11c-18.75-18.75-49.16-18.75-67.91 0l-56.55 56.55 128.02 128.02 56.55-56.55c18.75-18.76 18.75-49.16 0-67.91z"></path></svg>` | ||
|
||
// exported api | ||
// = | ||
|
||
export const name = 'ctzn-code' | ||
|
||
export function setup (win, doc, editor) { | ||
class CtznCode extends createBaseClass(win) { | ||
get codeLines () { | ||
const nodes = this.shadowRoot.querySelector('slot')?.assignedNodes() || [] | ||
return nodes.map(n => n.textContent).join('\n').trim().split('\n') | ||
} | ||
|
||
render () { | ||
return html` | ||
<style> | ||
:host { | ||
display: block; | ||
background: #fff; | ||
border-radius: 6px; | ||
border: 1px solid #ccc; | ||
padding: 1rem; | ||
box-shadow: 0 2px 4px #0001; | ||
} | ||
.btn { | ||
display: inline-block; | ||
cursor: pointer; | ||
border-radius: 4px; | ||
padding: 0 8px; | ||
} | ||
.btn:hover { | ||
background: #eee; | ||
} | ||
.btn svg { | ||
width: 12px; | ||
color: #666; | ||
} | ||
slot { | ||
display: none; | ||
} | ||
header { | ||
padding-bottom: 8px; | ||
} | ||
.rendered-code { | ||
overflow-x: scroll; | ||
background-color: #F3F4F6; /*bg-gray-100*/ | ||
padding: 0.8rem; | ||
border: 1px solid #D1D5DB /*border-gray-300*/; | ||
border-radius: 5px; | ||
counter-reset: line; | ||
} | ||
.rendered-code > :last-child { | ||
margin-bottom: 0 !important; | ||
} | ||
.rendered-code > code { | ||
display: block; | ||
white-space: pre; | ||
color: #1F2937 /*text-gray-800*/; | ||
background-color: transparent; | ||
} | ||
.rendered-code > code::before { | ||
display: inline-block; | ||
text-align: right; | ||
width: 2ch; | ||
margin-right: 1ch; | ||
color: #9CA3AF /*text-gray-400*/; | ||
counter-increment: line; | ||
content: counter(line); | ||
} | ||
</style> | ||
<header> | ||
<strong>Code snippet</strong> | ||
<span class="btn" @click=${e => this.onClickEdit(e)}>${penSvg}</span> | ||
</header> | ||
<div class="rendered-code"> | ||
${repeat(this.codeLines, line => html`<code>${line}</code>`)} | ||
</div> | ||
<slot></slot> | ||
` | ||
} | ||
|
||
onClickEdit (e) { | ||
doPropertiesDialog(this, editor) | ||
} | ||
} | ||
win.customElements.define('ctzn-code', CtznCode) | ||
} | ||
|
||
export function insert (editor) { | ||
doPropertiesDialog(null, editor) | ||
} | ||
|
||
// internal methods | ||
// = | ||
|
||
function doPropertiesDialog (el, editor) { | ||
editor.windowManager.open({ | ||
title: 'Code snippet', | ||
size: 'large', | ||
body: { | ||
type: 'panel', | ||
items: [ | ||
{ | ||
type: 'textarea', | ||
name: 'code', | ||
placeholder: 'Insert your code snippet here' | ||
} | ||
] | ||
}, | ||
buttons: [ | ||
{ | ||
type: 'cancel', | ||
name: 'closeButton', | ||
text: 'Cancel' | ||
}, | ||
{ | ||
type: 'submit', | ||
name: 'submitButton', | ||
text: 'Save', | ||
primary: true | ||
} | ||
], | ||
initialData: { | ||
code: el ? el.textContent : '' | ||
}, | ||
onSubmit: (dialog) => { | ||
var data = dialog.getData() | ||
|
||
if (!el) { | ||
editor.insertContent(`<ctzn-code id="__new"><pre></pre></ctzn-code>`) | ||
let newEl = editor.$('#__new') | ||
newEl[0].querySelector('pre').textContent = data.code | ||
newEl[0].updateDom() | ||
editor.selection.select(newEl[0]) | ||
newEl.removeAttr('id') | ||
} | ||
else { | ||
editor.undoManager.transact(() => { | ||
el.querySelector('pre').textContent = data.code | ||
}) | ||
el.updateDom() | ||
editor.nodeChanged() | ||
} | ||
dialog.close() | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters