|
| 1 | +class ConfigEditor extends Component { |
| 2 | + refs = { |
| 3 | + config: '#config', |
| 4 | + textarea: '#config textarea[type="text"]', |
| 5 | + save: '.save', |
| 6 | + close: '.close' |
| 7 | + }; |
| 8 | + |
| 9 | + constructor() { |
| 10 | + super(); |
| 11 | + this.config = JSON.parse(localStorage.getItem("config")).config; |
| 12 | + } |
| 13 | + |
| 14 | + style() { |
| 15 | + return ` |
| 16 | + #config { |
| 17 | + position: absolute; |
| 18 | + display: flex; |
| 19 | + align-items: center; |
| 20 | + justify-content: center; |
| 21 | + width: calc(100% - 2px); |
| 22 | + height: 100%; |
| 23 | + background: var(--editor-bg); |
| 24 | + z-index: 99; |
| 25 | + visibility: hidden; |
| 26 | + top: -100%; |
| 27 | + transition: all .3s ease-in-out; |
| 28 | + } |
| 29 | +
|
| 30 | + #config.active { |
| 31 | + top: 0; |
| 32 | + visibility: visible; |
| 33 | + } |
| 34 | +
|
| 35 | + #config div { |
| 36 | + position: relative; |
| 37 | + width: 95%; |
| 38 | + } |
| 39 | +
|
| 40 | + #config textarea { |
| 41 | + border: 0; |
| 42 | + outline: 0; |
| 43 | + width: 100%; |
| 44 | + box-shadow: inset 0 -1px var(--editor-unfocus); |
| 45 | + padding: .5em 0; |
| 46 | + background: none; |
| 47 | + font: 350 12px 'Roboto', sans-serif; |
| 48 | + letter-spacing: 1px; |
| 49 | + color: var(--editor-text); |
| 50 | + resize: none; |
| 51 | + height: 28vh; |
| 52 | + -ms-overflow-style: none; |
| 53 | + scrollbar-width: none; |
| 54 | + } |
| 55 | +
|
| 56 | + #config textarea:focus { |
| 57 | + box-shadow: inset 0 -1px var(--editor-focus); |
| 58 | + } |
| 59 | +
|
| 60 | + #config textarea::selection { |
| 61 | + background: var(--editor-select-bg); |
| 62 | + color: var(--editor-select); |
| 63 | + } |
| 64 | +
|
| 65 | + #config textarea::-webkit-scrollbar { |
| 66 | + display: none; |
| 67 | + } |
| 68 | +
|
| 69 | + #config .save { |
| 70 | + background: 0; |
| 71 | + border: 0; |
| 72 | + outline: 0; |
| 73 | + color: var(--editor-save); |
| 74 | + position: absolute; |
| 75 | + right: 28px; |
| 76 | + cursor: pointer; |
| 77 | + top: 6px; |
| 78 | + } |
| 79 | +
|
| 80 | + #config .save:hover { |
| 81 | + filter: opacity(.8); |
| 82 | + } |
| 83 | +
|
| 84 | + #config .close { |
| 85 | + background: 0; |
| 86 | + border: 0; |
| 87 | + outline: 0; |
| 88 | + color: var(--editor-close); |
| 89 | + position: absolute; |
| 90 | + right: 0; |
| 91 | + cursor: pointer; |
| 92 | + top: 4px; |
| 93 | + } |
| 94 | +
|
| 95 | + #config .close:hover { |
| 96 | + filter: opacity(.8); |
| 97 | + } |
| 98 | +
|
| 99 | + .material-icons.md-20 { |
| 100 | + font-size: 20px; |
| 101 | + } |
| 102 | +
|
| 103 | + .material-icons.md-24 { |
| 104 | + font-size: 24px; |
| 105 | + } |
| 106 | +
|
| 107 | + `; |
| 108 | + } |
| 109 | + |
| 110 | + imports() { |
| 111 | + return [ |
| 112 | + this.resources.fonts.roboto, |
| 113 | + this.resources.icons.material |
| 114 | + ]; |
| 115 | + } |
| 116 | + |
| 117 | + template() { |
| 118 | + return ` |
| 119 | + <div id="config"> |
| 120 | + <div> |
| 121 | + <textarea type="text" spellcheck="false"></textarea> |
| 122 | + <button class="save"><i class="material-icons md-20"></i></button> |
| 123 | + <button class="close"><i class="material-icons md-24"></i></button> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + `; |
| 127 | + } |
| 128 | + |
| 129 | + activate() { |
| 130 | + this.refs.config.classList.add('active'); |
| 131 | + this.refs.textarea.scrollIntoView(); |
| 132 | + setTimeout(() => this.refs.textarea.focus(), 100); |
| 133 | + } |
| 134 | + |
| 135 | + deactivate() { |
| 136 | + this.refs.config.classList.remove('active'); |
| 137 | + } |
| 138 | + |
| 139 | + saveConfig() { |
| 140 | + localStorage.setItem("CONFIG", this.refs.textarea.value); |
| 141 | + this.deactivate(); |
| 142 | + location.reload(); |
| 143 | + } |
| 144 | + |
| 145 | + handleSearch(event) { |
| 146 | + const { key } = event; |
| 147 | + |
| 148 | + if (key === 'Escape') |
| 149 | + this.deactivate(); |
| 150 | + } |
| 151 | + |
| 152 | + setEvents() { |
| 153 | + this.refs.config.onkeyup = (e) => this.handleSearch(e); |
| 154 | + this.refs.close.onclick = () => this.deactivate(); |
| 155 | + this.refs.save.onclick = () => this.saveConfig(); |
| 156 | + } |
| 157 | + |
| 158 | + setConfig() { |
| 159 | + this.refs.textarea.value = JSON.stringify(this.config, null, 4); |
| 160 | + } |
| 161 | + |
| 162 | + connectedCallback() { |
| 163 | + this.render().then(() => { |
| 164 | + this.setEvents(); |
| 165 | + this.setConfig(); |
| 166 | + }); |
| 167 | + } |
| 168 | +} |
0 commit comments