editor.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <section class="container">
  2. <div class="left-half noprint">
  3. <div class="editor">
  4. <ul class="toolbar">
  5. <li><button type="button" data-command="bold">Fett</button></li>
  6. <li><button type="button" data-command="italic">Kursiv</button></li>
  7. <!-- <li><button type="button" data-command="link">Link</button></li>
  8. <li><button type="button" data-command="image">Bild</button></li> -->
  9. <li><button type="button" data-command="header1">Groß</button></li>
  10. <li><button type="button" data-command="header2">Bisschen Groß</button></li>
  11. <li><button type="button" data-command="unorderedList">Liste</button></li>
  12. <li><button type="button" data-command="orderedList">Aufzählung</button></li>
  13. <!-- <li><button type="button" data-command="code"></button></li> -->
  14. </ul>
  15. <textarea bind:value=text type=text>{text}</textarea>
  16. </div>
  17. </div>
  18. <div class="right-half">
  19. {#if schueler}
  20. {#each schueler as s (s.ID)}
  21. <Brief schueler={schueler}><span slot="kuerzel">{s.Lehrer}</span><span slot="anschrift">{s.Vorname} {s.Name}<br>{s.Strasse}<br>{s.PLZ} {s.OrtAbk}</span><svelte:component this={Component} s={s} /></Brief>
  22. {/each}
  23. {/if}
  24. </div>
  25. </section>
  26. <script>
  27. import svelte from 'svelte'
  28. import TextareaEditor from 'textarea-editor'
  29. import marked from 'marked'
  30. export default {
  31. oncreate () {
  32. const textarea = document.querySelector('textarea')
  33. const toolbar = document.querySelector('.toolbar')
  34. const editor = new TextareaEditor(textarea)
  35. toolbar.addEventListener('mousedown', e => e.preventDefault());
  36. toolbar.addEventListener('click', e => {
  37. const command = e.target.getAttribute('data-command');
  38. if (!command) return;
  39. let url;
  40. if (/image|link/.test(command) && !editor.hasFormat(command)) {
  41. url = prompt('URL:');
  42. }
  43. editor.toggle(command, url);
  44. })
  45. textarea.addEventListener('keydown', (e) => {
  46. const key = e.which;
  47. const cmd = e.metaKey || e.ctrlKey;
  48. if (!cmd) return;
  49. switch (key) {
  50. case 66:
  51. editor.toggle('bold');
  52. break;
  53. case 73:
  54. editor.toggle('italic');
  55. break;
  56. default: return;
  57. }
  58. e.preventDefault();
  59. })
  60. },
  61. data () {return {
  62. text: 'Hallo {s.Vorname}'
  63. }},
  64. computed: {
  65. Component: ({text}) => svelte.create(marked(text, { sanitize: false }))
  66. },
  67. components: {Brief: './partials/brief.html'}
  68. }
  69. </script>
  70. <style>
  71. @import 'daten/main.css';
  72. /* Pattern styles */
  73. .container {
  74. display: grid;
  75. grid-gap: 10px;
  76. grid-template-columns: 1fr 2fr;
  77. margin-top: 10px;
  78. }
  79. .left-half {
  80. grid-column: 1;
  81. }
  82. .right-half {
  83. grid-column: 2;
  84. }
  85. .editor {
  86. position: sticky;
  87. height: 90%;
  88. margin-left: 10px;
  89. }
  90. textarea {
  91. width: 100%;
  92. height: 100%;
  93. padding: 20px;
  94. border-color: #ccc;
  95. resize: none;
  96. font-size: 1.6em;
  97. }
  98. textarea:focus {
  99. outline: none;
  100. border-color: #3498db;
  101. }
  102. .toolbar {
  103. background: #ddd;
  104. list-style: none;
  105. margin: 0;
  106. padding: 0;
  107. overflow: hidden;
  108. width: 100%;
  109. }
  110. .toolbar button {
  111. float: left;
  112. font-weight: bold;
  113. background: #ddd;
  114. color: #333;
  115. border: none;
  116. padding: 15px;
  117. }
  118. .toolbar button:hover {
  119. background: #ccc;
  120. }
  121. </style>