Skip to content

Instantly share code, notes, and snippets.

@pengx17
Created March 29, 2022 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pengx17/4af8e5369855202abfeb77f4fb67274b to your computer and use it in GitHub Desktop.
Save pengx17/4af8e5369855202abfeb77f4fb67274b to your computer and use it in GitHub Desktop.
auto pairing double quotes
let currentRepo = logseq.api.get_current_graph().path;
function isActive() {
return logseq.api.get_current_graph().path === currentRepo;
}
function isEditing() {
return logseq.api.check_editing();
}
function getActiveTextarea() {
if (isEditing()) {
const textarea = document.activeElement;
return textarea.nodeName === "TEXTAREA" ? textarea : undefined;
}
}
function quoteTextInTextarea(textarea, quote) {
if (isEditing()) {
const { selectionStart, selectionEnd } = textarea;
const selectedText = textarea.value.substring(selectionStart, selectionEnd);
if (selectedText) {
textarea.value =
textarea.value.substring(0, selectionStart) +
quote +
selectedText +
quote +
textarea.value.substring(selectionEnd);
textarea.selectionStart = selectionStart + 1;
textarea.selectionEnd = selectionEnd + 1;
return true;
}
}
}
window.addEventListener(
"keydown",
(e) => {
if (isActive() && e.key === '"') {
const textarea = getActiveTextarea();
if (textarea) {
const quoted = quoteTextInTextarea(textarea, '"');
if (quoted) {
e.stopPropagation();
e.preventDefault();
}
}
}
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment