Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | /chat input field rework, as discussed in [forum:9e85f44f864eb1f5 | forum post 9e85f44f864eb1f5]. Part 1: revert to plain text input fields, with compact-mode toggle swapping between them. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | chat-input-revisited |
Files: | files | file ages | folders |
SHA3-256: |
136d95b6f172d9b24ed2ca0164177216 |
User & Date: | stephan 2021-10-10 04:13:53 |
Context
2021-10-10
| ||
05:53 | /chat: added option to toggle between text and contenteditable widget, defaulting to the former. Prettied up the config view a bit and made it more right-handed friendly. ... (check-in: 5d7c98ef92 user: stephan tags: chat-input-revisited) | |
04:13 | /chat input field rework, as discussed in [forum:9e85f44f864eb1f5 | forum post 9e85f44f864eb1f5]. Part 1: revert to plain text input fields, with compact-mode toggle swapping between them. ... (check-in: 136d95b6f1 user: stephan tags: chat-input-revisited) | |
2021-10-09
| ||
14:43 | Version 2.17 ... (check-in: f48180f2ff user: drh tags: trunk, release, version-2.17) | |
Changes
Changes to src/chat.c.
︙ | ︙ | |||
172 173 174 175 176 177 178 | db_get("chat-alert-sound","alerts/plunk.wav")); zProjectName = db_get("project-name","Unnamed project"); zInputPlaceholder0 = mprintf("Type markdown-formatted message for %h.", zProjectName); style_set_current_feature("chat"); style_header("Chat"); @ <div id='chat-input-area'> | | > > > > > > > > | | | 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | db_get("chat-alert-sound","alerts/plunk.wav")); zProjectName = db_get("project-name","Unnamed project"); zInputPlaceholder0 = mprintf("Type markdown-formatted message for %h.", zProjectName); style_set_current_feature("chat"); style_header("Chat"); @ <div id='chat-input-area'> @ <div id='chat-input-line-wrapper' class='compact'> @ <input type="text" id="chat-input-field-single" \ @ data-placeholder0="%h(zInputPlaceholder0)" \ @ data-placeholder="%h(zInputPlaceholder0)" \ @ class="chat-input-field"></input> @ <textarea id="chat-input-field-multi" \ @ data-placeholder0="%h(zInputPlaceholder0)" \ @ data-placeholder="%h(zInputPlaceholder0)" \ @ class="chat-input-field hidden"></textarea> @ <div contenteditable id="chat-input-field-x" \ @ data-placeholder0="%h(zInputPlaceholder0)" \ @ data-placeholder="%h(zInputPlaceholder0)" \ @ class="chat-input-field hidden"></div> @ <div id='chat-buttons-wrapper'> @ <span class='cbutton' id="chat-button-preview" \ @ title="Preview message (Shift-Enter)">👁</span> @ <span class='cbutton' id="chat-button-attach" \ @ title="Attach file to message">%s(zPaperclip)</span> @ <span class='cbutton' id="chat-button-settings" \ @ title="Configure chat">⚙</span> |
︙ | ︙ |
Changes to src/fossil.page.chat.js.
︙ | ︙ | |||
92 93 94 95 96 97 98 | var extra = 0; if(com){ ht = wh; }else{ elemsToCount.forEach((e)=>e ? extra += D.effectiveHeight(e) : false); ht = wh - extra; } | | | | | > > | 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | var extra = 0; if(com){ ht = wh; }else{ elemsToCount.forEach((e)=>e ? extra += D.effectiveHeight(e) : false); ht = wh - extra; } f.chat.e.inputX.style.maxHeight = (ht/2)+"px"; /* ^^^^ this is a middle ground between having no size cap on the input field and having a fixed arbitrary cap. */; contentArea.style.height = contentArea.style.maxHeight = [ "calc(", (ht>=100 ? ht : 100), "px", " - 0.75em"/*fudge value*/,")" /* ^^^^ hypothetically not needed, but both Chrome/FF on Linux will force scrollbars on the body if this value is too small (<0.75em in my tests). */ ].join(''); if(false){ console.debug("resized.",wh, extra, ht, window.getComputedStyle(contentArea).maxHeight, contentArea); console.debug("Set input max height to: ", f.chat.e.inputX.style.maxHeight); } }; var doit; window.addEventListener('resize',function(ev){ clearTimeout(doit); doit = setTimeout(resized, 100); }, false); return resized; })(); ForceResizeKludge.$disabled = true/*gets deleted when setup is finished*/; fossil.FRK = ForceResizeKludge/*for debugging*/; const Chat = ForceResizeKludge.chat = (function(){ const cs = { verboseErrors: false /* if true then certain, mostly extraneous, error messages may be sent to the console. */, e:{/*map of certain DOM elements.*/ messageInjectPoint: E1('#message-inject-point'), pageTitle: E1('head title'), loadOlderToolbar: undefined /* the load-posts toolbar (dynamically created) */, inputWrapper: E1("#chat-input-area"), inputElementWrapper: E1('#chat-input-line-wrapper'), fileSelectWrapper: E1('#chat-input-file-area'), viewMessages: E1('#chat-messages-wrapper'), btnSubmit: E1('#chat-button-submit'), btnAttach: E1('#chat-button-attach'), inputX: E1('#chat-input-field-x'), input1: E1('#chat-input-field-single'), inputM: E1('#chat-input-field-multi'), inputFile: E1('#chat-input-file'), contentDiv: E1('div.content'), viewConfig: E1('#chat-config'), viewPreview: E1('#chat-preview'), previewContent: E1('#chat-preview-content'), btnPreview: E1('#chat-button-preview'), views: document.querySelectorAll('.chat-view'), |
︙ | ︙ | |||
174 175 176 177 178 179 180 | }, /** Gets (no args) or sets (1 arg) the current input text field value, taking into account single- vs multi-line input. The getter returns a string and the setter returns this object. */ inputValue: function(){ const e = this.inputElement(); if(arguments.length){ | > | | | | 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | }, /** Gets (no args) or sets (1 arg) the current input text field value, taking into account single- vs multi-line input. The getter returns a string and the setter returns this object. */ inputValue: function(){ const e = this.inputElement(); if(arguments.length){ if(e.isContentEditable) e.innerText = arguments[0]; else e.value = arguments[0]; return this; } return e.isContentEditable ? e.innerText : e.value; }, /** Asks the current user input field to take focus. Returns this. */ inputFocus: function(){ this.inputElement().focus(); return this; }, /** Returns the current message input element. */ inputElement: function(){ return this.e.inputFields[this.e.inputFields.$currentIndex]; }, /** Enables (if yes is truthy) or disables all elements in * this.disableDuringAjax. */ enableAjaxComponents: function(yes){ D[yes ? 'enable' : 'disable'](this.disableDuringAjax); return this; }, |
︙ | ︙ | |||
577 578 579 580 581 582 583 | animate: function f(e,a,cb){ if(!f.$disabled){ D.addClassBriefly(e, a, 0, cb); } return this; } }; | > > > > > > | | | 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | animate: function f(e,a,cb){ if(!f.$disabled){ D.addClassBriefly(e, a, 0, cb); } return this; } }; cs.e.inputFields = [ cs.e.input1, cs.e.inputM, cs.e.inputX ]; cs.e.inputFields.$currentIndex = 0; cs.e.inputFields.forEach(function(e,ndx){ if(ndx===cs.e.inputFields.$currentIndex) D.removeClass(e,'hidden'); else D.addClass(e,'hidden'); }); if(D.attr(cs.e.inputX,'contenteditable','plaintext-only').isContentEditable){ cs.$browserHasPlaintextOnly = true; }else{ /* Only the Chrome family supports contenteditable=plaintext-only */ cs.$browserHasPlaintextOnly = false; D.attr(cs.e.inputX,'contenteditable','true'); } cs.animate.$disabled = true; F.fetch.beforesend = ()=>cs.ajaxStart(); F.fetch.aftersend = ()=>cs.ajaxEnd(); cs.pageTitleOrig = cs.e.pageTitle.innerText; const qs = (e)=>document.querySelector(e); const argsToArray = function(args){ |
︙ | ︙ | |||
1159 1160 1161 1162 1163 1164 1165 | document.addEventListener('paste', pasteListener, true); if(window.Selection && window.Range && !Chat.$browserHasPlaintextOnly){ /* Acrobatics to keep *some* installations of Firefox from pasting formatting into contenteditable fields. This also works on Chrome, but chrome has the contenteditable=plaintext-only property which does this for us. */ | | | 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 | document.addEventListener('paste', pasteListener, true); if(window.Selection && window.Range && !Chat.$browserHasPlaintextOnly){ /* Acrobatics to keep *some* installations of Firefox from pasting formatting into contenteditable fields. This also works on Chrome, but chrome has the contenteditable=plaintext-only property which does this for us. */ Chat.e.inputX.addEventListener( 'paste', function(ev){ if (ev.clipboardData && ev.clipboardData.getData) { const pastedText = ev.clipboardData.getData('text/plain'); const selection = window.getSelection(); if (!selection.rangeCount) return false; selection.deleteFromDocument(/*remove selected content*/); |
︙ | ︙ | |||
1183 1184 1185 1186 1187 1188 1189 | which is not compatible with how we use it, so... */ ev.dataTransfer.effectAllowed = 'none'; ev.dataTransfer.dropEffect = 'none'; ev.preventDefault(); ev.stopPropagation(); return false; }; | < < | < | 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 | which is not compatible with how we use it, so... */ ev.dataTransfer.effectAllowed = 'none'; ev.dataTransfer.dropEffect = 'none'; ev.preventDefault(); ev.stopPropagation(); return false; }; ['drop','dragenter','dragleave','dragend'].forEach( (k)=>Chat.e.inputX.addEventListener(k, noDragDropEvents, false) ); return bxs; })()/*drag/drop/paste*/; const tzOffsetToString = function(off){ const hours = Math.round(off/60), min = Math.round(off % 30); return ''+(hours + (min ? '.5' : '')); |
︙ | ︙ | |||
1321 1322 1323 1324 1325 1326 1327 | /* Ship it! */ ev.preventDefault(); ev.stopPropagation(); Chat.submitMessage(); return false; } }; | > | > | 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 | /* Ship it! */ ev.preventDefault(); ev.stopPropagation(); Chat.submitMessage(); return false; } }; Chat.e.inputFields.forEach( (e)=>e.addEventListener('keydown', inputWidgetKeydown, false) ); Chat.e.btnSubmit.addEventListener('click',(e)=>{ e.preventDefault(); Chat.submitMessage(); return false; }); Chat.e.btnAttach.addEventListener( 'click', ()=>Chat.e.inputFile.click(), false); |
︙ | ︙ | |||
1569 1570 1571 1572 1573 1574 1575 | Chat.settings.addListener('active-user-list-timestamps',function(s){ Chat.showActiveUserTimestamps(s.value); }); Chat.settings.addListener('chat-only-mode',function(s){ Chat.chatOnlyMode(s.value); }); Chat.settings.addListener('edit-compact-mode',function(s){ | > > > | > > > > > > > > > > | | > > > | 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 | Chat.settings.addListener('active-user-list-timestamps',function(s){ Chat.showActiveUserTimestamps(s.value); }); Chat.settings.addListener('chat-only-mode',function(s){ Chat.chatOnlyMode(s.value); }); Chat.settings.addListener('edit-compact-mode',function(s){ if(Chat.e.inputX!==Chat.inputElement()){ /* text field/textarea mode: swap them if needed. */ const a = s.value ? [Chat.e.input1, Chat.e.inputM, 0] : [Chat.e.inputM, Chat.e.input1, 1]; const v = Chat.inputValue(); Chat.inputValue(''); D.removeClass(a[0], 'hidden'); D.addClass(a[1], 'hidden'); Chat.e.inputFields.$currentIndex = a[2]; Chat.inputValue(v); a[0].focus(); } Chat.e.inputElementWrapper.classList[ s.value ? 'add' : 'remove' ]('compact'); }); Chat.settings.addListener('edit-ctrl-send',function(s){ const label = (s.value ? "Ctrl-" : "")+"Enter submits messages."; Chat.e.inputFields.forEach((e)=>{ const v = e.dataset.placeholder0 + " " +label; if(e.isContentEditable) e.dataset.placeholder = v; else D.attr(e,'placeholder',v); }); Chat.e.btnSubmit.title = label; }); const valueKludges = { /* Convert certain string-format values to other types... */ "false": false, "true": true }; |
︙ | ︙ | |||
1605 1606 1607 1608 1609 1610 1611 | this.e.previewContent.innerHTML = t; this.e.viewPreview.querySelectorAll('a').forEach(addAnchorTargetBlank); this.inputFocus(); }; Chat.e.viewPreview.querySelector('#chat-preview-close'). addEventListener('click', ()=>Chat.setCurrentView(Chat.e.viewMessages), false); let previewPending = false; | | | 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 | this.e.previewContent.innerHTML = t; this.e.viewPreview.querySelectorAll('a').forEach(addAnchorTargetBlank); this.inputFocus(); }; Chat.e.viewPreview.querySelector('#chat-preview-close'). addEventListener('click', ()=>Chat.setCurrentView(Chat.e.viewMessages), false); let previewPending = false; const elemsToEnable = [btnPreview, Chat.e.btnSubmit, Chat.e.inputFields]; const submit = function(ev){ ev.preventDefault(); ev.stopPropagation(); if(previewPending) return false; const txt = Chat.inputValue(); if(!txt){ Chat.setPreviewText(''); |
︙ | ︙ |
Changes to src/style.chat.css.
︙ | ︙ | |||
38 39 40 41 42 43 44 | padding: 0.25em 0.5em; margin-top: 0; min-width: 9em /*avoid unsightly "underlap" with the neighboring .message-widget-tab element*/; white-space: normal; } body.chat.monospace-messages .message-widget-content, | < < | | 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | padding: 0.25em 0.5em; margin-top: 0; min-width: 9em /*avoid unsightly "underlap" with the neighboring .message-widget-tab element*/; white-space: normal; } body.chat.monospace-messages .message-widget-content, body.chat.monospace-messages .chat-input-field{ font-family: monospace; } body.chat .message-widget-content > * { margin: 0; padding: 0; } body.chat .message-widget-content > pre { |
︙ | ︙ | |||
179 180 181 182 183 184 185 | } body.chat:not(.chat-only-mode) #chat-input-area{ /* Safari user reports that 2em is necessary to keep the file selection widget from overlapping the page footer, whereas a margin of 0 is fine for FF/Chrome (and 2em is a *huge* waste of space for those). */ margin-bottom: 0; } | | | | | | | | | | | 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | } body.chat:not(.chat-only-mode) #chat-input-area{ /* Safari user reports that 2em is necessary to keep the file selection widget from overlapping the page footer, whereas a margin of 0 is fine for FF/Chrome (and 2em is a *huge* waste of space for those). */ margin-bottom: 0; } #chat-input-field-x { display: inline-block/*supposed workaround for Chrome weirdness*/; padding: 0.2em; flex: 10 1 auto; background-color: rgba(156,156,156,0.3); overflow: auto; resize: vertical; white-space: pre-wrap; /* ^^^ Firefox, when pasting plain text into a contenteditable field, loses all newlines unless we explicitly set this. Chrome does not. */ cursor: text; /* ^^^ In some browsers the cursor may not change for a contenteditable element until it has focus, causing potential confusion. */ } #chat-input-field-x:empty::before { content: attr(data-placeholder); opacity: 0.6; } .chat-input-field:not(:focus){ border-width: 1px; border-style: solid; border-radius: 0.25em; } .chat-input-field:focus{ /* This transparent border helps avoid the text shifting around when the contenteditable attribute causes a border (which we apparently cannot style) to be added. */ border-width: 1px; border-style: solid; border-color: transparent; border-radius: 0.25em; } /* Widget holding the chat message input field, send button, and settings button. */ body.chat #chat-input-line-wrapper { display: flex; flex-direction: row; align-items: stretch; flex-wrap: nowrap; } /*body.chat #chat-input-line-wrapper:not(.compact) { flex-wrap: nowrap; }*/ body.chat #chat-input-line-wrapper.compact { /* "The problem" with wrapping, together with a contenteditable input field, is that the latter grows as the user types, so causes wrapping to happen while they type, then to unwrap as soon as the input field is cleared (when the message is sent). When we stay wrapped in compact mode, the wrapped buttons simply take up too much space. */ /*flex-wrap: wrap; justify-content: flex-end;*/ flex-direction: column; /** We "really do" need column orientation here because it's the only way to eliminate the possibility that (A) the buttons get truncated in very narrow windows and (B) that they keep stable positions. */ } body.chat #chat-input-line-wrapper.compact #chat-input-field-x { } body.chat #chat-buttons-wrapper { flex: 0 1 auto; display: flex; flex-direction: column; align-items: center; min-width: 4em; min-height: 1.5em; align-self: flex-end /*keep buttons stable at bottom/right even when input field resizes */; } body.chat #chat-input-line-wrapper.compact #chat-buttons-wrapper { flex-direction: row; flex: 1 1 auto; align-self: stretch; justify-content: flex-end; /*flex-wrap: wrap;*/ /* Wrapping would be ideal except that the edit widget grows in width as the user types, moving the buttons |
︙ | ︙ | |||
283 284 285 286 287 288 289 | align-items: center; cursor: pointer; font-size: 130%; } body.chat #chat-buttons-wrapper > .cbutton:hover { background-color: rgba(200,200,200,0.3); } | | | | < | > | | < | | | | | | 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | align-items: center; cursor: pointer; font-size: 130%; } body.chat #chat-buttons-wrapper > .cbutton:hover { background-color: rgba(200,200,200,0.3); } body.chat #chat-input-line-wrapper.compact #chat-buttons-wrapper > .cbutton { margin: 2px 0.125em 0 0.125em; min-width: 6ex; max-width: 6ex; min-height: 2.3ex; max-height: 2.3ex; font-size: 120%; } body.chat #chat-input-line-wrapper.compact #chat-buttons-wrapper #chat-button-submit { min-width: 12ex; } .chat-input-field { font-family: inherit } body.chat #chat-input-line-wrapper:not(.compact) #chat-input-field-multi, body.chat #chat-input-line-wrapper:not(.compact) #chat-input-field-x { min-height: 4rem; /* Problems related to max-height: - If we do NOT set a max-height then pasting/typing a large amount of text can cause this element to grow without bounds, larger than the window, and there's no way to navigate it sensibly. In this case, manually resizing the element (desktop only - mobile doesn't offer that) will force it to stay at the selected size even if more content is added to it later. - If we DO set a max-height then its growth is bounded but it also cannot manually expanded by the user. The lesser of the two evils seems to be to rely on the browser feature that a manual resize of the element will pin its size. */ } body.chat #chat-input-line-wrapper > #chat-button-settings{ margin: 0 0 0 0.25em; max-width: 2em; } body.chat #chat-input-line-wrapper > input[type=text], body.chat #chat-input-line-wrapper > textarea { flex: 20 1 auto; max-width: revert; min-width: 20em; } body.chat #chat-input-line-wrapper.compact > input[type=text] { margin: 0 0 0.25em 0/* gap for if/when buttons wrap*/; } /* Widget holding the file selection control and preview */ body.chat #chat-input-file-area { display: flex; flex-direction: row; margin: 0; |
︙ | ︙ | |||
365 366 367 368 369 370 371 | body.chat #chat-drop-details { padding: 0 1em; white-space: pre; font-family: monospace; margin: auto; flex: 0; } | > > > | | 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | body.chat #chat-drop-details { padding: 0 1em; white-space: pre; font-family: monospace; margin: auto; flex: 0; } body.chat #chat-drop-details:empty { padding: 0; margin: 0; } body.chat #chat-drop-details img { max-width: 45%; max-height: 45%; } body.chat .chat-view { flex: 20 1 auto /*ensure that these grow more than the non-.chat-view elements. |
︙ | ︙ |