Skip to content

Commit

Permalink
Fix Again
Browse files Browse the repository at this point in the history
  • Loading branch information
evanplaice committed Dec 23, 2019
1 parent 1990bc4 commit 3dd77dc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion demo/basic-usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="module" src="https://cdn.jsdelivr.net/npm/@vanillawc/wc-demo@latest/index.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@vanillawc/[email protected].4/index.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@vanillawc/[email protected].6/index.js"></script>
</head>
<body>
<wc-demo title="WC-CodeMirror" link="https://github.com/vanillawc/wc-codemirror" desc="Editor Basic Setup" src="assets/basic-usage.sample"></wc-demo>
Expand Down
25 changes: 17 additions & 8 deletions dist/wc-codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -9692,6 +9692,7 @@ class WCCodeMirror extends HTMLElement {
}

attributeChangedCallback (name, oldValue, newValue) {
if (!this.__initialized) { return; }
if (oldValue !== newValue) {
this[name] = newValue;
}
Expand All @@ -9700,7 +9701,8 @@ class WCCodeMirror extends HTMLElement {
get src () { return this.getAttribute('src'); }
set src (value) {
this.setAttribute('src', value);
this.fetchSrc();
console.log('wat');
this.setSrc();
this.__editor.refresh();
}

Expand All @@ -9714,33 +9716,40 @@ class WCCodeMirror extends HTMLElement {
const template = document.createElement('template');
template.innerHTML = WCCodeMirror.template();
this.appendChild(template.content.cloneNode(true));
this.__initialized = false;
this.__element = null;
this.__editor = null;
}

async connectedCallback () {
this.initialize();
}

initialize () {
this.__element = this.querySelector('#code');
this.__element.style = this.hasAttribute('style') ? this.style.cssText : 'width:100%;height:100%';
this.appendChild(this.__element);

const src = this.getAttribute('src');
this.__element.value = await this.fetchSrc(src);

this.__editor = CodeMirror.fromTextArea(this.__element, {
lineNumbers: true,
readOnly: false,
mode: this.getAttribute('mode'),
theme: this.getAttribute('theme')
});

this.__initialized = true;
}

async fetchSrc () {
const response = await fetch(this.src);
const contents = await response.text();
async setSrc () {
const src = this.getAttribute('src');
const contents = await this.fetchSrc(src);
this.__editor.swapDoc(CodeMirror.Doc(contents, this.getAttribute('mode')));
}

async fetchSrc (src) {
const response = await fetch(src);
return response.text();
}

static template () {
return `
<style>
Expand Down
2 changes: 1 addition & 1 deletion src/wc-codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export class WCCodeMirror extends HTMLElement {

constructor () {
super();
this.__initialized = false;
const template = document.createElement('template');
template.innerHTML = WCCodeMirror.template();
this.appendChild(template.content.cloneNode(true));
this.__initialized = false;
this.__element = null;
this.__editor = null;
}
Expand Down

0 comments on commit 3dd77dc

Please sign in to comment.