|
| 1 | +import { Controller } from '@hotwired/stimulus'; |
| 2 | +import { |
| 3 | + trans, |
| 4 | + setLocale, |
| 5 | + HELLO, |
| 6 | + SAY_HELLO, |
| 7 | + INVITATION_TITLE, |
| 8 | + NUM_OF_APPLES, |
| 9 | + FINISH_PLACE, |
| 10 | + PUBLISHED_AT, |
| 11 | + PROGRESS, |
| 12 | + VALUE_OF_OBJECT |
| 13 | +} from '../translator'; |
| 14 | + |
| 15 | +import hljs from 'highlight.js/lib/core'; |
| 16 | +import javascript from 'highlight.js/lib/languages/javascript'; |
| 17 | + |
| 18 | +hljs.registerLanguage('javascript', javascript); |
| 19 | + |
| 20 | +function highlight({ code, language = 'javascript' }) { |
| 21 | + return hljs.highlight(code, { language }).value; |
| 22 | +} |
| 23 | + |
| 24 | +export default class extends Controller { |
| 25 | + static targets = [ |
| 26 | + 'helloCode', |
| 27 | + 'helloOutput', |
| 28 | + |
| 29 | + 'sayHelloNameInput', |
| 30 | + 'sayHelloCode', |
| 31 | + 'sayHelloOutput', |
| 32 | + |
| 33 | + 'invitationTitleOrganizationGenderInput', |
| 34 | + 'invitationTitleOrganizationNameInput', |
| 35 | + 'invitationTitleCode', |
| 36 | + 'invitationTitleOutput', |
| 37 | + |
| 38 | + 'numOfApplesCountInput', |
| 39 | + 'numOfApplesCode', |
| 40 | + 'numOfApplesOutput', |
| 41 | + |
| 42 | + 'finishPlacePlaceInput', |
| 43 | + 'finishPlaceCode', |
| 44 | + 'finishPlaceOutput', |
| 45 | + |
| 46 | + 'publishedAtDateInput', |
| 47 | + 'publishedAtCode', |
| 48 | + 'publishedAtOutput', |
| 49 | + |
| 50 | + 'progressAtProgressInput', |
| 51 | + 'progressAtCode', |
| 52 | + 'progressAtOutput', |
| 53 | + |
| 54 | + 'valueOfObjectValueInput', |
| 55 | + 'valueOfObjectCode', |
| 56 | + 'valueOfObjectOutput', |
| 57 | + ] |
| 58 | + |
| 59 | + connect() { |
| 60 | + this.render(); |
| 61 | + } |
| 62 | + |
| 63 | + setLocale(e) { |
| 64 | + setLocale(e.target.value); |
| 65 | + this.render(); |
| 66 | + } |
| 67 | + |
| 68 | + render() { |
| 69 | + this.helloCodeTarget.innerHTML = highlight({ code: `import { trans, SAY_HELLO } from '../translator'; |
| 70 | + |
| 71 | +trans(HELLO)` }); |
| 72 | + this.helloOutputTarget.textContent = trans(HELLO); |
| 73 | + |
| 74 | + this.sayHelloCodeTarget.innerHTML = highlight({ |
| 75 | + code: `import { trans, SAY_HELLO } from '../translator'; |
| 76 | + |
| 77 | +trans(SAY_HELLO, { |
| 78 | + name: ${this.#quoteValue(this.sayHelloNameInputTarget.value)} |
| 79 | +})` |
| 80 | + }); |
| 81 | + this.sayHelloOutputTarget.textContent = trans(SAY_HELLO, { |
| 82 | + name: this.sayHelloNameInputTarget.value |
| 83 | + }); |
| 84 | + |
| 85 | + this.invitationTitleCodeTarget.innerHTML = highlight({ |
| 86 | + code: `import { trans, INVITATION_TITLE } from '../translator'; |
| 87 | + |
| 88 | +trans(INVITATION_TITLE, { |
| 89 | + organizer_gender: ${this.#quoteValue(this.invitationTitleOrganizationGenderInputTarget.value)}, |
| 90 | + organizer_name: ${this.#quoteValue(this.invitationTitleOrganizationNameInputTarget.value)}, |
| 91 | +})` |
| 92 | + }) ; |
| 93 | + this.invitationTitleOutputTarget.textContent = trans(INVITATION_TITLE, { |
| 94 | + organizer_gender: this.invitationTitleOrganizationGenderInputTarget.value, |
| 95 | + organizer_name: this.invitationTitleOrganizationNameInputTarget.value |
| 96 | + }); |
| 97 | + |
| 98 | + this.numOfApplesCodeTarget.innerHTML = highlight({ |
| 99 | + code: `import { trans, NUM_OF_APPLES } from '../translator'; |
| 100 | + |
| 101 | +trans(NUM_OF_APPLES, { |
| 102 | + apples: ${this.numOfApplesCountInputTarget.value} |
| 103 | +})` |
| 104 | + }); |
| 105 | + this.numOfApplesOutputTarget.textContent = trans(NUM_OF_APPLES, { |
| 106 | + apples: this.numOfApplesCountInputTarget.value |
| 107 | + }) |
| 108 | + |
| 109 | + this.finishPlaceCodeTarget.innerHTML = highlight({ |
| 110 | + code: `import { trans, FINISH_PLACE } from '../translator'; |
| 111 | + |
| 112 | +trans(FINISH_PLACE, { |
| 113 | + place: ${this.finishPlacePlaceInputTarget.value} |
| 114 | +})` |
| 115 | + }); |
| 116 | + this.finishPlaceOutputTarget.textContent = trans(FINISH_PLACE, { |
| 117 | + place: this.finishPlacePlaceInputTarget.value, |
| 118 | + }); |
| 119 | + |
| 120 | + this.publishedAtCodeTarget.innerHTML = highlight({ |
| 121 | + code: `import { trans, PUBLISHED_AT } from '../translator'; |
| 122 | + |
| 123 | +trans(PUBLISHED_AT, { |
| 124 | + publication_date: new Date('${this.publishedAtDateInputTarget.value}') |
| 125 | +})` |
| 126 | + }); |
| 127 | + this.publishedAtOutputTarget.textContent = trans(PUBLISHED_AT, { |
| 128 | + publication_date: new Date(this.publishedAtDateInputTarget.value), |
| 129 | + }); |
| 130 | + |
| 131 | + this.progressAtCodeTarget.innerHTML = highlight({ |
| 132 | + code: `import { trans, PROGRESS } from '../translator'; |
| 133 | + |
| 134 | +trans(PROGRESS, { |
| 135 | + progress: ${this.progressAtProgressInputTarget.value / 100}, |
| 136 | +})` |
| 137 | + }); |
| 138 | + this.progressAtOutputTarget.textContent = trans(PROGRESS, { |
| 139 | + progress: this.progressAtProgressInputTarget.value / 100, |
| 140 | + }); |
| 141 | + |
| 142 | + this.valueOfObjectCodeTarget.innerHTML = highlight({ |
| 143 | + code: `import { trans, VALUE_OF_OBJECT } from '../translator'; |
| 144 | + |
| 145 | +trans(VALUE_OF_OBJECT, { |
| 146 | + value: ${this.valueOfObjectValueInputTarget.value} |
| 147 | +})` |
| 148 | + }); |
| 149 | + this.valueOfObjectOutputTarget.textContent = trans(VALUE_OF_OBJECT, { |
| 150 | + value: this.valueOfObjectValueInputTarget.value |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + #quoteValue(value) { |
| 155 | + return '\'' + value.replace('\'', '\\\'') + '\''; |
| 156 | + } |
| 157 | +} |
0 commit comments