Skip to content

Commit

Permalink
Added markdown preview
Browse files Browse the repository at this point in the history
  • Loading branch information
notangelmario committed Oct 16, 2022
1 parent 216e014 commit 771838e
Show file tree
Hide file tree
Showing 21 changed files with 55 additions and 75 deletions.
4 changes: 2 additions & 2 deletions src/pages/index.html → index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#1e1e1e">
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#fffffe">
<link rel="stylesheet" href="../app.css"/>
<link rel="stylesheet" href="./src/app.css"/>
<title>Miniated</title>
<meta name="description" content="Miniated is a minimalist web editor based on Monaco Editor."/>
</head>
Expand All @@ -31,6 +31,6 @@ <h1>
</p>
</div>
</div>
<script type="module" src="../main.ts"></script>
<script type="module" src="./src/main.ts"></script>
</body>
</html>
13 changes: 5 additions & 8 deletions src/pages/markdown-preview.html → markdown-preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#000000">
<meta name="msapplication-TileColor" content="#ffffff">
<!-- <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#1e1e1e"> -->
<!-- <meta name="theme-color" media="(prefers-color-scheme: light)" content="#fffffe"> -->
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#1e1e1e">
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#fffffe">
<!-- <link rel="stylesheet" href="./src/app.css"/> -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet/dist/gfm.min.css"/>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet/dist/gfm.min.css" />
<title>Miniated &middot; Markdown Preview</title>
<meta name="description" content="Miniated is a minimalist web editor based on Monaco Editor."/>
</head>
<body>
<main>

</main>
<script type="module" src="../markdown_preview.ts"></script>
<main></main>
<script type="module" src="./src/markdown_preview.ts"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "miniated",
"private": true,
"author": "Savin Angel-Mario ([email protected])",
"version": "1.1.4",
"version": "1.2.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
9 changes: 1 addition & 8 deletions src/lib/compat.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@

export function isCompat() {
//@ts-ignore
// if (!('launchQueue' in window && 'files' in LaunchParams.prototype)) return false;
if (!("showOpenFilePicker" in window)) return false;
if (isMobile()) return false;


return true;
}
export const isCompat = !isMobile() && ("showOpenFilePicker" in window)

