Many changes. See further for more details.

- Notification API
- New Modal API
- Vault JSON import and export
- Improved Page Cache
- Adding Context Menu API
- Adding Vault Deletion
- Fixing Sync Issues
- Implementing Share Target API
- Implementing Share To API
This commit is contained in:
Fabian
2019-03-04 21:48:31 -05:00
parent 313f5aee97
commit 3ef36ab6ca
38 changed files with 2117 additions and 1852 deletions

View File

@ -3,5 +3,5 @@ import "./add_button.scss";
import Plus from "feather-icons/dist/icons/plus.svg";
export default function AddButton({ onClick }: { onClick: () => void }) {
return <button class="add_button_button circular primary" onClick={() => onClick()}><Plus width={undefined} height={undefined} /></button>
return <button title={"add button"} class="add_button_button circular primary shadowed" onClick={() => onClick()}><Plus width={undefined} height={undefined} /></button>
}

View File

@ -1,6 +1,12 @@
import { Router } from "./Routing";
import { h } from "preact";
import NotificationsComponent from "./notifications";
import { ModalComponent } from "./modals/Modal";
export default function App() {
return <Router></Router>
return <div>
<ModalComponent />
<NotificationsComponent />
<Router />
</div>
}

View File

@ -18,13 +18,9 @@ export class Router extends Component<{}, { next?: JSX.Element, current: JSX.Ele
componentWillUnmount() {
Navigation.pageObservable.unsubscribe(this.onChange)
}
to = -1;
onChange([page]: JSX.Element[]) {
this.setState({ next: page, current: this.state.next || this.state.current });
if (this.to >= 0) {
clearTimeout(this.to)
this.to = -1;
}
}
render() {
@ -46,8 +42,8 @@ export class Router extends Component<{}, { next?: JSX.Element, current: JSX.Ele
{this.state.next}
</div>
}
return <div style="overflow:hidden">
<div class="transition_container" ref={elm => this.mounted = elm}>
return <div style="overflow:hidden; width: 1vw;">
<div class="transition_container" key={this.state.current.key} ref={elm => this.mounted = elm}>
{this.state.current}
</div>
{overlay}

View File

@ -1,40 +1,87 @@
import { h, Component } from 'preact';
import "./modal.scss"
import { Modal } from './Modal';
import Modal from './Modal';
export class InputModal extends Component<{ title: string, fieldname: string, type: "text" | "password", onResult: (result) => void }, {}> {
input: HTMLInputElement;
rand: string;
constructor(props) {
super(props);
this.rand = Math.random().toString();
export class InputModal extends Modal<string> {
constructor(protected title: string, private fieldname: string, private type: "text" | "password") {
super();
}
componentWillUnmount() {
if (this.input)
this.input.value = "";
private static IMD = class extends Component<{ modal: InputModal }, {}> {
rand: string;
input: HTMLInputElement;
constructor(props) {
super(props);
this.rand = Math.random().toString();
}
componentWillUnmount() {
if (this.input)
this.input.value = "";
}
render() {
return <Modal.BaseModal modal={this.props.modal}>
<fieldset style="border:none;">
<label for={this.rand}>{this.props.modal.fieldname}</label>
<input style="min-width: 85%" autofocus ref={elm => {
this.input = elm
if (this.input)
setTimeout(() => this.input.focus(), 0)
}} type={this.props.modal.type} id={this.rand} placeholder={this.props.modal.fieldname} onKeyDown={evt => {
if (evt.keyCode === 13) {
this.props.modal.result(this.input.value)
}
}} />
<div style="text-align: right;">
<button class="primary" style="display: inline-block;" onClick={() => {
this.props.modal.result(this.input.value);
}}>Enter</button>
</div>
</fieldset>
</Modal.BaseModal>
}
}
render() {
return <Modal title={this.props.title} onClose={() => this.props.onResult(null)}>
<fieldset style="border:none;">
<label for={this.rand}>{this.props.fieldname}</label>
<input style="min-width: 85%" autofocus ref={elm => {
this.input = elm
if (this.input)
setTimeout(() => this.input.focus(), 0)
}} type={this.props.type} id={this.rand} placeholder={this.props.fieldname} onKeyDown={evt => {
if (evt.keyCode === 13) {
this.props.onResult(this.input.value)
}
}} />
<div style="text-align: right;">
<button class="primary" style="display: inline-block;" onClick={() => {
this.props.onResult(this.input.value);
}}>Enter</button>
</div>
</fieldset>
</Modal>
getComponent() {
return <InputModal.IMD modal={this} />
}
}
}
// export class InputModal extends Component<{ title: string, fieldname: string, type: "text" | "password", onResult: (result) => void }, {}> {
// input: HTMLInputElement;
// rand: string;
// constructor(props) {
// super(props);
// this.rand = Math.random().toString();
// }
// componentWillUnmount() {
// if (this.input)
// this.input.value = "";
// }
// render() {
// return <Modal title={this.props.title} onClose={() => this.props.onResult(null)}>
// <fieldset style="border:none;">
// <label for={this.rand}>{this.props.fieldname}</label>
// <input style="min-width: 85%" autofocus ref={elm => {
// this.input = elm
// if (this.input)
// setTimeout(() => this.input.focus(), 0)
// }} type={this.props.type} id={this.rand} placeholder={this.props.fieldname} onKeyDown={evt => {
// if (evt.keyCode === 13) {
// this.props.onResult(this.input.value)
// }
// }} />
// <div style="text-align: right;">
// <button class="primary" style="display: inline-block;" onClick={() => {
// this.props.onResult(this.input.value);
// }}>Enter</button>
// </div>
// </fieldset>
// </Modal>
// }
// }

View File

@ -1,9 +1,17 @@
import { h, Component } from 'preact';
import "./modal.scss"
import { Modal } from './Modal';
import Modal from './Modal';
export default function LoadingModal() {
return <Modal title="Loading" noClose>
<div class="spinner primary" style="height: 80px; width: 80px; margin: 3rem auto;"></div>
</Modal>
export default class LoadingModal extends Modal<undefined> {
title = "";
constructor() {
super();
}
getComponent() {
return <Modal.BaseModal modal={this}>
<div class="spinner primary" style="height: 80px; width: 80px; margin: 3rem auto;"></div>
</Modal.BaseModal>
}
}

173
src/components/modals/Modal.tsx Executable file → Normal file
View File

@ -1,31 +1,144 @@
import { h, Component } from 'preact';
import "./modal.scss"
export class Modal extends Component<{ title: string, onClose?: () => void, noClose?: boolean }, {}> {
constructor(props) {
super(props);
}
render() {
return <div class="modal_container" onClick={(evt) => {
let path = evt.composedPath();
if (!path.find(e => {
let res = false;
let s = (e as Element);
if (s) {
if (s.classList) {
res = s.classList.contains("card")
}
}
return res;
}))
if (this.props.onClose) this.props.onClose();
}}>
<div class="card" >
<h3 class="section">{this.props.title}</h3>
{this.props.children}
</div>
</div>
}
import Observable from "../../helper/observable";
import { h, Component } from "preact";
export default abstract class Modal<T> {
// Static
private static modalObservableServer = new Observable<{ modal: Modal<any>, close: boolean }>(false);
public static modalObservable = Modal.modalObservableServer.getPublicApi();
protected abstract title: string;
// Private
private onResult: (result: T | null) => void;
// Protected
protected result(value: T | null) {
if (this.onResult)
this.onResult(value);
}
//Public
/**
* This function shows the modal
* Do not cell when using getResult()
*/
public async show() {
Modal.modalObservableServer.send({ modal: this, close: false });
}
/**
* Shows the modal and waits for result.
*
* Call close when successful
*/
public async getResult() {
this.show();
return new Promise<T | null>((yes) => this.onResult = yes);
}
public close() {
Modal.modalObservableServer.send({ modal: this, close: true });
}
public abstract getComponent(): JSX.Element;
public static BaseModal = class BaseModal<T> extends Component<{ modal: Modal<T> }, {}> {
render() {
return <div class="modal_container" onClick={(evt) => {
let path = evt.composedPath();
if (!path.find(e => {
let res = false;
let s = (e as Element);
if (s) {
if (s.classList) {
res = s.classList.contains("card")
}
}
return res;
})) {
this.props.modal.result(null)
}
}} onKeyDown={evt => {
if (evt.keyCode === 27) {
this.props.modal.result(null)
}
}}>
<div class="card" >
<h3 class="section">{this.props.modal.title}</h3>
{this.props.children}
</div>
</div>
}
}
}
// export default abstract class Modal<T, S> extends Component<{}, S> {
// // Abstract
// protected abstract renderChilds(): JSX.Element;
// protected abstract title: string;
// render() {
// return <div class="modal_container" onClick={(evt) => {
// let path = evt.composedPath();
// if (!path.find(e => {
// let res = false;
// let s = (e as Element);
// if (s) {
// if (s.classList) {
// res = s.classList.contains("card")
// }
// }
// return res;
// })) {
// this.result(null)
// }
// }} onKeyDown={evt => {
// if (evt.keyCode === 27) {
// this.result(null)
// }
// }}>
// <div class="card" >
// <h3 class="section">{this.title}</h3>
// {this.renderChilds()}
// </div>
// </div>
// }
// }
export class ModalComponent extends Component<{}, { modal: Modal<any> | undefined, component: JSX.Element | undefined }>{
constructor(props) {
super(props);
this.onModal = this.onModal.bind(this);
}
onModal([{ modal, close }]: { modal: Modal<any>, close: boolean }[]) {
if (!close && this.state.modal !== modal) {
this.setState({ modal: modal, component: modal.getComponent() })
}
else {
if (this.state.modal === modal) // Only close if the same
this.setState({ modal: undefined, component: undefined })
}
}
componentWillMount() {
Modal.modalObservable.subscribe(this.onModal, true);
}
componentWillUnmount() {
Modal.modalObservable.unsubscribe(this.onModal);
}
render() {
return <div>
{this.state.component}
</div>
}
}

View File

@ -1,39 +1,48 @@
import { h, Component } from 'preact';
import "./modal.scss"
import { Modal } from './Modal';
import Modal from './Modal';
export class YesNoModal extends Component<{ title: string, onResult: (result: boolean | undefined) => void }, {}> {
constructor(props) {
super(props);
this.onKeyDown = this.onKeyDown.bind(this);
export class YesNoModal extends Modal<boolean> {
constructor(protected title: string) {
super();
}
componentWillMount() {
window.addEventListener("keydown", this.onKeyDown);
private static IMD = class extends Component<{ modal: YesNoModal }, {}> {
constructor(props) {
super(props);
this.onKeyDown = this.onKeyDown.bind(this);
}
componentWillMount() {
window.addEventListener("keydown", this.onKeyDown);
}
componentWillUnmount() {
window.removeEventListener("keydown", this.onKeyDown);
}
onKeyDown(evt: KeyboardEvent) {
if (evt.keyCode === 74 || evt.keyCode === 89) this.props.modal.result(true)
else if (evt.keyCode === 78) this.props.modal.result(false)
}
render() {
return <Modal.BaseModal modal={this.props.modal}>
<fieldset style="border:none;">
<div style="text-align: right;">
<button class="primary" style="display: inline-block;" onClick={() => {
this.props.modal.result(false);
}}>No</button>
<button class="primary" style="display: inline-block;" onClick={() => {
this.props.modal.result(true);
}}>Yes</button>
</div>
</fieldset>
</Modal.BaseModal>
}
}
componentWillUnmount() {
window.removeEventListener("keydown", this.onKeyDown);
}
onKeyDown(evt: KeyboardEvent) {
if (evt.keyCode === 74 || evt.keyCode === 89) this.props.onResult(true)
else if (evt.keyCode === 78) this.props.onResult(false)
}
render() {
return <Modal title={this.props.title} onClose={() => this.props.onResult(undefined)}>
<fieldset style="border:none;">
<div style="text-align: right;">
<button class="primary" style="display: inline-block;" onClick={() => {
this.props.onResult(false);
}}>No</button>
<button class="primary" style="display: inline-block;" onClick={() => {
this.props.onResult(true);
}}>Yes</button>
</div>
</fieldset>
</Modal>
getComponent() {
return <YesNoModal.IMD modal={this} />
}
}

View File

@ -0,0 +1,6 @@
.context_menu {
button {
margin: 0;
display: block;
}
}

View File

@ -0,0 +1,8 @@
import { h } from "preact";
import "./context.scss";
export default function ContextMenu({ children, event }: { children: JSX.Element | JSX.Element[], event: MouseEvent }) {
return <div style={{ position: "fixed", left: event.pageX, top: event.pageY, zIndex: 10 }} class="context_menu">
{children}
</div>
}

View File

@ -0,0 +1,48 @@
.notifications_container {
position: fixed;
top: 0;
right: 0;
z-index: 128;
margin-top: 35px;
}
.notifications_success {
background: #2ECC40;
color: white;
}
.notifications_warning {
background: #FCA624;
color: white;
}
.notifications_error {
background: #FF4136;
color: white;
}
.notifications_info {
background: #0074D9;
color: white;
}
.notifications_notification {
h4 {
font-weight: bold;
margin: 0;
}
span {
margin-left: 3px;
word-break: break-word;
word-wrap: break-word;
}
position: static;
margin-top:10px;
margin-right:35px;
padding: 0.5em;
width: 300px;
}

View File

@ -0,0 +1,79 @@
import { h, Component } from "preact";
import Notes from "../notes";
import "./notifications.scss"
import Notifications, { MessageType } from "../notifications";
export default class NotificationsComponent extends Component<{}, {
notifications: { message: string; type: MessageType }[]
}> {
constructor(props: any, ctx: any) {
super(props, ctx);
this.state = {
notifications: [
// { message: "test001", type: MessageType.ERROR },
// { message: "test002", type: MessageType.WARNING },
// { message: "test003", type: MessageType.INFO },
// { message: "test004", type: MessageType.SUCCESS },
// { message: "test004 lsnmfkasngkanlkgnsljkebfkjebfkabsdkfjdabsksbvhjevbakjvkhhvsfhsvdkfvsehvfksevfhsvdif\naosugfiuasgdiug", type: MessageType.SUCCESS }
]
};
this.onNotification = this.onNotification.bind(this);
}
componentWillMount() {
Notifications.messageObservable.subscribe(this.onNotification);
}
componentWillUnmount() {
Notifications.messageObservable.unsubscribe(this.onNotification);
}
onNotification([not]: { message: string, type: MessageType }[]) {
console.log("Got notification", not)
let n = this.state.notifications.slice(0);
n.push(not);
this.setState({ notifications: n });
setTimeout(() => {
this.removeNot(not);
}, 3000);
}
removeNot(not: { message: string }) {
let n = this.state.notifications.slice(0).filter(e => e !== not);
this.setState({ notifications: n });
}
render() {
let notifications = this.state.notifications.map(not => {
let c;
let m;
switch (not.type) {
case MessageType.ERROR:
c = "notifications_error"
m = "Error";
break;
case MessageType.SUCCESS:
c = "notifications_success"
m = "Success";
break;
case MessageType.INFO:
c = "notifications_info"
m = "Info";
break;
case MessageType.WARNING:
c = "notifications_warning"
m = "Warning";
break;
}
return <div class={"notifications_notification " + c + " shadowed rounded"} key={not.message} onClick={() => this.removeNot(not)}>
<h4>{m}</h4>
<span>{not.message}</span>
</div>
})
return <div class="notifications_container">
{notifications}
</div>
}
}

View File

@ -0,0 +1,30 @@
import { h } from "preact";
import { Page } from "../../../page";
import VaultsPage from "../vaults/Vaults";
import Navigation from "../../../navigation";
export default class SharePage extends Page<{ state: any }, { vault: string }> {
text: string;
componentWillMount() {
let { title, text, url } = Navigation.getQuery() || {} as any;
let note = "";
if (title) {
note += title + "\n"
}
if (text) {
note += text;
}
if (url) {
note += "\n" + url;
}
this.text = note;
}
render() {
return <VaultsPage state={undefined} selectVault onSelected={vault => {
Navigation.setPage("/vault", { id: vault }, { entry: "true", note: this.text }, true);
}} />
}
}

View File

@ -1,5 +1,5 @@
import { h, Component } from "preact"
import Notes, { IVault, ViewNote, MessageType } from "../../../notes";
import Notes, { IVault, ViewNote } from "../../../notes";
import Trash from "feather-icons/dist/icons/trash-2.svg"
import X from "feather-icons/dist/icons/x.svg"
import Save from "feather-icons/dist/icons/save.svg"
@ -7,9 +7,11 @@ import Save from "feather-icons/dist/icons/save.svg"
import Navigation from "../../../navigation";
import { YesNoModal } from "../../modals/YesNoModal";
import LoadingModal from "../../modals/LoadingModal";
import Notifications, { MessageType } from "../../../notifications";
import Modal from "../../modals/Modal";
const minRows = 3;
export default class EntryComponent extends Component<{ vault: Promise<IVault>, id: string | undefined }, { loading: boolean, title: string, changed: boolean, modal: JSX.Element | undefined }> {
export default class EntryComponent extends Component<{ vault: Promise<IVault>, id: string | undefined, note: string | undefined }, { title: string, changed: boolean }> {
old_text: string;
text: string = "";
vault: IVault;
@ -20,29 +22,39 @@ export default class EntryComponent extends Component<{ vault: Promise<IVault>,
skip_save: boolean = false;
loading?: LoadingModal;
constructor(props) {
super(props);
this.state = { changed: false, title: "", modal: undefined, loading: true };
this.state = { changed: false, title: "" };
}
private toVault() {
history.back()
// Navigation.setPage("/vault", { id: this.vault.id }, { entry: "false" }, true);
// history.back()
Navigation.setPage("/vault", { id: this.vault.id }, { entry: "false" }, true);
}
async componentWillMount() {
try {
this.skip_save = false;
this.setState({ loading: true })
this.loading = new LoadingModal();
this.loading.show();
this.vault = await this.props.vault;
let note: ViewNote;
let changed = false;
if (this.props.id)
note = await this.vault.getNote(this.props.id)
else
else {
note = this.vault.newNote();
if (this.props.note) {
note.__value = this.props.note;
changed = true;
}
}
if (!note) {
Notes.messageObservableServer.send({ message: "Note not found!", type: MessageType.ERROR });
Notifications.sendNotification("Note not found!", MessageType.ERROR);
// this.toVault()
} else {
this.note = note;
@ -52,10 +64,12 @@ export default class EntryComponent extends Component<{ vault: Promise<IVault>,
this.rows = rows;
}
let [title] = this.text.split("\n", 1);
this.setState({ loading: false, title })
this.setState({ title, changed })
if (this.loading)
this.loading.close();
}
} catch (err) {
Notes.sendErrorMessage(err);
Notifications.sendError(err);
}
}
@ -67,7 +81,7 @@ export default class EntryComponent extends Component<{ vault: Promise<IVault>,
this.setState({ changed: false })
}
} catch (err) {
Notes.sendErrorMessage(err);
Notifications.sendError(err);
}
}
@ -110,30 +124,28 @@ export default class EntryComponent extends Component<{ vault: Promise<IVault>,
}
}
exitHandler() {
async exitHandler() {
if (this.state.changed) {
let modal = <YesNoModal title="Really want to quit?" onResult={res => {
if (res === true) {
this.skip_save = true;
this.toVault();
}
this.setState({ modal: undefined });
}} />
this.setState({ modal })
let modal = new YesNoModal("Really want to quit?");
let res = await modal.getResult();
modal.close();
if (res === true) {
this.skip_save = true;
this.toVault();
}
} else
this.toVault()
}
textAreaKeyPress(evt: KeyboardEvent) {
console.log(evt);
if ((evt.keyCode === 83 || evt.keyCode === 13) && evt.ctrlKey) {
event.preventDefault()
evt.preventDefault()
this.save();
return false;
}
else if (evt.keyCode === 27) {
event.preventDefault();
// this.skip_save = true;
// this.toVault()
evt.preventDefault();
this.exitHandler();
return false;
}
@ -141,8 +153,6 @@ export default class EntryComponent extends Component<{ vault: Promise<IVault>,
}
render() {
let loading_modal = this.state.loading ? <LoadingModal /> : undefined;
const save_handler = async () => {
await this.save()
Navigation.setPage("/vault", { id: this.vault.id }, { entry: "false" }, true);
@ -155,8 +165,6 @@ export default class EntryComponent extends Component<{ vault: Promise<IVault>,
}
return <div>
{loading_modal}
{this.state.modal}
<header>
<div>
<a class="button header_icon_button" onClick={() => this.exitHandler()}><X height={undefined} width={undefined} /></a>

View File

@ -1,19 +1,142 @@
import { h, Component } from "preact"
import { IVault, BaseNote } from "../../../notes";
import Notes, { IVault, BaseNote } from "../../../notes";
import AddButton from "../../AddButton";
import Navigation from "../../../navigation";
import ArrowLeft from "feather-icons/dist/icons/arrow-left.svg"
import MoreVertival from "feather-icons/dist/icons/more-vertical.svg"
import ContextMenu from "../../modals/context";
import Notifications from "../../../notifications";
export default class EntryList extends Component<{ vault: Promise<IVault> }, { entries: BaseNote[] }> {
export default class EntryList extends Component<{ vault: Promise<IVault> }, { notes: BaseNote[], context: JSX.Element | undefined }> {
constructor(props) {
super(props)
this.state = { entries: [] }
this.state = { notes: [], context: undefined }
this.onDragOver = this.onDragOver.bind(this);
this.onDrop = this.onDrop.bind(this);
this.reloadNotes = this.reloadNotes.bind(this);
}
vault: IVault;
reloadNotes() {
return new Promise(yes => this.vault.getAllNotes().then(entries => this.setState({ notes: entries }, yes)));
}
async componentWillMount() {
this.vault = await this.props.vault;
this.vault.getAllNotes().then(entries => this.setState({ entries }))
this.reloadNotes();
document.body.addEventListener("dragover", this.onDragOver);
document.body.addEventListener("drop", this.onDrop);
Notes.syncObservable.subscribe(this.reloadNotes);
}
componentWillUnmount() {
document.body.removeEventListener("dragover", this.onDragOver);
document.body.removeEventListener("drop", this.onDrop);
Notes.syncObservable.unsubscribe(this.reloadNotes);
}
onDragOver(evt: DragEvent) {
evt.preventDefault();
}
onContext(evt: MouseEvent, note: BaseNote) {
evt.preventDefault();
const shareNote = async () => {
let nav = window.navigator as any;
if (nav.share !== undefined) {
let vnote = await this.vault.getNote(note._id);
nav.share({ title: vnote.preview.split("\n")[0], text: vnote.__value })
.then(() => console.log('Successful share'))
.catch(error => {
console.error('Error sharing:', error)
Notifications.sendError(error)
});
} else {
Notifications.sendError("Sharing not possible on this device")
}
}
let share;
if ((window.navigator as any).share) {
share = <button onClick={() => shareNote()}>
share
</button>
let context = <ContextMenu event={evt} >
{share}
</ContextMenu>
this.setState({ context });
}
return false;
}
async onDrop(evt: DragEvent) {
evt.preventDefault();
if (evt.dataTransfer.items) {
for (let i = 0; i < evt.dataTransfer.items.length; i++) {
let item = evt.dataTransfer.items[i];
if (item.kind === "file") {
let file = item.getAsFile();
if (file.type !== "application/json") {
Notifications.sendError("Invalid File Type!!!")
} else {
try {
let data = await new Promise<string>((yes, no) => {
let fr = new FileReader()
fr.onload = (ev) => {
yes((ev.target as any).result);
}
fr.onerror = no;
fr.readAsText(file);
})
let parsed = JSON.parse(data);
let c = new Error("Could not parse!");
let notes: { content: string, time: Date }[] = null;
if (Array.isArray(parsed)) { // Could be from SecureNotes 1
notes = parsed.map(elm => {
if (typeof elm.message !== "string" || typeof elm.time !== "string") {
throw c;
}
return {
content: elm.message,
time: new Date(elm.time)
}
})
} else if (parsed.version) { // Could be from SecureNotes 2
if (parsed.version === 1) { //Could be from SecureNotes 2 version 1
notes = (parsed.notes as any[]).map(elm => {
if (typeof elm.content !== "string" || typeof elm.time !== "string") {
throw c;
}
return {
content: elm.content,
time: new Date(elm.time)
}
})
} else {
throw c;
}
} else
throw c;
await Promise.all(notes.map(n => {
let note = this.vault.newNote();
note.__value = n.content;
return this.vault.saveNote(note, n.time);
}));
await this.reloadNotes();
Notifications.sendSuccess(`Imported ${notes.length} notes!`);
} catch (err) {
Notifications.sendError("Cannot read File!")
console.error(err);
}
}
}
}
}
}
render() {
@ -21,10 +144,10 @@ export default class EntryList extends Component<{ vault: Promise<IVault> }, { e
Navigation.setPage("/vault", { id: this.vault.id }, { id, entry: "true" })
}
let elms = this.state.entries.map(entry => {
let [first, second] = entry.preview.split("\n", 2);
return <div class="vault_vault" onClick={() => {
open_entry(entry._id)
let elms = this.state.notes.map(note => {
let [first, second] = note.preview.split("\n", 2);
return <div class="vault_vault" onContextMenu={evt => this.onContext(evt, note)} onClick={() => {
open_entry(note._id)
}}>
<span>{first}</span><br />
<span>{second}</span>
@ -32,12 +155,14 @@ export default class EntryList extends Component<{ vault: Promise<IVault> }, { e
})
return <div>
{this.state.context}
<header>
<div>
<a class="button header_icon_button" onClick={() => history.back()}><ArrowLeft height={undefined} width={undefined} /></a>
</div>
<h1 style="display:inline" class="button header_title" onClick={() => Navigation.setPage("/")}>{this.vault ? this.vault.name : ""}</h1>
<a class="button header_icon_button"><MoreVertival height={undefined} width={undefined} /></a>
<span></span>
{/* <a class="button header_icon_button"><MoreVertival height={undefined} width={undefined} /></a> */}
</header>
<AddButton onClick={() => open_entry(null)} />
<div class="container">

View File

@ -9,7 +9,7 @@ import "./vault.scss"
export interface VaultProps {
state: { id: string };
hidden: { entry: string, id: string };
hidden: { entry: string, id: string, note: string };
}
export default class VaultPage extends Page<VaultProps, { entries: BaseNote[] }> {
@ -29,7 +29,7 @@ export default class VaultPage extends Page<VaultProps, { entries: BaseNote[] }>
render() {
if (this.props.hidden && this.props.hidden.entry === "true") {
return <EntryComponent vault={this.vault} id={this.props.hidden.id} />
return <EntryComponent vault={this.vault} id={this.props.hidden.id} note={this.props.hidden.note} />
} else {
return <EntryList vault={this.vault} />
}

View File

@ -10,7 +10,7 @@
.vault_vault {
padding: 0.5rem;
border-bottom: solid 1px var(--fore-color);
border-bottom: solid 1px var(--card-border-color);
}
.vault_vault:hover {

257
src/components/routes/vaults/Vaults.tsx Executable file → Normal file
View File

@ -8,61 +8,207 @@ import Navigation from "../../../navigation";
import { InputModal } from "../../modals/InputModal";
import { YesNoModal } from "../../modals/YesNoModal";
import AddButton from "../../AddButton";
import ContextMenu from "../../modals/context";
import Notifications from "../../../notifications";
export interface VaultsProps {
state: any;
selectVault?: boolean;
onSelected?: (vaultid: string) => void;
}
export default class VaultsPage extends Page<VaultsProps, { vaults: VaultList, modal: JSX.Element | undefined }> {
export default class VaultsPage extends Page<VaultsProps, { vaults: VaultList, modal: JSX.Element | undefined, context: JSX.Element | undefined }> {
constructor(props: VaultsProps) {
super(props);
this.state = { vaults: [], modal: undefined };
this.state = { vaults: [], modal: undefined, context: undefined };
this.updateVaults = this.updateVaults.bind(this);
}
updateVaults() {
Notes.getVaults().then(vaults => this.setState({ vaults }))
return new Promise(yes => {
Notes.getVaults().then(vaults => this.setState({ vaults }, yes))
})
}
componentWillMount() {
this.updateVaults()
Notes.syncObservable.subscribe(this.updateVaults);
}
componentWillUnmount() {
Notes.syncObservable.unsubscribe(this.updateVaults);
}
async getKey(vault: { name: string, id: string }, permanent = true) {
let inp_mod = new InputModal("Enter password for " + vault.name, "Password", "password");
let key = undefined;
while (true) {
let value = await inp_mod.getResult();
if (value === null) {
inp_mod.close();
return false;
} else {
key = Notes.passwordToKey(value);
try {
await Notes.getVault(vault.id, key);
break;
} catch (err) {
Notifications.sendError("Invalid password!")
}
}
}
inp_mod.close();
let perm = false;
if (permanent) {
let save_modal = new YesNoModal("Save permanent?");
let res = await save_modal.getResult();
save_modal.close();
if (res === undefined) {
res = false;
}
perm = res;
}
Notes.saveVaultKey(vault.id, key, perm);
return true;
}
async openVault(vault: { name: string, encrypted: boolean, id: string }) {
const action = () => {
if (this.props.selectVault) {
this.props.onSelected(vault.id);
} else {
Navigation.setPage("/vault", { id: vault.id })
}
}
if (vault.encrypted) {
let key = Notes.getVaultKey(vault.id);
if (key)
action()
else {
if (await this.getKey(vault))
action();
}
} else {
action()
}
}
async addButtonClick() {
let name_modal = new InputModal("Enter new name", "Name", "text");
let name = await name_modal.getResult();
name_modal.close();
if (name === null) return;
let encrypted_modal = new YesNoModal("Encrypt?");
let encrypted = encrypted_modal.getResult();
encrypted_modal.close();
if (encrypted === null) return;
let password;
if (encrypted) {
let password_modal = new InputModal("Enter new password", "Password", "password");
password = await password_modal.getResult();
password_modal.close();
if (password === null) return;
}
let key;
if (password) {
key = Notes.passwordToKey(password)
}
await Notes.createVault(name, key)
this.updateVaults();
}
onContext(evt: MouseEvent, vault: { name: string, encrypted: boolean, id: string }) {
evt.preventDefault();
console.log("Context", evt);
// let context = <div style={{ position: "fixed", left: evt.pageX, top: evt.pageY, zIndex: 10 }}>
// <button>Action 1</button>
// </div>
const close = () => {
window.removeEventListener("click", close);
this.setState({ context: undefined });
}
window.addEventListener("click", close);
let deleteb = <button onClick={async () => {
let delete_modal = new YesNoModal("Delete Vault? Cannot be undone!");
let result = await delete_modal.getResult();
delete_modal.close();
if (result) {
Notes.deleteVault(vault.id).then(() => {
this.updateVaults();
}).catch(err => {
Notifications.sendError("error deleting vault!")
console.error(err);
})
}
}}>
delete
</button>;
let delete_key;
if (Notes.getVaultKey(vault.id)) {
delete_key = <button onClick={() => { Notes.forgetVaultKey(vault.id); }}>
forget password
</button>;
}
let exportb = <button onClick={async () => {
let key: Uint8Array;
if (vault.encrypted) {
await this.getKey(vault, false)
key = Notes.getVaultKey(vault.id);
}
let note_vault = await Notes.getVault(vault.id, key);
let base_notes = await note_vault.getAllNotes();
let notes = await Promise.all(base_notes.map(e => {
return note_vault.getNote(e._id);
}));
let result =
{
version: 1,
notes: notes.map(e => {
return {
content: e.__value,
time: e.time
}
})
}
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(result, undefined, 3));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "notes_export_" + vault.name + ".json");
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}}>
export
</button>;
let context = <ContextMenu event={evt} >
{deleteb}
{delete_key}
{exportb}
</ContextMenu>
this.setState({ context });
return false;
}
render() {
let elms = this.state.vaults.map(vault => {
return <div class="vaults_vault" onClick={() => {
const open = () => {
Navigation.setPage("/vault", { id: vault.id })
}
if (vault.encrypted) {
let key = Notes.getVaultKey(vault.id);
if (key) open()
else {
let modal = <InputModal title={"Enter password for " + vault.name} type="password" fieldname="Password" onResult={(value) => {
if (value === null) this.setState({ modal: undefined });
else {
let key = Notes.passwordToKey(value);
Notes.getVault(vault.id, key).then(() => {
let modal = <YesNoModal title="Save permanent?" onResult={(res) => {
if (res === undefined)
this.setState({ modal: undefined });
else {
this.setState({ modal: undefined });
Notes.saveVaultKey(vault.id, key, res);
open()
}
}} />
this.setState({ modal })
}).catch(err => {
alert("Invalid password!")
})
}
}} />
this.setState({ modal })
}
} else {
open()
}
}}>
return <div class="vaults_vault" onClick={() => this.openVault(vault)} onContextMenu={(evt) => this.onContext(evt, vault)}>
{vault.encrypted ? <Lock height={undefined} width={undefined} /> : <Unlock height={undefined} width={undefined} />}
<span>
{vault.name}
@ -72,41 +218,8 @@ export default class VaultsPage extends Page<VaultsProps, { vaults: VaultList, m
return <div style={{ marginTop: "-12px", paddingTop: "12px" }} >
{this.state.modal}
<AddButton onClick={() => {
let modal = <InputModal title="Enter new name" fieldname="Name" type="text" onResult={(name) => {
const create = async (password?: string) => {
let key;
if (password) {
key = Notes.passwordToKey(password)
}
await Notes.createVault(name, key)
this.updateVaults();
}
if (name === null) this.setState({ modal: undefined })
else {
let modal = <YesNoModal title="Encrypt?" onResult={(encrypted) => {
if (encrypted === null) this.setState({ modal: undefined })
if (encrypted) {
let modal = <InputModal title="Enter new password" fieldname="Password" type="password" onResult={(password) => {
if (password === null) this.setState({ modal: undefined })
else {
create(password)
this.setState({ modal: undefined });
}
}} />
this.setState({ modal })
} else {
create()
this.setState({ modal: undefined });
}
}} />
this.setState({ modal })
}
}} />
this.setState({ modal })
}} />
{this.state.context}
<AddButton onClick={() => this.addButtonClick()} />
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-8 col-lg-6 col-md-offset-2 col-lg-offset-3">

View File

@ -4,7 +4,7 @@
.vaults_vault {
padding: 0.5rem;
border-bottom: solid 1px var(--fore-color);
border-bottom: solid 1px var(--card-border-color);
}
.vaults_vault:hover {

View File

@ -11,7 +11,7 @@
.transition_slidein {
animation-name: slidein;
animation-duration: 0.3s;
z-index: 128;
z-index: 64;
}
@keyframes slidein {