From 0036da2b5e8838c784c4e602b1d70542bb620fe6 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 15 Nov 2020 21:37:49 -0500 Subject: [PATCH] automatically bracket inline textile formatting when required (fixes derpibooru/philomena#225, furbooru/philomena#49) --- assets/js/textiletoolbar.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/assets/js/textiletoolbar.js b/assets/js/textiletoolbar.js index ebd97085..75e5b5c2 100644 --- a/assets/js/textiletoolbar.js +++ b/assets/js/textiletoolbar.js @@ -8,15 +8,15 @@ import { $, $$ } from './utils/dom'; const textileSyntax = { bold: { action: wrapSelection, - options: { prefix: '*', suffix: '*', shortcutKey: 'b' } + options: { prefix: '*', suffix: '*', shortcutKey: 'b', type: 'inline' } }, italics: { action: wrapSelection, - options: { prefix: '_', suffix: '_', shortcutKey: 'i' } + options: { prefix: '_', suffix: '_', shortcutKey: 'i', type: 'inline' } }, under: { action: wrapSelection, - options: { prefix: '+', suffix: '+', shortcutKey: 'u' } + options: { prefix: '+', suffix: '+', shortcutKey: 'u', type: 'inline' } }, spoiler: { action: wrapSelection, @@ -24,19 +24,19 @@ const textileSyntax = { }, code: { action: wrapSelection, - options: { prefix: '@', suffix: '@', shortcutKey: 'e' } + options: { prefix: '@', suffix: '@', shortcutKey: 'e', type: 'inline' } }, strike: { action: wrapSelection, - options: { prefix: '-', suffix: '-' } + options: { prefix: '-', suffix: '-', type: 'inline' } }, superscript: { action: wrapSelection, - options: { prefix: '^', suffix: '^' } + options: { prefix: '^', suffix: '^', type: 'inline' } }, subscript: { action: wrapSelection, - options: { prefix: '~', suffix: '~' } + options: { prefix: '~', suffix: '~', type: 'inline' } }, quote: { action: wrapSelection, @@ -85,7 +85,7 @@ function getSelections(textarea) { function wrapSelection(textarea, options) { const { selectedText, beforeSelection, afterSelection } = getSelections(textarea), - { text = selectedText, prefix = '', suffix = '' } = options, + { text = selectedText, prefix = '', suffix = '', type } = options, // For long comments, record scrollbar position to restore it later scrollTop = textarea.scrollTop, emptyText = text === ''; @@ -97,7 +97,12 @@ function wrapSelection(textarea, options) { }); } - textarea.value = beforeSelection + prefix + newText + suffix + afterSelection; + if (type === 'inline' && newText.includes('\n')) { + textarea.value = `${beforeSelection}[${prefix}${newText}${suffix}]${afterSelection}`; + } + else { + textarea.value = `${beforeSelection}${prefix}${newText}${suffix}${afterSelection}`; + } // If no text were highlighted, place the caret inside // the formatted section, otherwise place it at the end