From ec7c272594779b857643ed4725c514ecb8d7fcd1 Mon Sep 17 00:00:00 2001
From: Zef Hemel <zef@zef.me>
Date: Mon, 1 Aug 2022 11:04:48 +0200
Subject: [PATCH] Removed status bar, replaced with `Show: Stats` command

---
 packages/common/util.ts                | 10 ----------
 packages/plugs/core/core.plug.yaml     |  8 ++++++++
 packages/plugs/core/stats.ts           | 21 +++++++++++++++++++++
 packages/web/components/status_bar.tsx | 19 -------------------
 packages/web/editor.tsx                |  2 --
 packages/web/styles/main.scss          | 12 +-----------
 6 files changed, 30 insertions(+), 42 deletions(-)
 create mode 100644 packages/plugs/core/stats.ts
 delete mode 100644 packages/web/components/status_bar.tsx

diff --git a/packages/common/util.ts b/packages/common/util.ts
index 1e73f0d..5f8b3dd 100644
--- a/packages/common/util.ts
+++ b/packages/common/util.ts
@@ -1,13 +1,3 @@
-export function countWords(str: string): number {
-  const matches = str.match(/[\w\d\'-]+/gi);
-  return matches ? matches.length : 0;
-}
-
-export function readingTime(wordCount: number): number {
-  // 225 is average word reading speed for adults
-  return Math.ceil(wordCount / 225);
-}
-
 export function safeRun(fn: () => Promise<void>) {
   fn().catch((e) => {
     console.error(e);
diff --git a/packages/plugs/core/core.plug.yaml b/packages/plugs/core/core.plug.yaml
index 0862bde..6cf571e 100644
--- a/packages/plugs/core/core.plug.yaml
+++ b/packages/plugs/core/core.plug.yaml
@@ -342,3 +342,11 @@ functions:
     path: ./link.ts:titleUnfurl
     events:
       - unfurl:title-unfurl
+
+  # Random stuff
+  statsCommand:
+    path: ./stats.ts:statsCommand
+    command:
+      name: "Stats: Show"
+      key: "Ctrl-Shift-s"
+      mac: "Cmd-Shift-s"
diff --git a/packages/plugs/core/stats.ts b/packages/plugs/core/stats.ts
new file mode 100644
index 0000000..737b84a
--- /dev/null
+++ b/packages/plugs/core/stats.ts
@@ -0,0 +1,21 @@
+import {
+  flashNotification,
+  getText,
+} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
+
+function countWords(str: string): number {
+  const matches = str.match(/[\w\d\'-]+/gi);
+  return matches ? matches.length : 0;
+}
+
+function readingTime(wordCount: number): number {
+  // 225 is average word reading speed for adults
+  return Math.ceil(wordCount / 225);
+}
+
+export async function statsCommand() {
+  const text = await getText();
+  const wordCount = countWords(text);
+  const time = readingTime(wordCount);
+  await flashNotification(`${wordCount} words, ${time} minutes to read.`);
+}
diff --git a/packages/web/components/status_bar.tsx b/packages/web/components/status_bar.tsx
deleted file mode 100644
index d9a14cb..0000000
--- a/packages/web/components/status_bar.tsx
+++ /dev/null
@@ -1,19 +0,0 @@
-import { EditorView } from "@codemirror/view";
-import * as util from "../../common/util";
-
-export function StatusBar({ editorView }: { editorView?: EditorView }) {
-  let wordCount = 0,
-    readingTime = 0;
-  if (editorView) {
-    let text = editorView.state.sliceDoc();
-    wordCount = util.countWords(text);
-    readingTime = util.readingTime(wordCount);
-  }
-  return (
-    <div id="sb-status-bar">
-      <div className="inner">
-        {wordCount} words | {readingTime} min
-      </div>
-    </div>
-  );
-}
diff --git a/packages/web/editor.tsx b/packages/web/editor.tsx
index f931d20..3312ebd 100644
--- a/packages/web/editor.tsx
+++ b/packages/web/editor.tsx
@@ -50,7 +50,6 @@ import { SlashCommandHook } from "./hooks/slash_command";
 import { pasteLinkExtension } from "./editor_paste";
 import { markdownSyscalls } from "@silverbulletmd/common/syscalls/markdown";
 import { clientStoreSyscalls } from "./syscalls/clientStore";
-import { StatusBar } from "./components/status_bar";
 import {
   loadMarkdownExtensions,
   MDExt,
@@ -758,7 +757,6 @@ export class Editor {
             />
           </div>
         )}
-        <StatusBar editorView={editor.editorView} />
       </>
     );
   }
diff --git a/packages/web/styles/main.scss b/packages/web/styles/main.scss
index c0e7d45..eed9627 100644
--- a/packages/web/styles/main.scss
+++ b/packages/web/styles/main.scss
@@ -67,7 +67,7 @@
       .notifications {
         position: absolute;
         font-family: "iA-Mono";
-        bottom: 45px;
+        bottom: 0;
         left: 5px;
         right: 5px;
         font-size: 15px;
@@ -171,13 +171,3 @@
     }
   }
 }
-
-#sb-status-bar {
-  height: 40px;
-  line-height: 40px;
-  padding: 0 10px;
-  text-align: right;
-  background-color: rgb(213, 213, 213);
-  border-top: rgb(193, 193, 193) 1px solid;
-  font-family: "iA-Mono";
-}