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

2629
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -16,7 +16,7 @@
"@hibas123/secure-file-wrapper": "^2.3.1", "@hibas123/secure-file-wrapper": "^2.3.1",
"aes-js": "^3.1.2", "aes-js": "^3.1.2",
"crypto-js": "^3.1.9-1", "crypto-js": "^3.1.9-1",
"feather-icons": "^4.10.0", "feather-icons": "^4.19.0",
"idb": "^3.0.2", "idb": "^3.0.2",
"js-sha256": "^0.9.0", "js-sha256": "^0.9.0",
"js-sha512": "^0.8.0", "js-sha512": "^0.8.0",
@ -26,9 +26,9 @@
"uuid": "^3.3.2" "uuid": "^3.3.2"
}, },
"devDependencies": { "devDependencies": {
"@types/lodash.clonedeep": "^4.5.4", "@types/lodash.clonedeep": "^4.5.5",
"@types/uuid": "^3.4.4", "@types/uuid": "^3.4.4",
"copy-webpack-plugin": "^4.6.0", "copy-webpack-plugin": "^5.0.0",
"css-loader": "^2.1.0", "css-loader": "^2.1.0",
"file-loader": "^3.0.1", "file-loader": "^3.0.1",
"html-webpack-plugin": "^3.2.0", "html-webpack-plugin": "^3.2.0",
@ -39,13 +39,13 @@
"sass-loader": "^7.1.0", "sass-loader": "^7.1.0",
"style-loader": "^0.23.1", "style-loader": "^0.23.1",
"ts-loader": "^5.3.0", "ts-loader": "^5.3.0",
"typescript": "^3.2.4", "typescript": "^3.3.3333",
"webpack": "^4.29.0", "webpack": "^4.29.6",
"webpack-bundle-analyzer": "^3.0.3", "webpack-bundle-analyzer": "^3.1.0",
"webpack-cli": "^3.1.2", "webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.1.10", "webpack-dev-server": "^3.2.1",
"webpack-visualizer-plugin": "^0.1.11", "webpack-visualizer-plugin": "^0.1.11",
"workbox-webpack-plugin": "^3.6.3", "workbox-webpack-plugin": "^4.0.0",
"worker-loader": "^2.0.0" "worker-loader": "^2.0.0"
} }
} }

View File

@ -1,6 +0,0 @@
{
"auth_server": "https://auth.stamm.me",
"client_id": "",
"permission": "",
"callback_url": ""
}

