Moved bold and italics to plug, new file: plug protocol
This commit is contained in:
@@ -214,6 +214,12 @@ function $11a7e2bff790f35a$export$c72d34660a162238() {
|
||||
function $11a7e2bff790f35a$export$da3f040fb23d21f() {
|
||||
return $4ba3510c824e3aea$export$c5be9092dbf465c("editor.getCursor");
|
||||
}
|
||||
function $11a7e2bff790f35a$export$ca798a7e6e94638c() {
|
||||
return $4ba3510c824e3aea$export$c5be9092dbf465c("editor.getSelection");
|
||||
}
|
||||
function $11a7e2bff790f35a$export$f6e36f80a8190133(from, to) {
|
||||
return $4ba3510c824e3aea$export$c5be9092dbf465c("editor.setSelection", from, to);
|
||||
}
|
||||
function $11a7e2bff790f35a$export$a1544dad697b423d() {
|
||||
return $4ba3510c824e3aea$export$c5be9092dbf465c("editor.save");
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -132,3 +132,22 @@ functions:
|
||||
path: ./template.ts:instantiateTemplateCommand
|
||||
command:
|
||||
name: "Template: Instantiate for Page"
|
||||
|
||||
quoteSelection:
|
||||
path: ./text.ts:quoteSelection
|
||||
command:
|
||||
name: "Text: Quote Selection"
|
||||
key: "Ctrl->"
|
||||
mac: "Cmd-Shift-."
|
||||
bold:
|
||||
path: ./text.ts:boldCommand
|
||||
command:
|
||||
name: "Text: Bold"
|
||||
key: "Ctrl-b"
|
||||
mac: "Cmd-b"
|
||||
italic:
|
||||
path: ./text.ts:italicCommand
|
||||
command:
|
||||
name: "Text: Italic"
|
||||
key: "Ctrl-i"
|
||||
mac: "Cmd-i"
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import {
|
||||
getSelection,
|
||||
getText,
|
||||
insertAtCursor,
|
||||
moveCursor,
|
||||
replaceRange,
|
||||
setSelection,
|
||||
} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
||||
|
||||
export async function quoteSelection() {
|
||||
let text = await getText();
|
||||
const selection = await getSelection();
|
||||
let from = selection.from;
|
||||
while (from >= 0 && text[from] !== "\n") {
|
||||
from--;
|
||||
}
|
||||
from++;
|
||||
if (text[from] === ">" && text[from + 1] === " ") {
|
||||
// Already quoted, we have to unquote
|
||||
text = text.slice(from + 2, selection.to);
|
||||
text = text.replaceAll("\n> ", "\n");
|
||||
} else {
|
||||
text = text.slice(from, selection.to);
|
||||
text = `> ${text.replaceAll("\n", "\n> ")}`;
|
||||
}
|
||||
await replaceRange(from, selection.to, text);
|
||||
}
|
||||
|
||||
export function boldCommand() {
|
||||
return insertMarker("**");
|
||||
}
|
||||
|
||||
export function italicCommand() {
|
||||
return insertMarker("_");
|
||||
}
|
||||
|
||||
async function insertMarker(marker: string) {
|
||||
let text = await getText();
|
||||
const selection = await getSelection();
|
||||
if (selection.from === selection.to) {
|
||||
// empty selection
|
||||
if (markerAt(selection.from)) {
|
||||
// Already there, skipping ahead
|
||||
await moveCursor(selection.from + marker.length);
|
||||
} else {
|
||||
// Not there, inserting
|
||||
await insertAtCursor(marker + marker);
|
||||
await moveCursor(selection.from + marker.length);
|
||||
}
|
||||
} else {
|
||||
let from = selection.from;
|
||||
let to = selection.to;
|
||||
let hasMarker = markerAt(from);
|
||||
if (!markerAt(from)) {
|
||||
// Maybe just before the cursor? We'll accept that
|
||||
from = selection.from - marker.length;
|
||||
to = selection.to + marker.length;
|
||||
hasMarker = markerAt(from);
|
||||
}
|
||||
|
||||
if (!hasMarker) {
|
||||
// Adding
|
||||
await replaceRange(
|
||||
selection.from,
|
||||
selection.to,
|
||||
marker + text.slice(selection.from, selection.to) + marker
|
||||
);
|
||||
await setSelection(
|
||||
selection.from + marker.length,
|
||||
selection.to + marker.length
|
||||
);
|
||||
} else {
|
||||
// Removing
|
||||
await replaceRange(
|
||||
from,
|
||||
to,
|
||||
text.substring(from + marker.length, to - marker.length)
|
||||
);
|
||||
await setSelection(from, to - marker.length * 2);
|
||||
}
|
||||
}
|
||||
|
||||
function markerAt(pos: number) {
|
||||
for (var i = 0; i < marker.length; i++) {
|
||||
if (text[pos + i] !== marker[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user