mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 01:06:35 -04:00
Improve behavior of paragraph command and add tooltip
This commit is contained in:
parent
893028b2df
commit
dcb6a11053
4 changed files with 22 additions and 7 deletions
|
@ -29,6 +29,7 @@ editing-mathjax-inline = MathJax inline
|
||||||
editing-media = Media
|
editing-media = Media
|
||||||
editing-ordered-list = Ordered list
|
editing-ordered-list = Ordered list
|
||||||
editing-outdent = Decrease indent
|
editing-outdent = Decrease indent
|
||||||
|
editing-paragraph = Paragraph
|
||||||
editing-paste = Paste
|
editing-paste = Paste
|
||||||
editing-record-audio-f5 = Record audio (F5)
|
editing-record-audio-f5 = Record audio (F5)
|
||||||
editing-remove-formatting-ctrlandr = Remove formatting (Ctrl+R)
|
editing-remove-formatting-ctrlandr = Remove formatting (Ctrl+R)
|
||||||
|
|
|
@ -63,6 +63,18 @@ const indentListItem = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const toggleParagraph = (): void => {
|
||||||
|
const currentField = document.activeElement as EditingArea;
|
||||||
|
const paragraph = getParagraph(currentField.shadowRoot!);
|
||||||
|
|
||||||
|
if (!paragraph) {
|
||||||
|
document.execCommand("formatBlock", false, "p");
|
||||||
|
} else {
|
||||||
|
paragraph.insertAdjacentElement("beforeend", document.createElement("br"));
|
||||||
|
paragraph.replaceWith(...paragraph.childNodes);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const checkForParagraph = (): boolean => {
|
const checkForParagraph = (): boolean => {
|
||||||
const currentField = document.activeElement as EditingArea;
|
const currentField = document.activeElement as EditingArea;
|
||||||
return Boolean(getParagraph(currentField.shadowRoot!));
|
return Boolean(getParagraph(currentField.shadowRoot!));
|
||||||
|
@ -134,11 +146,9 @@ export function getFormatBlockGroup(): DynamicSvelteComponent<typeof ButtonGroup
|
||||||
const paragraphButton = commandIconButton({
|
const paragraphButton = commandIconButton({
|
||||||
icon: paragraphIcon,
|
icon: paragraphIcon,
|
||||||
command: "formatBlock",
|
command: "formatBlock",
|
||||||
onClick: () => {
|
onClick: toggleParagraph,
|
||||||
document.execCommand("formatBlock", false, "p");
|
|
||||||
},
|
|
||||||
onUpdate: checkForParagraph,
|
onUpdate: checkForParagraph,
|
||||||
tooltip: tr.editingUnorderedList(),
|
tooltip: tr.editingParagraph(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const ulButton = commandIconButton({
|
const ulButton = commandIconButton({
|
||||||
|
|
|
@ -102,6 +102,11 @@ const isListItem = (element: Element): element is HTMLLIElement =>
|
||||||
window.getComputedStyle(element).display === "list-item";
|
window.getComputedStyle(element).display === "list-item";
|
||||||
const isParagraph = (element: Element): element is HTMLParamElement =>
|
const isParagraph = (element: Element): element is HTMLParamElement =>
|
||||||
element.tagName === "P";
|
element.tagName === "P";
|
||||||
|
const isBlockElement = (
|
||||||
|
element: Element
|
||||||
|
): element is HTMLLIElement & HTMLParamElement =>
|
||||||
|
isListItem(element) || isBlockElement(element);
|
||||||
|
|
||||||
export const getListItem = getAnchorParent(isListItem);
|
export const getListItem = getAnchorParent(isListItem);
|
||||||
export const getParagraph = getAnchorParent(isParagraph);
|
export const getParagraph = getAnchorParent(isParagraph);
|
||||||
|
export const getBlockElement = getAnchorParent(isBlockElement);
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
import { updateActiveButtons } from "editor-toolbar";
|
import { updateActiveButtons } from "editor-toolbar";
|
||||||
import { EditingArea } from "./editingArea";
|
import { EditingArea } from "./editingArea";
|
||||||
import { caretToEnd, nodeIsElement, getListItem, getParagraph } from "./helpers";
|
import { caretToEnd, nodeIsElement, getBlockElement } from "./helpers";
|
||||||
import { triggerChangeTimer } from "./changeTimer";
|
import { triggerChangeTimer } from "./changeTimer";
|
||||||
|
|
||||||
export function onInput(event: Event): void {
|
export function onInput(event: Event): void {
|
||||||
|
@ -24,8 +24,7 @@ export function onKey(evt: KeyboardEvent): void {
|
||||||
// prefer <br> instead of <div></div>
|
// prefer <br> instead of <div></div>
|
||||||
if (
|
if (
|
||||||
evt.code === "Enter" &&
|
evt.code === "Enter" &&
|
||||||
!getListItem(currentField.shadowRoot!) &&
|
!getBlockElement(currentField.shadowRoot!) !== evt.shiftKey
|
||||||
!getParagraph(currentField.shadowRoot!)
|
|
||||||
) {
|
) {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
document.execCommand("insertLineBreak");
|
document.execCommand("insertLineBreak");
|
||||||
|
|
Loading…
Reference in a new issue