BIN
public/favicon.ico Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -1 +1,54 @@
{} {
"short_name": "Secure Notes",
"name": "Secure Notes",
"decription": "A place to store your notes securly",
"share_target": {
"action": "/#/share",
"params": {
"title": "title",
"text": "text",
"url": "url"
}
},
"icons": [
{
"src": "notepad16.png",
"sizes": "16x16",
"type": "image/png"
},
{
"src": "notepad24.png",
"sizes": "24x24",
"type": "image/png"
},
{
"src": "notepad32.png",
"sizes": "32x32",
"type": "image/png"
},
{
"src": "notepad64.png",
"sizes": "64x64",
"type": "image/png"
},
{
"src": "notepad128.png",
"sizes": "128x128",
"type": "image/png"
},
{
"src": "notepad256.png",
"sizes": "256x256",
"type": "image/png"
},
{
"src": "notepad512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"start_url": "/",
"display": "standalone",
"theme_color": "#2196F3",
"background_color": "#ffffff"
}

BIN
public/notepad128.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/notepad16.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 562 B

BIN
public/notepad24.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 554 B

BIN
public/notepad256.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

BIN
public/notepad32.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 720 B

BIN
public/notepad512.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
public/notepad64.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 915 B

View File

@ -3,5 +3,5 @@ import "./add_button.scss";
import Plus from "feather-icons/dist/icons/plus.svg"; import Plus from "feather-icons/dist/icons/plus.svg";
export default function AddButton({ onClick }: { onClick: () => void }) { 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 { Router } from "./Routing";
import { h } from "preact"; import { h } from "preact";
import NotificationsComponent from "./notifications";
import { ModalComponent } from "./modals/Modal";
export default function App() { 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() { componentWillUnmount() {
Navigation.pageObservable.unsubscribe(this.onChange) Navigation.pageObservable.unsubscribe(this.onChange)
} }
to = -1;
onChange([page]: JSX.Element[]) { onChange([page]: JSX.Element[]) {
this.setState({ next: page, current: this.state.next || this.state.current }); this.setState({ next: page, current: this.state.next || this.state.current });
if (this.to >= 0) {
clearTimeout(this.to)
this.to = -1;
}
} }
render() { render() {
@ -46,8 +42,8 @@ export class Router extends Component<{}, { next?: JSX.Element, current: JSX.Ele
{this.state.next} {this.state.next}
</div> </div>
} }
return <div style="overflow:hidden"> return <div style="overflow:hidden; width: 1vw;">
<div class="transition_container" ref={elm => this.mounted = elm}> <div class="transition_container" key={this.state.current.key} ref={elm => this.mounted = elm}>
{this.state.current} {this.state.current}
</div> </div>
{overlay} {overlay}

View File

@ -1,11 +1,16 @@
import { h, Component } from 'preact'; import { h, Component } from 'preact';
import "./modal.scss" import "./modal.scss"
import { Modal } from './Modal'; import Modal from './Modal';
export class InputModal extends Modal<string> {
constructor(protected title: string, private fieldname: string, private type: "text" | "password") {
super();
}
export class InputModal extends Component<{ title: string, fieldname: string, type: "text" | "password", onResult: (result) => void }, {}> { private static IMD = class extends Component<{ modal: InputModal }, {}> {
input: HTMLInputElement;
rand: string; rand: string;
input: HTMLInputElement;
constructor(props) { constructor(props) {
super(props); super(props);
this.rand = Math.random().toString(); this.rand = Math.random().toString();
@ -17,24 +22,66 @@ export class InputModal extends Component<{ title: string, fieldname: string, ty
} }
render() { render() {
return <Modal title={this.props.title} onClose={() => this.props.onResult(null)}> return <Modal.BaseModal modal={this.props.modal}>
<fieldset style="border:none;"> <fieldset style="border:none;">
<label for={this.rand}>{this.props.fieldname}</label> <label for={this.rand}>{this.props.modal.fieldname}</label>
<input style="min-width: 85%" autofocus ref={elm => { <input style="min-width: 85%" autofocus ref={elm => {
this.input = elm this.input = elm
if (this.input) if (this.input)
setTimeout(() => this.input.focus(), 0) setTimeout(() => this.input.focus(), 0)
}} type={this.props.type} id={this.rand} placeholder={this.props.fieldname} onKeyDown={evt => { }} type={this.props.modal.type} id={this.rand} placeholder={this.props.modal.fieldname} onKeyDown={evt => {
if (evt.keyCode === 13) { if (evt.keyCode === 13) {
this.props.onResult(this.input.value) this.props.modal.result(this.input.value)
} }
}} /> }} />
<div style="text-align: right;"> <div style="text-align: right;">
<button class="primary" style="display: inline-block;" onClick={() => { <button class="primary" style="display: inline-block;" onClick={() => {
this.props.onResult(this.input.value); this.props.modal.result(this.input.value);
}}>Enter</button> }}>Enter</button>
</div> </div>
</fieldset> </fieldset>
</Modal> </Modal.BaseModal>
}
}
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 { h, Component } from 'preact';
import "./modal.scss" import "./modal.scss"
import { Modal } from './Modal'; import Modal from './Modal';
export default function LoadingModal() {
return <Modal title="Loading" noClose> 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> <div class="spinner primary" style="height: 80px; width: 80px; margin: 3rem auto;"></div>
</Modal> </Modal.BaseModal>
}
} }

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

@ -1,12 +1,49 @@
import { h, Component } from 'preact'; import Observable from "../../helper/observable";
import "./modal.scss" 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();
export class Modal extends Component<{ title: string, onClose?: () => void, noClose?: boolean }, {}> { protected abstract title: string;
constructor(props) {
super(props); // 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() { render() {
return <div class="modal_container" onClick={(evt) => { return <div class="modal_container" onClick={(evt) => {
let path = evt.composedPath(); let path = evt.composedPath();
@ -19,13 +56,89 @@ export class Modal extends Component<{ title: string, onClose?: () => void, noCl
} }
} }
return res; return res;
})) })) {
if (this.props.onClose) this.props.onClose(); this.props.modal.result(null)
}
}} onKeyDown={evt => {
if (evt.keyCode === 27) {
this.props.modal.result(null)
}
}}> }}>
<div class="card" > <div class="card" >
<h3 class="section">{this.props.title}</h3> <h3 class="section">{this.props.modal.title}</h3>
{this.props.children} {this.props.children}
</div> </div>
</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,14 +1,18 @@
import { h, Component } from 'preact'; import { h, Component } from 'preact';
import "./modal.scss" import "./modal.scss"
import { Modal } from './Modal'; import Modal from './Modal';
export class YesNoModal extends Component<{ title: string, onResult: (result: boolean | undefined) => void }, {}> { export class YesNoModal extends Modal<boolean> {
constructor(protected title: string) {
super();
}
private static IMD = class extends Component<{ modal: YesNoModal }, {}> {
constructor(props) { constructor(props) {
super(props); super(props);
this.onKeyDown = this.onKeyDown.bind(this); this.onKeyDown = this.onKeyDown.bind(this);
} }
componentWillMount() { componentWillMount() {
window.addEventListener("keydown", this.onKeyDown); window.addEventListener("keydown", this.onKeyDown);
} }
@ -18,22 +22,27 @@ export class YesNoModal extends Component<{ title: string, onResult: (result: bo
} }
onKeyDown(evt: KeyboardEvent) { onKeyDown(evt: KeyboardEvent) {
if (evt.keyCode === 74 || evt.keyCode === 89) this.props.onResult(true) if (evt.keyCode === 74 || evt.keyCode === 89) this.props.modal.result(true)
else if (evt.keyCode === 78) this.props.onResult(false) else if (evt.keyCode === 78) this.props.modal.result(false)
} }
render() { render() {
return <Modal title={this.props.title} onClose={() => this.props.onResult(undefined)}> return <Modal.BaseModal modal={this.props.modal}>
<fieldset style="border:none;"> <fieldset style="border:none;">
<div style="text-align: right;"> <div style="text-align: right;">
<button class="primary" style="display: inline-block;" onClick={() => { <button class="primary" style="display: inline-block;" onClick={() => {
this.props.onResult(false); this.props.modal.result(false);
}}>No</button> }}>No</button>
<button class="primary" style="display: inline-block;" onClick={() => { <button class="primary" style="display: inline-block;" onClick={() => {
this.props.onResult(true); this.props.modal.result(true);
}}>Yes</button> }}>Yes</button>
</div> </div>
</fieldset> </fieldset>
</Modal> </Modal.BaseModal>
}
}
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 { 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 Trash from "feather-icons/dist/icons/trash-2.svg"
import X from "feather-icons/dist/icons/x.svg" import X from "feather-icons/dist/icons/x.svg"
import Save from "feather-icons/dist/icons/save.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 Navigation from "../../../navigation";
import { YesNoModal } from "../../modals/YesNoModal"; import { YesNoModal } from "../../modals/YesNoModal";
import LoadingModal from "../../modals/LoadingModal"; import LoadingModal from "../../modals/LoadingModal";
import Notifications, { MessageType } from "../../../notifications";
import Modal from "../../modals/Modal";
const minRows = 3; 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; old_text: string;
text: string = ""; text: string = "";
vault: IVault; vault: IVault;
@ -20,29 +22,39 @@ export default class EntryComponent extends Component<{ vault: Promise<IVault>,
skip_save: boolean = false; skip_save: boolean = false;
loading?: LoadingModal;
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { changed: false, title: "", modal: undefined, loading: true }; this.state = { changed: false, title: "" };
} }
private toVault() { private toVault() {
history.back() // history.back()
// Navigation.setPage("/vault", { id: this.vault.id }, { entry: "false" }, true); Navigation.setPage("/vault", { id: this.vault.id }, { entry: "false" }, true);
} }
async componentWillMount() { async componentWillMount() {
try { try {
this.skip_save = false; this.skip_save = false;
this.setState({ loading: true }) this.loading = new LoadingModal();
this.loading.show();
this.vault = await this.props.vault; this.vault = await this.props.vault;
let note: ViewNote; let note: ViewNote;
let changed = false;
if (this.props.id) if (this.props.id)
note = await this.vault.getNote(this.props.id) note = await this.vault.getNote(this.props.id)
else else {
note = this.vault.newNote(); note = this.vault.newNote();
if (this.props.note) {
note.__value = this.props.note;
changed = true;
}
}
if (!note) { if (!note) {
Notes.messageObservableServer.send({ message: "Note not found!", type: MessageType.ERROR }); Notifications.sendNotification("Note not found!", MessageType.ERROR);
// this.toVault() // this.toVault()
} else { } else {
this.note = note; this.note = note;
@ -52,10 +64,12 @@ export default class EntryComponent extends Component<{ vault: Promise<IVault>,
this.rows = rows; this.rows = rows;
} }
let [title] = this.text.split("\n", 1); let [title] = this.text.split("\n", 1);
this.setState({ loading: false, title }) this.setState({ title, changed })
if (this.loading)
this.loading.close();
} }
} catch (err) { } 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 }) this.setState({ changed: false })
} }
} catch (err) { } 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) { if (this.state.changed) {
let modal = <YesNoModal title="Really want to quit?" onResult={res => { let modal = new YesNoModal("Really want to quit?");
let res = await modal.getResult();
modal.close();
if (res === true) { if (res === true) {
this.skip_save = true; this.skip_save = true;
this.toVault(); this.toVault();
} }
this.setState({ modal: undefined });
}} />
this.setState({ modal })
} else } else
this.toVault() this.toVault()
} }
textAreaKeyPress(evt: KeyboardEvent) { textAreaKeyPress(evt: KeyboardEvent) {
console.log(evt);
if ((evt.keyCode === 83 || evt.keyCode === 13) && evt.ctrlKey) { if ((evt.keyCode === 83 || evt.keyCode === 13) && evt.ctrlKey) {
event.preventDefault() evt.preventDefault()
this.save(); this.save();
return false; return false;
} }
else if (evt.keyCode === 27) { else if (evt.keyCode === 27) {
event.preventDefault(); evt.preventDefault();
// this.skip_save = true;
// this.toVault()
this.exitHandler(); this.exitHandler();
return false; return false;
} }
@ -141,8 +153,6 @@ export default class EntryComponent extends Component<{ vault: Promise<IVault>,
} }
render() { render() {
let loading_modal = this.state.loading ? <LoadingModal /> : undefined;
const save_handler = async () => { const save_handler = async () => {
await this.save() await this.save()
Navigation.setPage("/vault", { id: this.vault.id }, { entry: "false" }, true); 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> return <div>
{loading_modal}
{this.state.modal}
<header> <header>
<div> <div>
<a class="button header_icon_button" onClick={() => this.exitHandler()}><X height={undefined} width={undefined} /></a> <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 { h, Component } from "preact"
import { IVault, BaseNote } from "../../../notes"; import Notes, { IVault, BaseNote } from "../../../notes";
import AddButton from "../../AddButton"; import AddButton from "../../AddButton";
import Navigation from "../../../navigation"; import Navigation from "../../../navigation";
import ArrowLeft from "feather-icons/dist/icons/arrow-left.svg" import ArrowLeft from "feather-icons/dist/icons/arrow-left.svg"
import MoreVertival from "feather-icons/dist/icons/more-vertical.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) { constructor(props) {
super(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; vault: IVault;
reloadNotes() {
return new Promise(yes => this.vault.getAllNotes().then(entries => this.setState({ notes: entries }, yes)));
}
async componentWillMount() { async componentWillMount() {
this.vault = await this.props.vault; 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() { 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" }) Navigation.setPage("/vault", { id: this.vault.id }, { id, entry: "true" })
} }
let elms = this.state.entries.map(entry => { let elms = this.state.notes.map(note => {
let [first, second] = entry.preview.split("\n", 2); let [first, second] = note.preview.split("\n", 2);
return <div class="vault_vault" onClick={() => { return <div class="vault_vault" onContextMenu={evt => this.onContext(evt, note)} onClick={() => {
open_entry(entry._id) open_entry(note._id)
}}> }}>
<span>{first}</span><br /> <span>{first}</span><br />
<span>{second}</span> <span>{second}</span>
@ -32,12 +155,14 @@ export default class EntryList extends Component<{ vault: Promise<IVault> }, { e
}) })
return <div> return <div>
{this.state.context}
<header> <header>
<div> <div>
<a class="button header_icon_button" onClick={() => history.back()}><ArrowLeft height={undefined} width={undefined} /></a> <a class="button header_icon_button" onClick={() => history.back()}><ArrowLeft height={undefined} width={undefined} /></a>
</div> </div>
<h1 style="display:inline" class="button header_title" onClick={() => Navigation.setPage("/")}>{this.vault ? this.vault.name : ""}</h1> <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> </header>
<AddButton onClick={() => open_entry(null)} /> <AddButton onClick={() => open_entry(null)} />
<div class="container"> <div class="container">

View File

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

View File

@ -10,7 +10,7 @@
.vault_vault { .vault_vault {
padding: 0.5rem; padding: 0.5rem;
border-bottom: solid 1px var(--fore-color); border-bottom: solid 1px var(--card-border-color);
} }
.vault_vault:hover { .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 { InputModal } from "../../modals/InputModal";
import { YesNoModal } from "../../modals/YesNoModal"; import { YesNoModal } from "../../modals/YesNoModal";
import AddButton from "../../AddButton"; import AddButton from "../../AddButton";
import ContextMenu from "../../modals/context";
import Notifications from "../../../notifications";
export interface VaultsProps { export interface VaultsProps {
state: any; 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) { constructor(props: VaultsProps) {
super(props); super(props);
this.state = { vaults: [], modal: undefined }; this.state = { vaults: [], modal: undefined, context: undefined };
this.updateVaults = this.updateVaults.bind(this);
} }
updateVaults() { updateVaults() {
Notes.getVaults().then(vaults => this.setState({ vaults })) return new Promise(yes => {
Notes.getVaults().then(vaults => this.setState({ vaults }, yes))
})
} }
componentWillMount() { componentWillMount() {
this.updateVaults() 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() { render() {
let elms = this.state.vaults.map(vault => { let elms = this.state.vaults.map(vault => {
return <div class="vaults_vault" onClick={() => { return <div class="vaults_vault" onClick={() => this.openVault(vault)} onContextMenu={(evt) => this.onContext(evt, vault)}>
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()
}
}}>
{vault.encrypted ? <Lock height={undefined} width={undefined} /> : <Unlock height={undefined} width={undefined} />} {vault.encrypted ? <Lock height={undefined} width={undefined} /> : <Unlock height={undefined} width={undefined} />}
<span> <span>
{vault.name} {vault.name}
@ -72,41 +218,8 @@ export default class VaultsPage extends Page<VaultsProps, { vaults: VaultList, m
return <div style={{ marginTop: "-12px", paddingTop: "12px" }} > return <div style={{ marginTop: "-12px", paddingTop: "12px" }} >
{this.state.modal} {this.state.modal}
<AddButton onClick={() => { {this.state.context}
<AddButton onClick={() => this.addButtonClick()} />
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 })
}} />
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-sm-12 col-md-8 col-lg-6 col-md-offset-2 col-lg-offset-3"> <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 { .vaults_vault {
padding: 0.5rem; padding: 0.5rem;
border-bottom: solid 1px var(--fore-color); border-bottom: solid 1px var(--card-border-color);
} }
.vaults_vault:hover { .vaults_vault:hover {

View File

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

View File

@ -22,7 +22,6 @@
<link rel="mask-icon" href="/public/safari-pinned-tab.svg" color="#3E9AE9"> <link rel="mask-icon" href="/public/safari-pinned-tab.svg" color="#3E9AE9">
<meta name="msapplication-TileColor" content="#3E9AE9"> <meta name="msapplication-TileColor" content="#3E9AE9">
<meta name="theme-color" content="#3E9AE9"> <meta name="theme-color" content="#3E9AE9">
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mini.css/3.0.1/mini-default.min.css"> -->
</head> </head>
<body> <body>

View File

@ -1,8 +1,8 @@
import { h, render } from 'preact'; import { h, render } from 'preact';
import App from './components/App'; import App from './components/App';
// import "mini.css/src/flavors/mini-dark.scss" import "mini.css/src/flavors/mini-dark.scss"
import "mini.css/src/flavors/mini-default.scss" // import "mini.css/src/flavors/mini-default.scss"
import "./index.scss" import "./index.scss"
import Navigation from './navigation'; import Navigation from './navigation';
import VaultsPage from './components/routes/vaults/Vaults'; import VaultsPage from './components/routes/vaults/Vaults';
@ -11,6 +11,8 @@ import { Page } from './page';
import Notes from "./notes" import Notes from "./notes"
import DemoPage from './components/demo'; import DemoPage from './components/demo';
import VaultPage from './components/routes/vault/Vault'; import VaultPage from './components/routes/vault/Vault';
import SharePage from './components/routes/share/Share';
import Notifications from './notifications';
console.log(Notes); console.log(Notes);
(async () => { (async () => {
@ -22,7 +24,7 @@ console.log(Notes);
if (code) { if (code) {
let err = await Notes.getToken(code) let err = await Notes.getToken(code)
if (err) { if (err) {
alert("Login failed: " + err) Notifications.sendError("Login failed: " + err)
Notes.login() Notes.login()
} else { } else {
window.history.replaceState(null, document.title, "/" + window.location.hash); window.history.replaceState(null, document.title, "/" + window.location.hash);
@ -37,6 +39,7 @@ console.log(Notes);
Navigation.default = VaultsPage as typeof Page; Navigation.default = VaultsPage as typeof Page;
Navigation.addPage("/vault", VaultPage as typeof Page) Navigation.addPage("/vault", VaultPage as typeof Page)
Navigation.addPage("/demo", DemoPage as typeof Page) Navigation.addPage("/demo", DemoPage as typeof Page)
Navigation.addPage("/share", SharePage as typeof Page)
Navigation.start(); Navigation.start();
render(<App />, document.body, document.getElementById('app')); render(<App />, document.body, document.getElementById('app'));

View File

@ -1,6 +1,6 @@
import Observable from "./helper/observable"; import Observable from "./helper/observable";
import { Page, PageProps } from "./page"; import { Page, PageProps } from "./page";
import { h } from "preact"; import { h, VNode } from "preact";
function serializQuery(obj: any) { function serializQuery(obj: any) {
var str = []; var str = [];
@ -32,6 +32,8 @@ export default class Navigation {
private static _state: { [key: string]: any }; private static _state: { [key: string]: any };
private static _hidden_state: { [key: string]: any }; private static _hidden_state: { [key: string]: any };
private static _page_cache = new Map<string, VNode<any>>();
public static addPage(route: string, comp: typeof Page) { public static addPage(route: string, comp: typeof Page) {
Navigation._pages.set(route, comp); Navigation._pages.set(route, comp);
} }
@ -70,7 +72,22 @@ export default class Navigation {
else else
window.history.pushState(hidden, document.title, newhash); window.history.pushState(hidden, document.title, newhash);
} }
let page = h(component as any, { state: state, key: newhash + serializQuery(hidden), hidden: hidden }); let page = this._page_cache.get(newkey);
if (!page) {
console.log("Creating new page")
page = h(component as any, { state: state, key: newhash + serializQuery(hidden), hidden: hidden });
this._page_cache.set(newkey, page);
if (this._page_cache.size > 10) {
let cnt = this._page_cache.size - 10;
for (let key of this._page_cache.keys()) {
this._page_cache.delete(key);
cnt--;
if (cnt <= 0)
break;
}
}
}
Navigation._state = state; Navigation._state = state;
Navigation._hidden_state = hidden; Navigation._hidden_state = hidden;
@ -103,6 +120,22 @@ export default class Navigation {
} }
} }
static getQuery() {
let hash = window.location.hash.substring(1);
let s = undefined;
if (hash && hash !== "") {
let [_, query] = hash.split("?");
if (query) {
try {
s = parseQuery(query)
} catch (err) {
console.error(err);
}
}
}
return s;
}
static start() { static start() {
window.addEventListener("popstate", (ev) => { window.addEventListener("popstate", (ev) => {
Navigation.onHashChange(ev.state); Navigation.onHashChange(ev.state);

133
src/notes.ts Executable file → Normal file
View File

@ -12,12 +12,6 @@ export class HttpError extends Error {
} }
} }
export enum MessageType {
INFO,
WARNING,
ERROR
}
// const newSymbol = Symbol("Symbol for new Notes") // const newSymbol = Symbol("Symbol for new Notes")
export interface Note { export interface Note {
@ -47,6 +41,7 @@ import clonedeep = require("lodash.clonedeep");
import * as uuidv4 from "uuid/v4" import * as uuidv4 from "uuid/v4"
import IDB from "./helper/indexeddb"; import IDB from "./helper/indexeddb";
import { Transaction } from "idb"; import { Transaction } from "idb";
import Notifications, { MessageType } from "./notifications";
console.log(aesjs) console.log(aesjs)
const Encoder = new TextEncoder(); const Encoder = new TextEncoder();
@ -87,7 +82,7 @@ export interface IVault {
getAllNotes(): Promise<BaseNote[]>; getAllNotes(): Promise<BaseNote[]>;
searchNote(term: string): Promise<BaseNote[]> searchNote(term: string): Promise<BaseNote[]>
newNote(): ViewNote; newNote(): ViewNote;
saveNote(note: ViewNote): Promise<void>; saveNote(note: ViewNote, date?: Date): Promise<void>;
getNote(id: string): Promise<ViewNote>; getNote(id: string): Promise<ViewNote>;
deleteNote(id: string): Promise<void>; deleteNote(id: string): Promise<void>;
} }
@ -96,12 +91,8 @@ class NotesProvider {
private notesObservableServer = new Observable<Note>(true) private notesObservableServer = new Observable<Note>(true)
public notesObservable = this.notesObservableServer.getPublicApi() public notesObservable = this.notesObservableServer.getPublicApi()
public messageObservableServer = new Observable<{ message: string, type: MessageType }>(false) private syncObservableServer = new Observable<null>(true)
public messageObservable = this.messageObservableServer.getPublicApi() public syncObservable = this.syncObservableServer.getPublicApi()
public sendErrorMessage(error: Error) {
this.messageObservableServer.send({ message: error.message, type: MessageType.ERROR })
}
private database = new IDB("notes", ["notes", "oplog"]); private database = new IDB("notes", ["notes", "oplog"]);
private noteDB = this.database.getStore<DBNote>("notes"); private noteDB = this.database.getStore<DBNote>("notes");
@ -153,8 +144,14 @@ class NotesProvider {
} }
} }
public start() { public async start() {
return this.apiLockRls.then(lock => lock.release()).then(() => { this.sync() }) const next = () => {
setTimeout(() => {
this.sync().then(() => next);
}, 30000)
}
return this.apiLockRls.then(lock => lock.release()).then(() => { this.sync() }).then(() => next())
} }
private async getJWT() { private async getJWT() {
@ -163,8 +160,8 @@ class NotesProvider {
console.log("Getting JWT"); console.log("Getting JWT");
let req = await fetch(config.auth_server + "/api/oauth/jwt?refreshtoken=" + localStorage.getItem("refreshtoken")); let req = await fetch(config.auth_server + "/api/oauth/jwt?refreshtoken=" + localStorage.getItem("refreshtoken"));
if (req.status !== 200) { if (req.status !== 200) {
this.messageObservableServer.send({ message: "offline", type: MessageType.INFO }); Notifications.sendNotification("offline", MessageType.INFO);
throw new Error("Need login!") throw new Error("Offline")
} }
let res = await req.json(); let res = await req.json();
if (res.error) { if (res.error) {
@ -251,6 +248,16 @@ class NotesProvider {
log("Pairs builded"); log("Pairs builded");
let stats = {
remote_change: 0,
remote_delete: 0,
remote_create: 0,
local_change: 0,
local_delete: 0,
do_nothing: 0,
error: 0
}
log("Start inspection"); log("Start inspection");
for (let { local, remote, oplog, id } of pairs) { for (let { local, remote, oplog, id } of pairs) {
const apply = async (old = false) => { const apply = async (old = false) => {
@ -259,15 +266,18 @@ class NotesProvider {
switch (op.type) { switch (op.type) {
case OpLogType.CHANGE: case OpLogType.CHANGE:
log(id, "REMOTE CHANGE"); log(id, "REMOTE CHANGE");
stats.remote_change++;
await this._secureFile.update(id, op.values.value, b64.encode(op.values.preview), op.date, old); await this._secureFile.update(id, op.values.value, b64.encode(op.values.preview), op.date, old);
break; break;
case OpLogType.DELETE: case OpLogType.DELETE:
log(id, "REMOTE DELETE"); log(id, "REMOTE DELETE");
stats.remote_delete++;
if (old) break; // if the deletion is old, just ignore if (old) break; // if the deletion is old, just ignore
await this._secureFile.delete(id) await this._secureFile.delete(id)
break; break;
case OpLogType.CREATE: case OpLogType.CREATE:
log(id, "REMOTE CREATE"); log(id, "REMOTE CREATE");
stats.remote_create++;
await this._secureFile.create( await this._secureFile.create(
"", "",
op.values.value, op.values.value,
@ -282,8 +292,13 @@ class NotesProvider {
} }
} }
const localChange = (id: string) => {
//TODO implement
}
const create = async () => { const create = async () => {
log(id, "LOCAL CREATAE/UPDATE"); log(id, "LOCAL CREATAE/UPDATE");
stats.local_change++;
let value = await this._secureFile.get(id); let value = await this._secureFile.get(id);
let note: DBNote = { let note: DBNote = {
_id: remote._id, _id: remote._id,
@ -293,55 +308,61 @@ class NotesProvider {
__value: new Uint8Array(value) __value: new Uint8Array(value)
} }
await this.noteDB.set(id, note); await this.noteDB.set(id, note);
localChange(id);
} }
try { try {
log(id, "LRO: ", !!local, !!remote, !!oplog) // log(id, "LRO: ", !!local, !!remote, !!oplog)
if (remote && !oplog) { if (remote && !oplog) {
if (local) { if (local) {
let old = remote.active.time.valueOf() > local.time.valueOf(); let old = remote.active.time.valueOf() > local.time.valueOf();
if (old) if (old)
await create() await create()
else else {
stats.do_nothing++;
log(id, "DO NOTHING"); log(id, "DO NOTHING");
}
} else { } else {
await create() await create()
} }
} else if (!remote && local && !oplog) { // No local changes, but remote deleted } else if (!remote && local && !oplog) { // No local changes, but remote deleted
log("LOCAL DELETE"); log("LOCAL DELETE");
stats.local_delete++;
await this.noteDB.delete(id); await this.noteDB.delete(id);
localChange(id);
} else if (!remote && oplog) { // Remote does not exist, but oplog, just apply all changes including possible delete } else if (!remote && oplog) { // Remote does not exist, but oplog, just apply all changes including possible delete
await apply() await apply()
} else if (remote && oplog) { } else if (remote && oplog) {
let last = oplog[oplog.length - 1] let last = oplog[oplog.length - 1]
let old = remote.active.time.valueOf() > last.date.valueOf(); let old = remote.active.time.valueOf() > last.date.valueOf();
// if (local) { //If local changes and remote exist
if (old) if (old)
await create() // Will recreate local entry await create() // Will recreate local entry
await apply(old) // Will apply changes to remote await apply(old) // Will apply changes to remote
// }
// if (!local) { //If local ist deleted but remote exists check what is newer
// if (old)
// await create()
// await apply(old)
// }
} else { } else {
log(id, "DO NOTHING"); log(id, "DO NOTHING");
stats.do_nothing++;
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
this.messageObservableServer.send({ message: "Error syncing: " + id, type: MessageType.ERROR }) stats.error++;
Notifications.sendNotification("Error syncing: " + id, MessageType.ERROR);
} }
await this.oplogDB.delete(id); await this.oplogDB.delete(id);
} }
log("Stats", stats);
} finally { } finally {
log("Finished") log("Finished")
lock.release() lock.release()
this.syncObservableServer.send(null);
} }
} }
public forgetVaultKey(vault_id: string) {
this.vaultKeys.delete(vault_id);
localStorage.removeItem("vault_" + vault_id);
}
public getVaultKey(vault_id: string) { public getVaultKey(vault_id: string) {
let key = this.vaultKeys.get(vault_id); let key = this.vaultKeys.get(vault_id);
if (!key) { if (!key) {
@ -368,7 +389,7 @@ class NotesProvider {
.map(e => { .map(e => {
let value = e.__value; let value = e.__value;
let encrypted = false; let encrypted = false;
if (Decoder.decode(value) !== "__BASELINE__") encrypted = true; if (this.decrypt(value) !== "__BASELINE__") encrypted = true;
return { name: e.folder, encrypted, id: e._id } return { name: e.folder, encrypted, id: e._id }
})); }));
} }
@ -386,7 +407,7 @@ class NotesProvider {
this.addop(vault._id, OpLogType.CREATE, { this.addop(vault._id, OpLogType.CREATE, {
value: vault.__value, value: vault.__value,
preview: vault.preview preview: vault.preview
}, tx), }, vault.time, tx),
this.noteDB.set(vault._id, vault) this.noteDB.set(vault._id, vault)
]); ]);
} }
@ -398,6 +419,14 @@ class NotesProvider {
return new NotesProvider.Vault(vault, key) return new NotesProvider.Vault(vault, key)
} }
public async deleteVault(vault_id: string) {
let vault = await this.noteDB.get(vault_id);
if (!vault) throw new Error("Vault not found!");
let v = new NotesProvider.Vault(vault);
await Promise.all((await v.getAllNotes()).map(note => this.delete(note._id)));
await this.delete(v.id); // This can also delete a vault
}
public passwordToKey(password: string) { public passwordToKey(password: string) {
return new Uint8Array(sha256.arrayBuffer(password + config.client_id)) return new Uint8Array(sha256.arrayBuffer(password + config.client_id))
} }
@ -415,27 +444,37 @@ class NotesProvider {
} }
private _decrypt(value: ArrayBuffer, key?: Uint8Array): Uint8Array { private _decrypt(value: ArrayBuffer, key?: Uint8Array): Uint8Array {
if (!key) value; if (!key) return new Uint8Array(value);
var aesCtr = new aesjs.ModeOfOperation.ctr(key); var aesCtr = new aesjs.ModeOfOperation.ctr(key);
var decryptedBytes = aesCtr.decrypt(value); var decryptedBytes = aesCtr.decrypt(value);
return new Uint8Array(decryptedBytes) return new Uint8Array(decryptedBytes)
} }
private decrypt(value: ArrayBuffer, key: Uint8Array): string { private decrypt(value: ArrayBuffer, key?: Uint8Array): string {
let msg = this._decrypt(value, key) let msg = this._decrypt(value, key)
return Decoder.decode(this._decrypt(msg, this.generalEncryption)) return Decoder.decode(this._decrypt(msg, this.generalEncryption))
} }
async addop(note_id: string, type: OpLogType, values: { value: Uint8Array, preview: Uint8Array }, transaction?: Transaction) { async addop(note_id: string, type: OpLogType, values: { value: Uint8Array, preview: Uint8Array }, date: Date, transaction?: Transaction) {
let tx = transaction || Notes.oplogDB.transaction(); let tx = transaction || this.oplogDB.transaction();
let oplog = await Notes.oplogDB.get(note_id, tx); let oplog = await this.oplogDB.get(note_id, tx);
if (!oplog) oplog = { logs: [], id: note_id }; if (!oplog) oplog = { logs: [], id: note_id };
oplog.logs.push({ oplog.logs.push({
date: new Date(), date: date,
type, type,
values values
}) })
await Notes.oplogDB.set(note_id, oplog, tx); await this.oplogDB.set(note_id, oplog, tx);
}
async delete(id: string) {
let lock = await this.syncLock.getLock();
let tx = this.database.transaction(this.oplogDB, this.noteDB)
await Promise.all([
this.addop(id, OpLogType.DELETE, null, new Date(), tx),
this.noteDB.delete(id, tx)
])
lock.release();
} }
static Vault = class implements IVault { static Vault = class implements IVault {
@ -458,8 +497,7 @@ class NotesProvider {
async getAllNotes() { async getAllNotes() {
return Notes.noteDB.getAll() return Notes.noteDB.getAll()
.then(all => all .then(all => all.filter(e => e.folder === this.vault._id)
.filter(e => e.folder === this.vault._id)
.map<BaseNote>(e => { .map<BaseNote>(e => {
let new_note = clonedeep(<Note>e) as BaseNote let new_note = clonedeep(<Note>e) as BaseNote
delete (<any>new_note).__value delete (<any>new_note).__value
@ -483,7 +521,7 @@ class NotesProvider {
} }
} }
async saveNote(note: ViewNote) { async saveNote(note: ViewNote, date = new Date()) {
let lock = await Notes.syncLock.getLock(); let lock = await Notes.syncLock.getLock();
const tx = Notes.database.transaction(Notes.noteDB, Notes.oplogDB); const tx = Notes.database.transaction(Notes.noteDB, Notes.oplogDB);
let old_note = await Notes.noteDB.get(note._id, tx); let old_note = await Notes.noteDB.get(note._id, tx);
@ -493,13 +531,14 @@ class NotesProvider {
let [title, preview] = note.__value.split("\n"); let [title, preview] = note.__value.split("\n");
if (preview) preview = "\n" + preview; if (preview) preview = "\n" + preview;
else preview = "" else preview = ""
new_note.preview = this.encrypt(title + preview) new_note.preview = this.encrypt((title + preview).substr(0, 128))
new_note.time = date;
await Promise.all([ await Promise.all([
Notes.addop(note._id, !old_note ? OpLogType.CREATE : OpLogType.CHANGE, { Notes.addop(note._id, !old_note ? OpLogType.CREATE : OpLogType.CHANGE, {
value: new_note.__value, value: new_note.__value,
preview: new_note.preview preview: new_note.preview
}, tx), }, date, tx),
Notes.noteDB.set(note._id, new_note, tx) Notes.noteDB.set(note._id, new_note, tx)
]) ])
lock.release(); lock.release();
@ -513,14 +552,8 @@ class NotesProvider {
return new_note; return new_note;
} }
async deleteNote(id: string) { deleteNote(id: string) {
let lock = await Notes.syncLock.getLock(); return Notes.delete(id);
let tx = Notes.database.transaction(Notes.oplogDB, Notes.noteDB)
await Promise.all([
Notes.addop(id, OpLogType.DELETE, null, tx),
Notes.noteDB.delete(id, tx)
])
lock.release();
} }
} }
} }

34
src/notifications.ts Normal file
View File

@ -0,0 +1,34 @@
import Observable from "./helper/observable";
export enum MessageType {
INFO,
WARNING,
ERROR,
SUCCESS
}
export default class Notifications {
private static messageObservableServer = new Observable<{ message: string, type: MessageType }>(false)
public static messageObservable = Notifications.messageObservableServer.getPublicApi()
public static sendNotification(message: string, type: MessageType = MessageType.INFO) {
Notifications.messageObservableServer.send({ message, type });
}
public static sendInfo(message: string) {
this.sendNotification(message, MessageType.INFO)
}
public static sendWarning(message: string) {
this.sendNotification(message, MessageType.WARNING)
}
public static sendSuccess(message: string) {
this.sendNotification(message, MessageType.SUCCESS)
}
public static sendError(error: Error | string) {
Notifications.messageObservableServer.send({ message: typeof error === "string" ? error : error.message, type: MessageType.ERROR })
}
}

Binary file not shown.

View File

@ -5,13 +5,24 @@ const CopyWebpackPlugin = require('copy-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const Visualizer = require('webpack-visualizer-plugin'); const Visualizer = require('webpack-visualizer-plugin');
const path = require("path"); const path = require("path");
const fs = require("fs");
const config = require("./config.json");
const url = require("url");
module.exports = { module.exports = {
devServer: { devServer: {
host: "0.0.0.0", // Defaults to `localhost` host: "0.0.0.0", // Defaults to `localhost`
open: false, // Open the page in browser, open: false, // Open the page in browser,
contentBase: path.join(__dirname, 'dist'), contentBase: path.join(__dirname, 'dist'),
disableHostCheck: true disableHostCheck: true,
headers: {
"Access-Control-Allow-Origin": "*"
},
watchOptions: {
aggregateTimeout: 300,
poll: 500
},
public: url.parse(config.callback_url).hostname
}, },
plugins: [ plugins: [
new CopyWebpackPlugin([{ from: "public", to: "public" }]), new CopyWebpackPlugin([{ from: "public", to: "public" }]),