Add editorCheckCallback to all commands and remove empty onunload
- Add editorCheckCallback to hide commands when no Markdown editor is active - Remove no-op onunload override (base Plugin class provides default)
This commit is contained in:
+12
-1
@@ -1,10 +1,15 @@
|
|||||||
import { Plugin } from "obsidian";
|
import { Plugin, MarkdownView } from "obsidian";
|
||||||
import { transformText, cleanText, bulletToEmdash, bulletToArrow, numberedListSlash, numberedListParens, markdownToLinkedIn, FormatStyle } from "./formatter";
|
import { transformText, cleanText, bulletToEmdash, bulletToArrow, numberedListSlash, numberedListParens, markdownToLinkedIn, FormatStyle } from "./formatter";
|
||||||
|
|
||||||
|
function editorCheck(plugin: Plugin) {
|
||||||
|
return (_checking: boolean) => plugin.app.workspace.getActiveViewOfType(MarkdownView) !== null;
|
||||||
|
}
|
||||||
|
|
||||||
function addFormatCommand(plugin: Plugin, style: FormatStyle, name: string) {
|
function addFormatCommand(plugin: Plugin, style: FormatStyle, name: string) {
|
||||||
plugin.addCommand({
|
plugin.addCommand({
|
||||||
id: `unicode-formatter:${style}`,
|
id: `unicode-formatter:${style}`,
|
||||||
name,
|
name,
|
||||||
|
editorCheckCallback: editorCheck(plugin),
|
||||||
editorCallback: (editor) => {
|
editorCallback: (editor) => {
|
||||||
const selection = editor.getSelection();
|
const selection = editor.getSelection();
|
||||||
if (selection) {
|
if (selection) {
|
||||||
@@ -21,6 +26,7 @@ export function registerCommands(plugin: Plugin): void {
|
|||||||
plugin.addCommand({
|
plugin.addCommand({
|
||||||
id: "unicode-formatter:clean",
|
id: "unicode-formatter:clean",
|
||||||
name: "Remove Unicode Formatting",
|
name: "Remove Unicode Formatting",
|
||||||
|
editorCheckCallback: editorCheck(plugin),
|
||||||
editorCallback: (editor) => {
|
editorCallback: (editor) => {
|
||||||
const selection = editor.getSelection();
|
const selection = editor.getSelection();
|
||||||
if (selection) {
|
if (selection) {
|
||||||
@@ -31,6 +37,7 @@ export function registerCommands(plugin: Plugin): void {
|
|||||||
plugin.addCommand({
|
plugin.addCommand({
|
||||||
id: "unicode-formatter:bullet-to-arrow",
|
id: "unicode-formatter:bullet-to-arrow",
|
||||||
name: "Convert List Bullets to Arrow",
|
name: "Convert List Bullets to Arrow",
|
||||||
|
editorCheckCallback: editorCheck(plugin),
|
||||||
editorCallback: (editor) => {
|
editorCallback: (editor) => {
|
||||||
const selection = editor.getSelection();
|
const selection = editor.getSelection();
|
||||||
if (selection) {
|
if (selection) {
|
||||||
@@ -41,6 +48,7 @@ export function registerCommands(plugin: Plugin): void {
|
|||||||
plugin.addCommand({
|
plugin.addCommand({
|
||||||
id: "unicode-formatter:bullet-to-emdash",
|
id: "unicode-formatter:bullet-to-emdash",
|
||||||
name: "Convert List Bullets to Em Dash",
|
name: "Convert List Bullets to Em Dash",
|
||||||
|
editorCheckCallback: editorCheck(plugin),
|
||||||
editorCallback: (editor) => {
|
editorCallback: (editor) => {
|
||||||
const selection = editor.getSelection();
|
const selection = editor.getSelection();
|
||||||
if (selection) {
|
if (selection) {
|
||||||
@@ -51,6 +59,7 @@ export function registerCommands(plugin: Plugin): void {
|
|||||||
plugin.addCommand({
|
plugin.addCommand({
|
||||||
id: "unicode-formatter:numbered-list-slash",
|
id: "unicode-formatter:numbered-list-slash",
|
||||||
name: "Convert List to Numbered (Slash)",
|
name: "Convert List to Numbered (Slash)",
|
||||||
|
editorCheckCallback: editorCheck(plugin),
|
||||||
editorCallback: (editor) => {
|
editorCallback: (editor) => {
|
||||||
const selection = editor.getSelection();
|
const selection = editor.getSelection();
|
||||||
if (selection) {
|
if (selection) {
|
||||||
@@ -61,6 +70,7 @@ export function registerCommands(plugin: Plugin): void {
|
|||||||
plugin.addCommand({
|
plugin.addCommand({
|
||||||
id: "unicode-formatter:numbered-list-parens",
|
id: "unicode-formatter:numbered-list-parens",
|
||||||
name: "Convert List to Numbered (Parentheses)",
|
name: "Convert List to Numbered (Parentheses)",
|
||||||
|
editorCheckCallback: editorCheck(plugin),
|
||||||
editorCallback: (editor) => {
|
editorCallback: (editor) => {
|
||||||
const selection = editor.getSelection();
|
const selection = editor.getSelection();
|
||||||
if (selection) {
|
if (selection) {
|
||||||
@@ -71,6 +81,7 @@ export function registerCommands(plugin: Plugin): void {
|
|||||||
plugin.addCommand({
|
plugin.addCommand({
|
||||||
id: "unicode-formatter:markdown-to-linkedin",
|
id: "unicode-formatter:markdown-to-linkedin",
|
||||||
name: "Convert Markdown to Post Format",
|
name: "Convert Markdown to Post Format",
|
||||||
|
editorCheckCallback: editorCheck(plugin),
|
||||||
editorCallback: (editor) => {
|
editorCallback: (editor) => {
|
||||||
const selection = editor.getSelection();
|
const selection = editor.getSelection();
|
||||||
if (selection) {
|
if (selection) {
|
||||||
|
|||||||
@@ -5,7 +5,4 @@ export default class UnicodeFormatterPlugin extends Plugin {
|
|||||||
async onload() {
|
async onload() {
|
||||||
registerCommands(this);
|
registerCommands(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
onunload() {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user