Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textarea: command to toggle wrapping #5837

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions panel/src/components/Forms/Input/TextareaInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ export default {
document.execCommand("insertText", false, text);

if (input.value === current) {
input.setRangeText(text, input.selectionStart, input.selectionEnd);
const start = input.selectionStart;
const end = input.selectionEnd;
const mode = start === end ? "end" : "select";
input.setRangeText(text, start, end, mode);
}

this.$emit("input", input.value);
Expand Down Expand Up @@ -295,11 +298,25 @@ export default {
this.$refs.input.selectionEnd
);
},
toggle(before, after) {
after = after ?? before;
const selection = this.selection();

if (selection.startsWith(before) && selection.endsWith(after)) {
return this.insert(
selection
.slice(before.length)
.slice(0, selection.length - before.length - after.length)
);
}

return this.wrap(before, after);
},
upload() {
this.$panel.upload.pick(this.uploadOptions);
},
wrap(text) {
this.insert(text + this.selection() + text);
wrap(before, after) {
this.insert(before + this.selection() + (after ?? before));
}
},
validations() {
Expand Down
7 changes: 4 additions & 3 deletions panel/src/components/Forms/Toolbar/TextareaToolbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ export default {
bold: {
label: this.$t("toolbar.button.bold"),
icon: "bold",
click: () => this.command("wrap", "**"),
click: () => this.command("toggle", "**"),
shortcut: "b"
},
italic: {
label: this.$t("toolbar.button.italic"),
icon: "italic",
click: () => this.command("wrap", "*"),
click: () => this.command("toggle", "*"),
shortcut: "i"
},
link: {
Expand Down Expand Up @@ -94,7 +94,7 @@ export default {
code: {
label: this.$t("toolbar.button.code"),
icon: "code",
click: () => this.command("wrap", "`")
click: () => this.command("toggle", "`")
},
ul: {
label: this.$t("toolbar.button.ul"),
Expand Down Expand Up @@ -177,6 +177,7 @@ export default {
* - `dialog` opens a dialog component
* - `insert` inserts the given text at the current selection
* - `prepend` prepends the given text to the current selection
* - `toggle` toggles wrapping of current selection (accepts before, after texts)
* - `upload` opens the file upload dialog
* - `wrap` wraps the current selection with the given text
*/
Expand Down