export function isMobile() {
let check = false;
Expand Down
52 changes: 5 additions & 47 deletions src/lib/editor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import monaco from "./monaco";
import { onClose, onOpen, onSave, initDropFile, initLaunchWithFile, onCreate, startAutosave } from "./file";
import { Store } from "./store";
import { addMarkdownActions } from "./languages/markdown";

// Monaco editor doesn't have an API to change default keybindings
// so we have to tap into internal api to change default keybindings
Expand Down Expand Up @@ -62,13 +63,13 @@ export function disableBrowserKeybindings() {

function addActions(editor: monaco.editor.IStandaloneCodeEditor, store: Store) {
let disableAutosave: () => void = () => null;
let previewWindow: Window | null = null;
let previewWatcher: number | null = null;
const fileAvailableContext = editor.createContextKey<boolean>("fileAvailable", false);

initDropFile(document.body, editor, store, fileAvailableContext, disableAutosave);
initLaunchWithFile(editor, store, fileAvailableContext, disableAutosave);

addMarkdownActions(editor);

editor.addAction({
id: "miniated.open_file",
label: "Open File...",
Expand Down Expand Up @@ -115,26 +116,7 @@ function addActions(editor: monaco.editor.IStandaloneCodeEditor, store: Store) {
},
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS]
});
editor.addAction({
id: "miniated.export_markdown_html",
precondition: "fileAvailable",
label: "Export Markdown File to HTML...",
run: async () => {
const { exportToHtml } = await import("./languages/markdown");

exportToHtml(editor);
}
});
editor.addAction({
id: "miniated.export_markdown_html",
precondition: "fileAvailable",
label: "Export Markdown File to HTML...",
run: async () => {
const { exportToHtml } = await import("./languages/markdown");

exportToHtml(editor);
}
});

editor.addAction({
id: "miniated.close_file",
label: "Close File",
Expand All @@ -145,31 +127,7 @@ function addActions(editor: monaco.editor.IStandaloneCodeEditor, store: Store) {
},
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyQ]
});
editor.addAction({
id: "miniated.open_markdown_preview",
label: "Open Markdown Preview",
// precondition: "fileAvailable",
run: async () => {
const { openPreviewWindow, startPreviewWatcher } = await import("./languages/markdown");

previewWindow = openPreviewWindow(editor);
if (previewWindow) {
previewWatcher = startPreviewWatcher(editor, previewWindow);
}
}
});
editor.addAction({
id: "miniated.close_markdown_preview",
label: "Close Markdown Preview",
// precondition: "fileAvailable",
run: async () => {
const { stopPreviewWatcher } = await import("./languages/markdown");

if (previewWatcher && previewWindow) {
stopPreviewWatcher(previewWatcher, previewWindow);
}
}
});

editor.addAction({
id: "miniated.open_repo",
label: "Show Miniated on GitHub",
Expand Down
34 changes: 33 additions & 1 deletion src/lib/languages/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,33 @@ import { createNotice } from "../status";
import monaco from "../monaco";
import { writeFile } from "../file";

export function addMarkdownActions(editor: monaco.editor.IStandaloneCodeEditor) {
editor.addAction({
id: "miniated.export_markdown_html",
precondition: "fileAvailable",
label: "Export Markdown File to HTML...",
run: async () => {
exportToHtml(editor);
}
});
editor.addAction({
id: "miniated.open_markdown_preview",
label: "Open Markdown Preview",
precondition: "fileAvailable",
run: async () => {
const previewWindow = openPreviewWindow(editor);

if (previewWindow) {
startPreviewWatcher(editor, previewWindow);
} else {
createNotice("Could not open preview window.");
}
}
});
}

export function openPreviewWindow(editor: monaco.editor.IStandaloneCodeEditor) {
const preview = window.open("markdown-preview.html", "_blank");
const preview = window.open("/markdown-preview.html", "blank", "width=800,height=600");

if (preview) {
preview.postMessage(editor.getValue());
Expand All @@ -16,6 +41,13 @@ export function openPreviewWindow(editor: monaco.editor.IStandaloneCodeEditor) {
export function startPreviewWatcher(editor: monaco.editor.IStandaloneCodeEditor, previewWindow: Window) {
const interval = setInterval(() => {

if (previewWindow.closed) {
stopPreviewWatcher(interval, previewWindow);
console.log("Stopped watcher!");
return;
}

console.log("Working...");
previewWindow.postMessage(editor.getValue())
}, 2000);

Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isCompat } from "./lib/compat";

const editorWrapper = document.getElementById("app")!;

if (isCompat()) {
if (isCompat) {
const store = initStore();
initLanguageWorkers();
const editor = initEditor(editorWrapper, store);
Expand Down
9 changes: 5 additions & 4 deletions src/markdown_preview.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { convertToMarkdown } from "./lib/languages/markdown";

const origin = window.location.origin;
const mainWrapper = document.querySelector("main")!;
const origin = window.location.origin;
let currentEditorValue = "";

window.addEventListener("message", async (ev) => {

window.addEventListener("message", async ev => {
if (ev.origin !== origin) return;
if (ev.data === currentEditorValue) return;

currentEditorValue = ev.data;
const markdown = await convertToMarkdown(ev.data);

mainWrapper.innerHTML = markdown;
const markdown = await convertToMarkdown(ev.data);
mainWrapper.innerHTML = markdown;
})
5 changes: 2 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { VitePWA } from "vite-plugin-pwa";
// https://vitejs.dev/config/
export default defineConfig({
appType: "mpa",
root: resolve(__dirname, "src"),
plugins: [
VitePWA({
devOptions: {
Expand All @@ -27,8 +26,8 @@ export default defineConfig({
emptyOutDir: true,
rollupOptions: {
input: {
main: resolve(__dirname, "src", "pages", "index.html"),
"markdown-preview": resolve(__dirname, "src", "pages", "markdown-preview.html"),
main: resolve(__dirname, "index.html"),
"markdown-preview": resolve(__dirname, "markdown-preview.html"),
},
output: {
manualChunks: {
Expand Down

0 comments on commit 771838e

Please sign in to comment.