import { Editor } from "@toast-ui/vue-editor";
import { Viewer } from "@toast-ui/vue-editor";
-import codeSyntaxHighlight from "@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight-all.js";
-
import api from "../api";
import * as constants from "../constants";
import { Note, SearchResult } from "./classes";
},
data: function() {
- return {
- views: {
- login: 0,
- home: 1,
- note: 2,
- search: 3,
- },
- currentView: 1,
- usernameInput: null,
- passwordInput: null,
- rememberMeInput: false,
- notes: null,
- searchTerm: null,
- draftSaveTimeout: null,
- searchResults: null,
- currentNote: null,
- titleInput: null,
- initialContent: null,
- editMode: false,
- viewerOptions: { plugins: [codeSyntaxHighlight] },
- editorOptions: { plugins: [codeSyntaxHighlight] },
- };
+ return constants.dataDefaults();
},
computed: {
this.updateDocumentTitle();
},
+ navigate: function(href, e) {
+ if (e != undefined && e.ctrlKey == true) {
+ window.open(href);
+ } else {
+ history.pushState(null, "", href);
+ this.resetData();
+ this.route();
+ }
+ },
+
+ resetData: function() {
+ Object.assign(this.$data, constants.dataDefaults());
+ },
+
updateDocumentTitle: function() {
let pageTitleSuffix = null;
if (this.currentView == this.views.login) {
localStorage.setItem("token", response.data.access_token);
}
let redirectPath = helpers.getSearchParam(constants.params.redirect);
- window.open(redirectPath || "/", "_self");
+ parent.navigate(redirectPath || "/");
})
.finally(function() {
parent.usernameInput = null;
logout: function() {
sessionStorage.removeItem("token");
localStorage.removeItem("token");
- window.open(`/${constants.basePaths.login}`, "_self");
+ this.navigate(`/${constants.basePaths.login}`);
},
getNotes: function() {
},
search: function() {
- window.open(
+ this.navigate(
`/${constants.basePaths.search}?${
constants.params.searchTerm
- }=${encodeURI(this.searchTerm)}`,
- "_self"
+ }=${encodeURI(this.searchTerm)}`
);
},
toggleEditMode: function() {
// To Edit Mode
if (this.editMode == false) {
- this.titleInput = this.currentNote.title;
+ this.titleInput = this.currentNote.title || "New Note";
let draftContent = localStorage.getItem(this.currentNote.filename);
// Draft
if (draftContent && confirm("Do you want to resume the saved draft?")) {
) {
api
.delete(`/api/notes/${this.currentNote.filename}`)
- .then(window.open("/", "_self"));
+ .then(this.navigate("/"));
}
},
},
created: function() {
- EventBus.$on("logout", this.logout);
+ EventBus.$on("navigate", this.navigate);
document.addEventListener("keydown", this.keyboardShortcuts);
let token = localStorage.getItem("token");
this.route();
},
+
+ mounted: function() {
+ window.addEventListener("popstate", this.route);
+ },
};
<div>
<!-- Header -->
<div class="mt-4 mb-4">
- <h1 class="h1 clickable-link text-center"><a href="/">flatnotes</a></h1>
+ <h1 class="h1 clickable-link text-center">
+ <a href="/" @click.prevent="navigate('/', $event)">flatnotes</a>
+ </h1>
</div>
<!-- Login -->
</button>
<!-- Close -->
- <a href="/">
+ <a href="/" @click.prevent="navigate('/')">
<button
v-if="currentView == 2 && editMode == false"
type="button"
<!-- Note Loaded -->
<div v-else>
- <h2 v-if="editMode == false" class="mb-4">{{currentNote.title}}</h2>
- <input v-else type="text" class="h2 title-input" v-model="titleInput" />
+ <h2 v-if="editMode == false" class="mb-4">{{ currentNote.title }}</h2>
+ <input
+ v-else
+ type="text"
+ class="h2 title-input"
+ v-model="titleInput"
+ />
<!-- Viewer -->
<div class="mb-4 note">
class="mb-5"
>
<p class="h5 text-center clickable-link">
- <a v-html="result.titleHighlightsOrTitle" :href="result.href"></a>
+ <a
+ v-html="result.titleHighlightsOrTitle"
+ :href="result.href"
+ @click.prevent="navigate(result.href, $event)"
+ ></a>
</p>
<p
class="text-center text-muted"
class="text-center clickable-link mb-2"
:key="note.filename"
>
- <a :href="note.href">{{ note.title }}</a>
+ <a :href="note.href" @click.prevent="navigate(note.href, $event)">{{
+ note.title
+ }}</a>
</p>
</div>
</div>
+import codeSyntaxHighlight from "@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight-all.js";
+
export const markdownExt = "md";
// Base Paths
// Params
export const params = { searchTerm: "term", redirect: "redirect" };
+
+// Initial State
+export const dataDefaults = function() {
+ return {
+ // Views
+ views: {
+ login: 0,
+ home: 1,
+ note: 2,
+ search: 3,
+ },
+
+ // State
+ currentView: 1,
+ editMode: false,
+ draftSaveTimeout: null,
+
+ // Login Data
+ usernameInput: null,
+ passwordInput: null,
+ rememberMeInput: false,
+
+ // Note Data
+ notes: null,
+ searchTerm: null,
+ searchResults: null,
+ currentNote: null,
+ titleInput: null,
+ initialContent: null,
+
+ // Toast UI Plugins
+ viewerOptions: { plugins: [codeSyntaxHighlight] },
+ editorOptions: { plugins: [codeSyntaxHighlight] },
+ };
+};