Skip to content

Commit

Permalink
Fix Syntax Highlighting when 'src' is Not Defined
Browse files Browse the repository at this point in the history
  • Loading branch information
evanplaice committed May 29, 2020
1 parent d0f6417 commit 272a8ce
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Syntax highlighting requires importing a `mode` module for the language
Then specify the language with the `mode` attribute

```html
<wc-codemirror mode="javascript" src="sample.js"></wc-codemirror>
<wc-codemirror mode="javascript"></wc-codemirror>
```

### Theming
Expand All @@ -85,5 +85,5 @@ Theming requires importing an editor theme stylesheet
Then specify the theme with the `theme` attribute

```html
<wc-codemirror mode="javascript" theme="monokai" src="sample.js"></wc-codemirror>
<wc-codemirror mode="javascript" theme="monokai"></wc-codemirror>
```
9 changes: 6 additions & 3 deletions dev/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
<body>

<!-- basic -->
<wc-codemirror></wc-code-mirror>
<!-- <wc-codemirror></wc-code-mirror> -->

<!-- source -->
<!-- <wc-codemirror src="sample.txt"></wc-code-mirror> -->

<!-- syntax-highlighting -->
<!-- <wc-codemirror src="sample.js" mode="javascript"></wc-code-mirror> -->
<!-- <wc-codemirror mode="javascript"></wc-code-mirror> -->

<!-- theming -->
<!-- <wc-codemirror src="sample.js" mode="javascript" theme="monokai"></wc-code-mirror> -->
<!-- <wc-codemirror mode="javascript" theme="monokai"></wc-code-mirror> -->

<!-- html -->
<!-- <wc-codemirror src="sample.html" mode="htmlmixed" theme="duotone-light"></wc-code-mirror> -->

<!-- everything -->
<wc-codemirror src="sample.js" mode="javascript" theme="monokai"></wc-code-mirror>

</body>
</html>
31 changes: 18 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9712,7 +9712,7 @@ self.CodeMirror = CodeMirror;

class WCCodeMirror extends HTMLElement {
static get observedAttributes () {
return ['src', 'value'];
return ['src'];
}

attributeChangedCallback (name, oldValue, newValue) {
Expand All @@ -9726,12 +9726,11 @@ class WCCodeMirror extends HTMLElement {
set src (value) {
this.setAttribute('src', value);
this.setSrc();
this.__editor.refresh();
}

get value () { return this.__editor.getValue(); }
set value (value) {
this.__editor.swapDoc(CodeMirror.Doc(value), this.getAttribute('mode'));
this.setValue(value);
}

constructor () {
Expand All @@ -9747,17 +9746,9 @@ class WCCodeMirror extends HTMLElement {
async connectedCallback () {
this.__element = this.querySelector('#code');
this.__element.style = this.hasAttribute('style') ? this.style.cssText : 'width:100%;height:100%';
this.appendChild(this.__element);

if (this.hasAttribute('src')) {
const src = this.getAttribute('src');
this.__element.value = await this.fetchSrc(src);
} else {
this.__element.value = '';
}

const mode = this.getAttribute('mode') || 'null';
const theme = this.getAttribute('theme') || 'default';
const mode = this.hasAttribute('mode') ? this.getAttribute('mode') : 'null';
const theme = this.hasAttribute('theme') ? this.getAttribute('theme') : 'default';

this.__editor = CodeMirror.fromTextArea(this.__element, {
lineNumbers: true,
Expand All @@ -9766,13 +9757,27 @@ class WCCodeMirror extends HTMLElement {
theme
});

if (this.hasAttribute('src')) {
this.setSrc(this.getAttribute('src'));
} else {
// delay until editor initializes
await new Promise(resolve => setTimeout(resolve, 50));
this.setValue('');
}

this.__initialized = true;
}

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

async setValue (value) {
this.__editor.swapDoc(CodeMirror.Doc(value, this.getAttribute('mode')));
this.__editor.refresh();
}

async fetchSrc (src) {
Expand Down
2 changes: 1 addition & 1 deletion index.min.js

Large diffs are not rendered by default.

31 changes: 18 additions & 13 deletions src/wc-codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ self.CodeMirror = CodeMirror;

export class WCCodeMirror extends HTMLElement {
static get observedAttributes () {
return ['src', 'value'];
return ['src'];
}

attributeChangedCallback (name, oldValue, newValue) {
Expand All @@ -18,12 +18,11 @@ export class WCCodeMirror extends HTMLElement {
set src (value) {
this.setAttribute('src', value);
this.setSrc();
this.__editor.refresh();
}

get value () { return this.__editor.getValue(); }
set value (value) {
this.__editor.swapDoc(CodeMirror.Doc(value), this.getAttribute('mode'));
this.setValue(value);
}

constructor () {
Expand All @@ -39,17 +38,9 @@ export class WCCodeMirror extends HTMLElement {
async connectedCallback () {
this.__element = this.querySelector('#code');
this.__element.style = this.hasAttribute('style') ? this.style.cssText : 'width:100%;height:100%';
this.appendChild(this.__element);

if (this.hasAttribute('src')) {
const src = this.getAttribute('src');
this.__element.value = await this.fetchSrc(src);
} else {
this.__element.value = '';
}

const mode = this.getAttribute('mode') || 'null';
const theme = this.getAttribute('theme') || 'default';
const mode = this.hasAttribute('mode') ? this.getAttribute('mode') : 'null';
const theme = this.hasAttribute('theme') ? this.getAttribute('theme') : 'default';

this.__editor = CodeMirror.fromTextArea(this.__element, {
lineNumbers: true,
Expand All @@ -58,13 +49,27 @@ export class WCCodeMirror extends HTMLElement {
theme
});

if (this.hasAttribute('src')) {
this.setSrc(this.getAttribute('src'));
} else {
// delay until editor initializes
await new Promise(resolve => setTimeout(resolve, 50));
this.setValue('');
}

this.__initialized = true;
}

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

async setValue (value) {
this.__editor.swapDoc(CodeMirror.Doc(value, this.getAttribute('mode')));
this.__editor.refresh();
}

async fetchSrc (src) {
Expand Down

0 comments on commit 272a8ce

Please sign in to comment.