Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: add support for +/- font-size using shortcut keys (#4029)

Muhammad Ali0de463526f2c
diff --git a/packages/editor/src/extensions/font-size/font-size.ts b/packages/editor/src/extensions/font-size/font-size.ts
index 92ffb5170..bbf73cbd0 100644
--- a/packages/editor/src/extensions/font-size/font-size.ts
+++ b/packages/editor/src/extensions/font-size/font-size.ts
@@ -17,8 +17,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { Extension } from "@tiptap/core";
-import "@tiptap/extension-text-style";
+import { useToolbarStore } from "../../toolbar/stores/toolbar-store";
+import { Editor, Extension } from "@tiptap/core";
type FontSizeOptions = {
types: string[];
@@ -83,5 +83,33 @@ export const FontSize = Extension.create<FontSizeOptions>({
.run();
}
};
+ },
+ addKeyboardShortcuts() {
+ return {
+ "ctrl-]": ({ editor }) => {
+ editor
+ .chain()
+ .focus()
+ .setFontSize(`${Math.min(120, getFontSize(editor) + 1)}px`)
+ .run();
+ return true;
+ },
+ "Ctrl-[": ({ editor }) => {
+ editor
+ .chain()
+ .focus()
+ .setFontSize(`${Math.max(8, getFontSize(editor) - 1)}px`)
+ .run();
+ return true;
+ }
+ };
}
});
+
+function getFontSize(editor: Editor) {
+ const defaultFontSize = useToolbarStore.getState().fontSize;
+ const { fontSize } = editor.getAttributes("textStyle");
+ return fontSize
+ ? parseInt(fontSize.replace("px", "")) || 16
+ : defaultFontSize;
+}