Compare commits

..

No commits in common. "86ae6acc92c76595c339dfff2a30108378df04c9" and "3c80ac3125623b800babbe81d5a0535f05c2bfbc" have entirely different histories.

32 changed files with 6891 additions and 5246 deletions

View File

@ -1,9 +0,0 @@
https://notes.dev.hibas123.de:443 {
proxy / localhost:1234/
tls D:\Certs\_wildcard.dev.hibas123.de.pem D:\Certs\_wildcard.dev.hibas123.de-key.pem
}
https://sf.dev.hibas123.de:443 {
proxy / localhost:3004/
tls D:\Certs\_wildcard.dev.hibas123.de.pem D:\Certs\_wildcard.dev.hibas123.de-key.pem
}

10778
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,34 +4,48 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"build": "parcel build src/index.html", "build": "webpack --mode=production",
"dev": "parcel src/index.html" "watch": "webpack --mode=development --watch",
"start": "webpack-dev-server --mode=production",
"start:prod": "webpack-dev-server --mode=production",
"start:dev": "webpack-dev-server --mode=development"
}, },
"author": "Fabian Stamm <dev@fabianstamm.de>", "author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "MIT", "license": "MIT",
"browserslist": [
"last 2 Chrome versions"
],
"dependencies": { "dependencies": {
"@hibas123/secure-file-wrapper": "^2.5.1", "@hibas123/secure-file-wrapper": "^2.5.0",
"@hibas123/theme": "^1.0.5", "@hibas123/theme": "^1.0.5",
"@hibas123/utils": "^2.2.17", "@hibas123/utils": "^2.1.0",
"aes-js": "^3.1.2", "aes-js": "^3.1.2",
"codemirror": "^5.58.3", "feather-icons": "^4.22.1",
"feather-icons": "^4.28.0", "idb": "^4.0.3",
"idb": "^5.0.7",
"js-sha256": "^0.9.0", "js-sha256": "^0.9.0",
"lodash.clonedeep": "^4.5.0", "lodash.clonedeep": "^4.5.0",
"parcel": "^1.12.4",
"preact": "^10.5.7",
"preact-feather": "^4.1.0",
"secure-file-wrapper": "git+https://git.stamm.me/OpenServer/OSSecureFileWrapper.git", "secure-file-wrapper": "git+https://git.stamm.me/OpenServer/OSSecureFileWrapper.git",
"uuid": "^8.3.1" "uuid": "^3.3.2"
}, },
"devDependencies": { "devDependencies": {
"@types/codemirror": "0.0.99",
"@types/lodash.clonedeep": "^4.5.6", "@types/lodash.clonedeep": "^4.5.6",
"@types/uuid": "^8.3.0", "@types/uikit": "^2.27.7",
"typescript": "^4.1.2" "@types/uuid": "^3.4.5",
"copy-webpack-plugin": "^5.0.3",
"css-loader": "^3.0.0",
"file-loader": "^4.0.0",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.7.0",
"node-sass": "^4.12.0",
"preact": "^8.3.1",
"preact-svg-loader": "^0.2.1",
"raw-loader": "^3.0.0",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"ts-loader": "^6.0.4",
"typescript": "^3.5.2",
"webpack": "^4.35.2",
"webpack-bundle-analyzer": "^3.3.2",
"webpack-cli": "^3.3.5",
"webpack-dev-server": "^3.7.2",
"webpack-visualizer-plugin": "^0.1.11",
"worker-loader": "^2.0.0"
} }
} }

0
src/public/favicon.ico → public/favicon.ico Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

55
public/manifest.json Executable file
View File

@ -0,0 +1,55 @@
{
"short_name": "Secure Notes",
"name": "Secure Notes",
"decription": "A place to store your notes securly",
"share_target": {
"action": "/share",
"method": "GET",
"enctype": "application/x-www-form-urlencoded",
"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": "#1E88E5",
"background_color": "#ffffff"
}

0
src/public/notepad128.png → public/notepad128.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

0
src/public/notepad16.png → public/notepad16.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 562 B

After

Width:  |  Height:  |  Size: 562 B

0
src/public/notepad24.png → public/notepad24.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 554 B

After

Width:  |  Height:  |  Size: 554 B

0
src/public/notepad256.png → public/notepad256.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

0
src/public/notepad32.png → public/notepad32.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 720 B

After

Width:  |  Height:  |  Size: 720 B

0
src/public/notepad512.png → public/notepad512.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

0
src/public/notepad64.png → public/notepad64.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 915 B

After

Width:  |  Height:  |  Size: 915 B

121
public/serviceworker.js Normal file
View File

@ -0,0 +1,121 @@
function log(...params) {
console.log.apply(this, [...["%c[SW]: %c", "color: #f4b942;", "color:unset;"], ...params])
}
const CACHE = "offline";
let precacheFiles = [
"/",
"/index.html",
"/main.js",
"/main.css",
"/serviceworker.js"
]
//Install stage sets up the cache-array to configure pre-cache content
self.addEventListener('install', (evt) => {
log('The service worker is being installed.');
evt.waitUntil(precache().then(() => {
log('Skip waiting on install');
}).catch(log).then(() => self.skipWaiting()));
});
//allow sw to control of current page
self.addEventListener('activate', (event) => {
log('Claiming clients for current page');
return self.clients.claim();
});
self.addEventListener('message', (event) => {
log("Clearing cache");
caches.delete(CACHE);
event.waitUntil(precache());
});
var Types;
(function (Types) {
Types[Types["CACHE"] = 0] = "CACHE";
Types[Types["NOCACHE"] = 1] = "NOCACHE";
Types[Types["REFRESH"] = 2] = "REFRESH";
Types[Types["INDEX"] = 3] = "INDEX";
})(Types || (Types = {}));
let rules = [{
match: (url) => {
return url.indexOf("/api/") >= 0;
},
type: Types.NOCACHE
}, {
match: (url) => {
return url.indexOf("/share") >= 0;
},
type: Types.INDEX
},
{
match: () => {
return true;
},
type: Types.REFRESH
}
]
self.addEventListener('fetch', (evt) => {
if (evt.request.method != 'GET') return; // Dont care about POST requests
let rule = rules.find(rule => rule.match(evt.request.url));
evt.respondWith((async () => {
log("Cache:", Types[rule.type]);
switch (rule.type) {
case Types.CACHE:
return fromCache(evt.request);
case Types.REFRESH:
return refresh(evt.request).then(r => {
evt.waitUntil(r.refresh.catch(_ => {}));
return r.result;
});
case Types.NOCACHE:
return fetch(evt.request);
case Types.INDEX:
return refresh(new Request("/")).then(r => {
evt.waitUntil(r.refresh.catch(_ => {}));
return r.result;
})
}
})());
});
async function fromCache(request) {
let cache = await caches.open(CACHE);
let matching = await cache.match(request);
if (matching)
return matching
let res = await fetch(request.clone());
await cache.put(request, {
match: (url) => {
return url.indexOf("/version_hash") >= 0;
},
type: Types.NOCACHE
}, res);
return await cache.match(request);
}
async function refresh(request) {
let cache = await caches.open(CACHE);
let web = fetch(request.clone()).then(res => {
return cache.put(request, res).then(() => {
return cache.match(request);
})
})
let matching = await cache.match(request);
return {
result: matching ? matching : web,
refresh: web
}
}
function precache() {
return caches.open(CACHE).then(function (cache) {
return cache.addAll(precacheFiles);
});
}

View File

@ -1,14 +1,12 @@
import { h } from "preact"; import { h } from "preact";
import "./add_button.scss"; import "./add_button.scss";
import { Plus } from "preact-feather"; import Plus from "feather-icons/dist/icons/plus.svg";
export default function AddButton({ onClick }: { onClick: () => void }) { export default function AddButton({ onClick }: { onClick: () => void }) {
return ( return <button
<button
title="add button" title="add button"
class="fab btn-primary" class="fab btn-primary"
onClick={() => onClick()} onClick={() => onClick()}
> >
<Plus width={undefined} height={undefined} /> <Plus width={undefined} height={undefined} />
</button> </button>
);
} }

View File

@ -1,3 +0,0 @@
.CodeMirror {
height: auto;
}

View File

@ -1,44 +0,0 @@
import { h } from "preact";
import { useEffect, useRef } from "preact/hooks";
import * as CM from "codemirror";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/base16-dark.css";
import "./CodeMirror.scss";
import Theme from "../theme";
interface ICodeMirrorProps {
value: string;
onChange: (value: string) => void;
onSave?: (value: string) => void;
onClose?: () => void;
}
export default function CodeMirror(props: ICodeMirrorProps) {
const ref = useRef<HTMLTextAreaElement>();
useEffect(() => {
const instance = CM.fromTextArea(ref.current, {
value: props.value,
mode: "markdown",
lineNumbers: true,
lineWrapping: true,
theme: Theme.isDark.value ? "base16-dark" : "default",
viewportMargin: Infinity,
// extraKeys: {
// "Ctrl-S": (cm) => {
// const val = cm.getValue();
// props?.onSave(val);
// },
// Esc: props.onClose,
// },
});
if (props.value) instance.setValue(props.value);
instance.on("change", () => props?.onChange(instance.getValue()));
return () => {};
}, [ref]);
return <textarea ref={ref}></textarea>;
}

View File

@ -1,6 +1,6 @@
import { h, Component } from "preact"; import { h, Component } from "preact";
import Notes from "../notes"; import Notes from "../notes";
import { RefreshCw as Refresh } from "preact-feather"; import Refresh from "feather-icons/dist/icons/refresh-cw.svg";
import "./footer.scss"; import "./footer.scss";
import Notifications from "../notifications"; import Notifications from "../notifications";

View File

@ -1,15 +1,13 @@
import { Observable } from "@hibas123/utils"; import { Observable } from "@hibas123/utils";
import { h, Component } from "preact"; import { h, Component } from "preact";
import { X } from "preact-feather"; import CloseIcon from "feather-icons/dist/icons/x.svg";
export default abstract class Modal<T> { export default abstract class Modal<T> {
// Static // Static
private static modalObservableServer = new Observable<{ private static modalObservableServer = new Observable<{ modal: Modal<any>, close: boolean }>();
modal: Modal<any>;
close: boolean;
}>();
public static modalObservable = Modal.modalObservableServer.getPublicApi(); public static modalObservable = Modal.modalObservableServer.getPublicApi();
protected abstract title: string; protected abstract title: string;
// Private // Private
@ -19,8 +17,10 @@ export default abstract class Modal<T> {
// Protected // Protected
protected result(value: T | null) { protected result(value: T | null) {
if (this.closeOnResult) this.close(); if (this.closeOnResult)
if (this.onResult) this.onResult(value); this.close()
if (this.onResult)
this.onResult(value);
} }
//Public //Public
@ -41,7 +41,7 @@ export default abstract class Modal<T> {
public async getResult(close = true) { public async getResult(close = true) {
this.closeOnResult = close; this.closeOnResult = close;
this.show(false); this.show(false);
return new Promise<T | null>((yes) => (this.onResult = yes)); return new Promise<T | null>((yes) => this.onResult = yes);
} }
public close() { public close() {
@ -50,68 +50,52 @@ export default abstract class Modal<T> {
public abstract getComponent(): JSX.Element; public abstract getComponent(): JSX.Element;
public static BaseModal = class BaseModal<T> extends Component< public static BaseModal = class BaseModal<T> extends Component<{ modal: Modal<T> }, {}> {
{ modal: Modal<T> },
{}
> {
render() { render() {
return ( return <div class="modal-container" onClick={(evt) => {
<div
class="modal-container"
onClick={(evt) => {
let path = evt.composedPath(); let path = evt.composedPath();
if ( if (!path.find(e => {
!path.find((e) => { let s = (e as Element);
let s = e as Element;
return s.id === "ModalContent"; return s.id === "ModalContent";
}) })) {
) { this.props.modal.result(null)
this.props.modal.result(null);
} }
}} }} onKeyDown={evt => {
onKeyDown={(evt) => {
if (evt.keyCode === 27) { if (evt.keyCode === 27) {
this.props.modal.result(null); this.props.modal.result(null)
} }
}} }}>
>
<div id="ModalContent" class="modal" > <div id="ModalContent" class="modal" >
<div class="modal-title" style=""> <div class="modal-title" style="">
<h3>{this.props.modal.title}</h3> <h3>{this.props.modal.title}</h3>
{/* <div> */} {/* <div> */}
{!this.props.modal.noClose ? ( {!this.props.modal.noClose ?
<X <CloseIcon onClick={() => this.props.modal.result(null)} width={undefined} height={undefined} />
onClick={() => this.props.modal.result(null)} : undefined
width={undefined} }
height={undefined}
/>
) : undefined}
{/* </div> */} {/* </div> */}
</div> </div>
{this.props.children} {this.props.children}
</div> </div>
</div> </div>
);
} }
};
} }
export class ModalComponent extends Component< }
{},
{ modal: Modal<any> | undefined; component: JSX.Element | undefined } export class ModalComponent extends Component<{}, { modal: Modal<any> | undefined, component: JSX.Element | undefined }>{
> {
constructor(props) { constructor(props) {
super(props); super(props);
this.onModal = this.onModal.bind(this); this.onModal = this.onModal.bind(this);
} }
onModal({ modal, close }: { modal: Modal<any>; close: boolean }) { onModal({ modal, close }: { modal: Modal<any>, close: boolean }) {
if (!close && this.state.modal !== modal) { if (!close && this.state.modal !== modal) {
this.setState({ modal: modal, component: modal.getComponent() }); this.setState({ modal: modal, component: modal.getComponent() })
} else { }
if (this.state.modal === modal && close) else {
// Only close if the same if (this.state.modal === modal && close) // Only close if the same
this.setState({ modal: undefined, component: undefined }); this.setState({ modal: undefined, component: undefined })
} }
} }
@ -124,6 +108,8 @@ export class ModalComponent extends Component<
} }
render() { render() {
return <div>{this.state.component}</div>; return <div>
{this.state.component}
</div>
} }
} }

View File

@ -2,71 +2,36 @@ import { h } from "preact";
import { Page } from "../../../page"; import { Page } from "../../../page";
import Theme, { ThemeStates } from "../../../theme"; import Theme, { ThemeStates } from "../../../theme";
import Navigation from "../../../navigation"; import Navigation from "../../../navigation";
import { ArrowLeft } from "preact-feather"; import ArrowLeft from "feather-icons/dist/icons/arrow-left.svg";
export default class SettingsPage extends Page< export default class SettingsPage extends Page<{ state: any }, { vault: string }> {
{ state: any }, componentWillMount() {
{ vault: string }
> { }
componentWillMount() {}
render() { render() {
let active = Theme.active(); let active = Theme.active();
return ( return <div>
<div>
<header class="header"> <header class="header">
<a class="header-icon-button" onClick={() => history.back()}> <a class="header-icon-button" onClick={() => history.back()}><ArrowLeft height={undefined} width={undefined} /></a>
<ArrowLeft height={undefined} width={undefined} /> <h3 style="display:inline" class="header-title" onClick={() => Navigation.setPage("/")}>Settings</h3>
</a>
<h3
style="display:inline"
class="header-title"
onClick={() => Navigation.setPage("/")}
>
Settings
</h3>
<span></span> <span></span>
</header> </header>
<div class="container"> <div class="container">
<div className="input-group"> <div className="input-group">
<label>Select Theme: </label> <label>Select Theme: </label>
<select <select class="inp" onChange={(ev) => Theme.change(Number((ev.target as HTMLSelectElement).value))}>
class="inp"
onChange={(ev) =>
Theme.change(
Number((ev.target as HTMLSelectElement).value)
)
}
>
{Object.keys(ThemeStates) {Object.keys(ThemeStates)
.filter((e) => Number.isNaN(Number(e))) .filter(e => Number.isNaN(Number(e)))
.map((e) => ( .map(e => <option selected={ThemeStates[e] === active} value={ThemeStates[e]}>{e.charAt(0).toUpperCase() + e.slice(1).toLowerCase()}</option>)}
<option
selected={ThemeStates[e] === active}
value={ThemeStates[e]}
>
{e.charAt(0).toUpperCase() +
e.slice(1).toLowerCase()}
</option>
))}
{/* <option value={ThemeStates.AUTO}>Auto</option> {/* <option value={ThemeStates.AUTO}>Auto</option>
<option value={ThemeStates.LIGHT}>Light</option> <option value={ThemeStates.LIGHT}>Light</option>
<option value={ThemeStates.DARK}>Dark</option> */} <option value={ThemeStates.DARK}>Dark</option> */}
</select> </select>
</div> </div>
{/* <button class="btn" onClick={() => Theme.toggle()}>Toggle Dark Mode</button> */} {/* <button class="btn" onClick={() => Theme.toggle()}>Toggle Dark Mode</button> */}
<button <button class="btn" onClick={() => window.navigator.serviceWorker.controller.postMessage("message")}>Clear cache</button>
class="btn"
onClick={() =>
window.navigator.serviceWorker.controller.postMessage(
"message"
)
}
>
Clear cache
</button>
</div> </div>
</div> </div >;
);
} }
} }

View File

@ -1,209 +0,0 @@
import { h, Component } from "preact";
import { IVault, ViewNote } from "../../../notes";
import { Trash2 as Trash, X, Save } from "preact-feather";
import Navigation from "../../../navigation";
import { YesNoModal } from "../../modals/YesNoModal";
import Notifications, { MessageType } from "../../../notifications";
const minRows = 3;
export default class EntryComponent extends Component<
{ vault: Promise<IVault>; id: string | undefined; note: string | undefined },
{ title: string; changed: boolean }
> {
private text: string = "";
private vault: IVault;
// private lineHeight: number = 24;
private note: ViewNote;
private rows: number = minRows;
private skip_save: boolean = false;
private inputElm: HTMLInputElement;
constructor(props) {
super(props);
this.state = { changed: false, title: "" };
this.textAreaChange = this.textAreaChange.bind(this);
this.textAreaKeyPress = this.textAreaKeyPress.bind(this);
}
private toVault() {
history.back();
}
componentDidMount() {
this.inputElm.focus();
}
async componentWillMount() {
this.text = "";
this.vault = undefined;
this.note = undefined;
this.rows = minRows;
this.skip_save = false;
this.setState({ changed: false, title: "" });
try {
this.skip_save = false;
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 {
note = this.vault.newNote();
if (this.props.note) {
note.__value = this.props.note;
changed = true;
}
}
if (!note) {
Notifications.sendNotification(
"Note not found!",
MessageType.ERROR
);
} else {
this.note = note;
this.text = note.__value;
let rows = this.getRows(this.text);
if (rows !== this.rows) {
this.rows = rows;
}
let [title] = this.text.split("\n", 1);
this.setState({ title, changed });
}
} catch (err) {
Notifications.sendError(err);
}
}
private async save() {
try {
if (this.state.changed) {
this.note.__value = this.text;
await this.vault.saveNote(this.note);
this.setState({ changed: false });
}
} catch (err) {
Notifications.sendError(err);
}
}
componentWillUnmount() {
if (!this.skip_save) this.save();
}
strToNr(value: string) {
let match = value.match(/\d/g);
return Number(match.join(""));
}
getRows(value: string) {
const lines = (value.match(/\r?\n/g) || "").length + 1;
return Math.max(lines + 1, minRows);
}
textAreaChange(evt: KeyboardEvent) {
if (evt.keyCode === 17 || evt.keyCode === 27) return; //No character, so not relevant for this function
if (!this.state.changed && this.textAreaKeyPress(evt))
this.setState({ changed: true });
let target = evt.target as HTMLTextAreaElement;
let value = target.value;
this.text = value;
let [title] = value.split("\n", 1);
if (title !== this.state.title) this.setState({ title });
let rows = this.getRows(value);
if (rows !== this.rows) {
target.rows = rows;
this.rows = rows;
}
}
async exitHandler() {
if (this.state.changed) {
let modal = new YesNoModal("Really want to quit?");
let res = await modal.getResult();
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) {
evt.preventDefault();
this.save();
return false;
} else if (evt.keyCode === 27) {
evt.preventDefault();
this.exitHandler();
return false;
}
return true;
}
render() {
const save_handler = async () => {
await this.save();
Navigation.setPage(
"/vault",
{ id: this.vault.id },
{ entry: "false" },
true
);
this.toVault();
};
const delete_handler = async () => {
await this.vault.deleteNote(this.props.id);
this.toVault();
};
console.log("Rerender");
return (
<div>
<header class="header">
<a class="header-icon-button" onClick={() => this.exitHandler()}>
<X height={undefined} width={undefined} />
</a>
{this.state.changed ? (
<a
class="header-icon-button"
style="margin-left: 0.5em;"
onClick={() => save_handler()}
>
<Save height={undefined} width={undefined} />
</a>
) : undefined}
<h3 style="display:inline" class="button header-title">
{this.state.title}
</h3>
<a class="header-icon-button" onClick={() => delete_handler()}>
<Trash height={undefined} width={undefined} />
</a>
</header>
<div class="container">
<textarea
value={this.text}
rows={this.rows}
class="inp"
style="width:100%;"
onInput={this.textAreaChange}
onKeyDown={this.textAreaKeyPress}
onChange={this.textAreaChange}
ref={(elm) => (this.inputElm = elm)}
/>
</div>
</div>
);
}
}

View File

@ -1,132 +1,188 @@
import { h, Component } from "preact"; import { h, Component } from "preact"
import { IVault, ViewNote } from "../../../notes"; import { IVault, ViewNote } from "../../../notes";
import { Trash2 as Trash, X, Save } from "preact-feather"; 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"
import Navigation from "../../../navigation"; import Navigation from "../../../navigation";
import { YesNoModal } from "../../modals/YesNoModal"; import { YesNoModal } from "../../modals/YesNoModal";
import Notifications, { MessageType } from "../../../notifications"; import Notifications, { MessageType } from "../../../notifications";
import CodeMirror from "../../CodeMirror";
import { useEffect, useMemo, useState } from "preact/hooks";
import { usePromise } from "../../../hooks";
interface IEntryProps { const minRows = 3;
vault: IVault; export default class EntryComponent extends Component<{ vault: Promise<IVault>, id: string | undefined, note: string | undefined }, { title: string, changed: boolean }> {
id?: string; private text: string = "";
note?: string; private vault: IVault;
// private lineHeight: number = 24;
private note: ViewNote;
private rows: number = minRows;
private skip_save: boolean = false;
private inputElm: HTMLInputElement;
constructor(props) {
super(props);
this.state = { changed: false, title: "" };
this.textAreaChange = this.textAreaChange.bind(this);
this.textAreaKeyPress = this.textAreaKeyPress.bind(this);
} }
export default function Entry(props: IEntryProps) { private toVault() {
const [changed, setChanged] = useState(false); history.back();
}
const [text, setText] = useState(""); componentDidMount() {
const title = useMemo(() => text?.split("\n", 1)[0], [text]); this.inputElm.focus();
}
const [loading, error, note] = usePromise(async () => { async componentWillMount() {
this.text = "";
this.vault = undefined;
this.note = undefined;
this.rows = minRows;
this.skip_save = false;
this.setState({ changed: false, title: "" });
try {
this.skip_save = false;
this.vault = await this.props.vault;
let note: ViewNote; let note: ViewNote;
if (props.id) { let changed = false;
note = await props.vault.getNote(props.id); if (this.props.id)
} else { note = await this.vault.getNote(this.props.id)
note = props.vault.newNote(); else {
if (props.note) { note = this.vault.newNote();
note.__value = props.note; if (this.props.note) {
setChanged(true); note.__value = this.props.note;
changed = true;
} }
} }
if (!note) { if (!note) {
Notifications.sendNotification("Note not found!", MessageType.ERROR); Notifications.sendNotification("Note not found!", MessageType.ERROR);
history.back();
} else { } else {
setText(note.__value); this.note = note;
this.text = note.__value;
let rows = this.getRows(this.text);
if (rows !== this.rows) {
this.rows = rows;
} }
let [title] = this.text.split("\n", 1);
return note; this.setState({ title, changed })
}, [props.vault, props.id]);
if (loading) {
return <div>Loading entry</div>;
} else {
const save = async () => {
try {
if (changed) {
note.__value = text;
await props.vault.saveNote(note);
setChanged(false);
} }
} catch (err) { } catch (err) {
Notifications.sendError(err); Notifications.sendError(err);
} }
}; }
const del = async () => { private async save() {
await props.vault.deleteNote(props.id); try {
history.back(); if (this.state.changed) {
}; this.note.__value = this.text;
await this.vault.saveNote(this.note);
this.setState({ changed: false })
}
} catch (err) {
Notifications.sendError(err);
}
}
const close = async () => { componentWillUnmount() {
if (changed) { if (!this.skip_save)
this.save()
}
strToNr(value: string) {
let match = value.match(/\d/g)
return Number(match.join(""))
}
getRows(value: string) {
const lines = (value.match(/\r?\n/g) || '').length + 1
return Math.max(lines + 1, minRows);
}
textAreaChange(evt: KeyboardEvent) {
if (evt.keyCode === 17 || evt.keyCode === 27) return; //No character, so not relevant for this function
if (!this.state.changed && this.textAreaKeyPress(evt)) this.setState({ changed: true })
let target = (evt.target as HTMLTextAreaElement)
let value = target.value;
this.text = value;
let [title] = value.split("\n", 1);
if (title !== this.state.title)
this.setState({ title });
let rows = this.getRows(value);
if (rows !== this.rows) {
target.rows = rows;
this.rows = rows;
}
}
async exitHandler() {
if (this.state.changed) {
let modal = new YesNoModal("Really want to quit?"); let modal = new YesNoModal("Really want to quit?");
let res = await modal.getResult(); let res = await modal.getResult();
if (!res) return; if (res === true) {
this.skip_save = true;
this.toVault();
}
} else
this.toVault()
} }
history.back();
};
// TODO: Add warning on possibly unwanted exit textAreaKeyPress(evt: KeyboardEvent) {
useEffect(() => { console.log(evt);
const keyevent = (evt: KeyboardEvent) => { if ((evt.keyCode === 83 || evt.keyCode === 13) && evt.ctrlKey) {
if (evt.key === "s" && evt.ctrlKey) { evt.preventDefault()
evt.preventDefault(); this.save();
save();
return false; return false;
} else if (evt.key === "Escape") { }
else if (evt.keyCode === 27) {
evt.preventDefault(); evt.preventDefault();
close(); this.exitHandler();
return false; return false;
} }
return true; return true;
}; }
document.addEventListener("keydown", keyevent); render() {
return () => { const save_handler = async () => {
document.removeEventListener("keydown", keyevent); await this.save()
}; Navigation.setPage("/vault", { id: this.vault.id }, { entry: "false" }, true);
}); this.toVault()
}
return ( const delete_handler = async () => {
<div> await this.vault.deleteNote(this.props.id);
<header class="header" style="margin-bottom: 0;"> this.toVault()
<a class="header-icon-button" onClick={close}> }
<X height={undefined} width={undefined} />
</a> console.log("Rerender")
{changed && (
<a return <div>
class="header-icon-button" <header class="header">
style="margin-left: 0.5em;" <a class="header-icon-button" onClick={() => this.exitHandler()}><X height={undefined} width={undefined} /></a>
onClick={save} {this.state.changed ? <a class="header-icon-button" style="margin-left: 0.5em;" onClick={() => save_handler()}><Save height={undefined} width={undefined} /></a> : undefined}
> <h3 style="display:inline" class="button header-title">{this.state.title}</h3>
<Save height={undefined} width={undefined} /> <a class="header-icon-button" onClick={() => delete_handler()}><Trash height={undefined} width={undefined} /></a>
</a>
)}
<h3 style="display:inline" class="button header-title">
{title}
</h3>
<a class="header-icon-button" onClick={del}>
<Trash height={undefined} width={undefined} />
</a>
</header> </header>
<div class="container" style="padding: 0"> <div class="container">
<CodeMirror <textarea
value={text} value={this.text}
onChange={(value) => { rows={this.rows}
setChanged(true); class="inp"
setText(value); style="width:100%;"
}} onInput={this.textAreaChange}
// onSave={save} onKeyDown={this.textAreaKeyPress}
// onClose={close} onChange={this.textAreaChange}
ref={elm => this.inputElm = elm}
/> />
</div> </div>
</div> </div>
);
} }
} }

View File

@ -1,25 +1,24 @@
import { h, Component } from "preact"; import { h, Component } from "preact"
import Notes, { 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, Search } from "preact-feather"; import ArrowLeft from "feather-icons/dist/icons/arrow-left.svg"
import Search from "feather-icons/dist/icons/search.svg"
import ContextMenu from "../../context"; import ContextMenu from "../../context";
import Notifications from "../../../notifications"; import Notifications from "../../../notifications";
import { Observable, Lock } from "@hibas123/utils"; import { Observable, Lock } from "@hibas123/utils";
export default class EntryList extends Component<
{ vault: IVault }, export default class EntryList extends Component<{ vault: Promise<IVault> }, { notes: BaseNote[], context: JSX.Element | undefined }> {
{ notes: BaseNote[]; context: h.JSX.Element | undefined }
> {
rawNotes: BaseNote[]; rawNotes: BaseNote[];
private searchObservableServer = new Observable<void>(1000); private searchObservableServer = new Observable<void>(1000);
private searchObservable = this.searchObservableServer.getPublicApi(); private searchObservable = this.searchObservableServer.getPublicApi();
constructor(props) { constructor(props) {
super(props); super(props)
console.log("Creating new Instance of EntryList"); console.log("Creating new Instance of EntryList")
this.state = { notes: [], context: undefined }; this.state = { notes: [], context: undefined }
this.onDragOver = this.onDragOver.bind(this); this.onDragOver = this.onDragOver.bind(this);
this.onDrop = this.onDrop.bind(this); this.onDrop = this.onDrop.bind(this);
this.reloadNotes = this.reloadNotes.bind(this); this.reloadNotes = this.reloadNotes.bind(this);
@ -28,13 +27,14 @@ export default class EntryList extends Component<
vault: IVault; vault: IVault;
async reloadNotes(s?: boolean) { async reloadNotes(s?: boolean) {
if (s) return; if (s)
return;
this.rawNotes = await this.vault.getAllNotes(); this.rawNotes = await this.vault.getAllNotes();
await this.applySearch(true); await this.applySearch(true);
} }
async componentWillMount() { async componentWillMount() {
this.vault = this.props.vault; this.vault = await this.props.vault;
this.reloadNotes(); this.reloadNotes();
document.body.addEventListener("dragover", this.onDragOver); document.body.addEventListener("dragover", this.onDragOver);
document.body.addEventListener("drop", this.onDrop); document.body.addEventListener("drop", this.onDrop);
@ -64,74 +64,59 @@ export default class EntryList extends Component<
const close = () => { const close = () => {
document.documentElement.removeEventListener("click", close); document.documentElement.removeEventListener("click", close);
this.setState({ context: undefined }); this.setState({ context: undefined });
}; }
document.documentElement.addEventListener("click", close); document.documentElement.addEventListener("click", close);
const shareNote = async () => { const shareNote = async () => {
let nav = window.navigator as any; let nav = window.navigator as any;
if (nav.share !== undefined) { if (nav.share !== undefined) {
let vnote = await this.vault.getNote(note._id); let vnote = await this.vault.getNote(note._id);
nav.share({ nav.share({ title: vnote.preview.split("\n")[0], text: vnote.__value })
title: vnote.preview.split("\n")[0], .then(() => console.log('Successful share'))
text: vnote.__value, .catch(error => {
}) console.error('Error sharing:', error)
.then(() => console.log("Successful share")) Notifications.sendError(error)
.catch((error) => {
console.error("Error sharing:", error);
Notifications.sendError(error);
}); });
} else { } else {
Notifications.sendError("Sharing not possible on this device"); Notifications.sendError("Sharing not possible on this device")
}
} }
};
const deleteNote = async () => { const deleteNote = async () => {
this.vault this.vault.deleteNote(note._id).then(() => {
.deleteNote(note._id) Notifications.sendSuccess("Deleted")
.then(() => { this.rawNotes = this.rawNotes.filter(e => e._id !== note._id);
Notifications.sendSuccess("Deleted"); this.setState({ notes: this.state.notes.filter(e => e._id !== note._id) });
this.rawNotes = this.rawNotes.filter((e) => e._id !== note._id); }).catch(err => {
this.setState({
notes: this.state.notes.filter((e) => e._id !== note._id),
});
})
.catch((err) => {
Notifications.sendError(err); Notifications.sendError(err);
}); })
}; }
const copyNote = async () => { const copyNote = async () => {
this.vault.getNote(note._id).then((note) => { this.vault.getNote(note._id).then(note => {
const el = document.createElement("textarea"); const el = document.createElement('textarea');
el.value = note.__value; el.value = note.__value;
document.body.appendChild(el); document.body.appendChild(el);
el.select(); el.select();
document.execCommand("copy"); document.execCommand('copy');
document.body.removeChild(el); document.body.removeChild(el);
Notifications.sendSuccess("Copied"); Notifications.sendSuccess("Copied");
}); })
}; }
let share; let share;
if ((window.navigator as any).share) { if ((window.navigator as any).share) {
share = ( share = <button class="btn" onClick={shareNote}>
<button class="btn" onClick={shareNote}>
share share
</button> </button>
);
} }
let context = ( let context = <ContextMenu event={evt} >
<ContextMenu event={evt}>
{share} {share}
<button class="btn" onClick={deleteNote}> <button class="btn" onClick={deleteNote}>delete</button>
delete <button class="btn" onClick={copyNote}>copy</button>
</button>
<button class="btn" onClick={copyNote}>
copy
</button>
</ContextMenu> </ContextMenu>
);
this.setState({ context }); this.setState({ context });
return false; return false;
@ -146,69 +131,57 @@ export default class EntryList extends Component<
if (item.kind === "file") { if (item.kind === "file") {
let file = item.getAsFile(); let file = item.getAsFile();
if (file.type !== "application/json") { if (file.type !== "application/json") {
Notifications.sendError("Invalid File Type!!!"); Notifications.sendError("Invalid File Type!!!")
} else { } else {
try { try {
let data = await new Promise<string>((yes, no) => { let data = await new Promise<string>((yes, no) => {
let fr = new FileReader(); let fr = new FileReader()
fr.onload = (ev) => { fr.onload = (ev) => {
yes((ev.target as any).result); yes((ev.target as any).result);
}; }
fr.onerror = no; fr.onerror = no;
fr.readAsText(file); fr.readAsText(file);
}); })
let parsed = JSON.parse(data); let parsed = JSON.parse(data);
let c = new Error("Could not parse!"); let c = new Error("Could not parse!");
let notes: { content: string; time: Date }[] = null; let notes: { content: string, time: Date }[] = null;
if (Array.isArray(parsed)) { if (Array.isArray(parsed)) { // Could be from SecureNotes 1
// Could be from SecureNotes 1 notes = parsed.map(elm => {
notes = parsed.map((elm) => { if (typeof elm.message !== "string" || typeof elm.time !== "string") {
if (
typeof elm.message !== "string" ||
typeof elm.time !== "string"
) {
throw c; throw c;
} }
return { return {
content: elm.message, content: elm.message,
time: new Date(elm.time), time: new Date(elm.time)
}; }
}); })
} else if (parsed.version) { } else if (parsed.version) { // Could be from SecureNotes 2
// Could be from SecureNotes 2 if (parsed.version === 1) { //Could be from SecureNotes 2 version 1
if (parsed.version === 1) { notes = (parsed.notes as any[]).map(elm => {
//Could be from SecureNotes 2 version 1 if (typeof elm.content !== "string" || typeof elm.time !== "string") {
notes = (parsed.notes as any[]).map((elm) => {
if (
typeof elm.content !== "string" ||
typeof elm.time !== "string"
) {
throw c; throw c;
} }
return { return {
content: elm.content, content: elm.content,
time: new Date(elm.time), time: new Date(elm.time)
}; }
}); })
} else { } else {
throw c; throw c;
} }
} else throw c; } else
throw c;
await Promise.all( await Promise.all(notes.map(n => {
notes.map((n) => {
let note = this.vault.newNote(); let note = this.vault.newNote();
note.__value = n.content; note.__value = n.content;
return this.vault.saveNote(note, n.time); return this.vault.saveNote(note, n.time);
}) }));
);
await this.reloadNotes(); await this.reloadNotes();
Notifications.sendSuccess( Notifications.sendSuccess(`Imported ${notes.length} notes!`);
`Imported ${notes.length} notes!`
);
} catch (err) { } catch (err) {
Notifications.sendError("Cannot read File!"); Notifications.sendError("Cannot read File!")
console.error(err); console.error(err);
} }
} }
@ -225,7 +198,8 @@ export default class EntryList extends Component<
return; return;
} }
if (this.searchLock.locked) return; if (this.searchLock.locked)
return;
const lock = await this.searchLock.getLock(); const lock = await this.searchLock.getLock();
console.time("SearchOP"); console.time("SearchOP");
@ -239,7 +213,7 @@ export default class EntryList extends Component<
return parts.every(function (el) { return parts.every(function (el) {
return note.preview.toLowerCase().indexOf(el) > -1; return note.preview.toLowerCase().indexOf(el) > -1;
}); });
}; }
let elements: BaseNote[]; let elements: BaseNote[];
if (!force && this.oldSearch && search.startsWith(this.oldSearch)) { if (!force && this.oldSearch && search.startsWith(this.oldSearch)) {
@ -248,32 +222,29 @@ export default class EntryList extends Component<
elements = [...this.rawNotes]; elements = [...this.rawNotes];
} }
await new Promise((yes) => {
await new Promise(yes => {
const idle = () => { const idle = () => {
window.requestIdleCallback( window.requestIdleCallback(deadline => {
(deadline) => {
let invTR = deadline.timeRemaining() <= 0; let invTR = deadline.timeRemaining() <= 0;
while ( while ((deadline.timeRemaining() > 0 || invTR) && elements.length > 0) {
(deadline.timeRemaining() > 0 || invTR) &&
elements.length > 0
) {
let element = elements.shift(); let element = elements.shift();
if (match(element)) { if (match(element)) {
notes.push(element); notes.push(element);
} }
} }
if (elements.length > 0) idle(); if (elements.length > 0)
else yes();
},
{ timeout: 100 }
);
};
idle(); idle();
}); else
yes();
}, { timeout: 100 });
}
idle();
})
// notes = elements.filter(note => match(note)); // notes = elements.filter(note => match(note));
} }
await new Promise((yes) => this.setState({ notes }, yes)); await new Promise(yes => this.setState({ notes }, yes));
this.oldSearch = search; this.oldSearch = search;
lock.release(); lock.release();
console.timeEnd("SearchOP"); console.timeEnd("SearchOP");
@ -289,67 +260,38 @@ export default class EntryList extends Component<
searchInput: HTMLInputElement; searchInput: HTMLInputElement;
render() { render() {
const open_entry = (id: string | null) => { const open_entry = (id: string | null) => {
Navigation.setPage( Navigation.setPage("/vault", { id: this.vault.id }, { id, entry: "true" })
"/vault", }
{ id: this.vault.id },
{ id, entry: "true" }
);
};
return ( return <div>
<div>
{this.state.context} {this.state.context}
<header class="header"> <header class="header">
<a class="header-icon-button" onClick={() => history.back()}> <a class="header-icon-button" onClick={() => history.back()}><ArrowLeft height={undefined} width={undefined} /></a>
<ArrowLeft height={undefined} width={undefined} /> <h3 style="display:inline" class="header-title" onClick={() => Navigation.setPage("/")}>{this.vault ? this.vault.name : ""}</h3>
</a>
<h3
style="display:inline"
class="header-title"
onClick={() => Navigation.setPage("/")}
>
{this.vault ? this.vault.name : ""}
</h3>
<span></span> <span></span>
{/* <a class="button header_icon_button"><MoreVertival height={undefined} width={undefined} /></a> */} {/* <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">
<div style="display:flex;"> <div style="display:flex;">
<input <input class="inp" type="text" style="width: 100%; height: 40px;" onKeyUp={this.searchChanged} ref={elm => this.searchInput = elm} />
class="inp" <button class="btn btn-primary" style="padding: 5px 10px; margin: 0; height: 40px; width: 40px;">
type="text"
style="width: 100%; height: 40px;"
onKeyUp={this.searchChanged}
ref={(elm) => (this.searchInput = elm)}
/>
<button
class="btn btn-primary"
style="padding: 5px 10px; margin: 0; height: 40px; width: 40px;"
>
<Search /> <Search />
</button> </button>
</div> </div>
<div class="vault-list" style="margin-top: 1rem;"> <div class="vault-list" style="margin-top: 1rem;">
{this.state.notes.map((note) => { {this.state.notes.map(note => {
let [first, second] = note.preview.split("\n", 2); let [first, second] = note.preview.split("\n", 2);
return ( return <div class="card vault-vault" onContextMenu={evt => this.onContext(evt, note)} onClick={() => {
<div open_entry(note._id)
class="card vault-vault" }}>
onContextMenu={(evt) => this.onContext(evt, note)}
onClick={() => {
open_entry(note._id);
}}
>
<div>{first}</div> <div>{first}</div>
<div>{second}</div> <div>{second}</div>
</div> </div>
);
})} })}
</div> </div>
</div> </div>
</div> </div>;
);
} }
} }

View File

@ -1,55 +1,47 @@
import { h } from "preact"; import { h } from "preact"
import { Page } from "../../../page"; import { Page } from "../../../page";
import Notes, { IVault, BaseNote } from "../../../notes"; import Notes, { IVault, BaseNote } from "../../../notes";
import Navigation from "../../../navigation"; import Navigation from "../../../navigation";
import EntryComponent from "./Entry"; import EntryComponent from "./Entry";
import EntryList from "./EntryList"; import EntryList from "./EntryList";
import "./vault.scss"; import "./vault.scss"
import { useState } from "preact/hooks";
import { usePromise } from "../../../hooks";
export interface VaultProps { export interface VaultProps {
state: { id: string }; state: { id: string };
hidden: { entry: string; id: string; note: string }; hidden: { entry: string, id: string, note: string };
} }
export default function VaultPage(props: VaultProps) { export default class VaultPage extends Page<VaultProps, { entries: BaseNote[] }> {
console.log("Vault page"); vault: Promise<IVault>
const [loading, error, vault] = usePromise( constructor(props: VaultProps) {
() => super(props);
Notes.getVault( this.state = { entries: [] };
this.props.state.id, }
Notes.getVaultKey(this.props.state.id)
),
[props.state.id]
);
if (vault) { async componentWillMount() {
window.debug.activeVault = vault; this.vault = Notes.getVault(this.props.state.id, Notes.getVaultKey(this.props.state.id))
this.vault.then(vlt => {
window.debug.activeVault = vlt;
window.debug.createNotes = (cnt = 10) => { window.debug.createNotes = (cnt = 10) => {
for (let i = 0; i < cnt; i++) { for (let i = 0; i < cnt; i++) {
let nt = vault.newNote(); let nt = vlt.newNote();
nt.__value = `Random Note ${i}\ With some Content ${i}`; nt.__value = `Random Note ${i}\ With some Content ${i}`;
vault.saveNote(nt); vlt.saveNote(nt);
} }
}; }
})
this.vault.catch(err => {
Navigation.setPage("/")
});
} }
if (loading) return <div>Loading Vault</div>; render() {
// Maybe return loading animation or so if (this.props.hidden && this.props.hidden.entry === "true") {
else if (error) return <div>{error.message}</div>; return <EntryComponent vault={this.vault} id={this.props.hidden.id} note={this.props.hidden.note} />
else {
if (props.hidden?.entry) {
return (
<EntryComponent
vault={vault}
id={props.hidden.id}
note={props.hidden.note}
/>
);
} else { } else {
return <EntryList vault={vault} />; return <EntryList vault={this.vault} />
} }
} }
} }

View File

@ -1,8 +1,10 @@
import { h } from "preact"; import { h } from "preact"
import { Page } from "../../../page"; import { Page } from "../../../page";
import Notes, { VaultList } from "../../../notes"; import Notes, { VaultList } from "../../../notes";
import "./vaults.scss"; import "./vaults.scss"
import { Lock, Unlock, Settings } from "preact-feather"; import Lock from "feather-icons/dist/icons/lock.svg";
import Unlock from "feather-icons/dist/icons/unlock.svg";
import Settings from "feather-icons/dist/icons/settings.svg"
import Navigation from "../../../navigation"; import Navigation from "../../../navigation";
import { InputModal } from "../../modals/InputModal"; import { InputModal } from "../../modals/InputModal";
import { YesNoModal } from "../../modals/YesNoModal"; import { YesNoModal } from "../../modals/YesNoModal";
@ -16,14 +18,7 @@ export interface VaultsProps {
onSelected?: (vaultid: string) => void; onSelected?: (vaultid: string) => void;
} }
export default class VaultsPage extends Page< export default class VaultsPage extends Page<VaultsProps, { vaults: VaultList, modal: JSX.Element | undefined, context: JSX.Element | undefined }> {
VaultsProps,
{
vaults: VaultList;
modal: h.JSX.Element | undefined;
context: h.JSX.Element | undefined;
}
> {
constructor(props: VaultsProps) { constructor(props: VaultsProps) {
super(props); super(props);
this.state = { vaults: [], modal: undefined, context: undefined }; this.state = { vaults: [], modal: undefined, context: undefined };
@ -31,14 +26,15 @@ export default class VaultsPage extends Page<
} }
updateVaults(s?: boolean) { updateVaults(s?: boolean) {
if (s) return; if (s)
return new Promise((yes) => { return;
Notes.getVaults().then((vaults) => this.setState({ vaults }, yes)); return new Promise(yes => {
}); Notes.getVaults().then(vaults => this.setState({ vaults }, yes))
})
} }
componentWillMount() { componentWillMount() {
this.updateVaults(); this.updateVaults()
Notes.syncObservable.subscribe(this.updateVaults); Notes.syncObservable.subscribe(this.updateVaults);
} }
@ -46,19 +42,15 @@ export default class VaultsPage extends Page<
Notes.syncObservable.unsubscribe(this.updateVaults); Notes.syncObservable.unsubscribe(this.updateVaults);
} }
async getKey(vault: { name: string; id: string }, permanent = true) { async getKey(vault: { name: string, id: string }, permanent = true) {
let inp_mod = new InputModal( let inp_mod = new InputModal("Enter password for " + vault.name, "Password", "password");
"Enter password for " + vault.name,
"Password",
"password"
);
let key = undefined; let key = undefined;
while (true) { while (true) {
// inp_mod.show(); // inp_mod.show();
let value = await inp_mod.getResult(false); let value = await inp_mod.getResult(false);
if (value === null) { if (value === null) {
console.log("Value is null"); console.log("Value is null")
inp_mod.close(); inp_mod.close()
return false; return false;
} else { } else {
key = Notes.passwordToKey(value); key = Notes.passwordToKey(value);
@ -66,11 +58,11 @@ export default class VaultsPage extends Page<
await Notes.getVault(vault.id, key); await Notes.getVault(vault.id, key);
break; break;
} catch (err) { } catch (err) {
Notifications.sendError("Invalid password!"); Notifications.sendError("Invalid password!")
} }
} }
} }
inp_mod.close(); inp_mod.close()
let perm = false; let perm = false;
if (permanent) { if (permanent) {
@ -87,24 +79,27 @@ export default class VaultsPage extends Page<
return true; return true;
} }
async openVault(vault: { name: string; encrypted: boolean; id: string }) { async openVault(vault: { name: string, encrypted: boolean, id: string }) {
const action = () => { const action = () => {
if (this.props.selectVault) { if (this.props.selectVault) {
this.props.onSelected(vault.id); this.props.onSelected(vault.id);
} else { } else {
Navigation.setPage("/vault", { id: vault.id }); Navigation.setPage("/vault", { id: vault.id })
}
} }
};
if (vault.encrypted) { if (vault.encrypted) {
let key = Notes.getVaultKey(vault.id); let key = Notes.getVaultKey(vault.id);
if (key) action(); if (key)
action()
else { else {
if (await this.getKey(vault)) action(); if (await this.getKey(vault))
}
} else {
action(); action();
} }
} else {
action()
}
} }
async addButtonClick() { async addButtonClick() {
@ -118,176 +113,122 @@ export default class VaultsPage extends Page<
let password; let password;
if (encrypted) { if (encrypted) {
let password_modal = new InputModal( let password_modal = new InputModal("Enter new password", "Password", "password");
"Enter new password",
"Password",
"password"
);
password = await password_modal.getResult(); password = await password_modal.getResult();
if (password === null) return; if (password === null) return;
} }
let key; let key;
if (password) { if (password) {
key = Notes.passwordToKey(password); key = Notes.passwordToKey(password)
} }
await Notes.createVault(name, key); await Notes.createVault(name, key)
this.updateVaults(); this.updateVaults();
} }
onContext( onContext(evt: MouseEvent, vault: { name: string, encrypted: boolean, id: string }) {
evt: MouseEvent,
vault: { name: string; encrypted: boolean; id: string }
) {
evt.preventDefault(); evt.preventDefault();
evt.stopPropagation(); evt.stopPropagation();
const close = () => { const close = () => {
document.documentElement.removeEventListener("click", close); document.documentElement.removeEventListener("click", close);
this.setState({ context: undefined }); this.setState({ context: undefined });
}; }
document.documentElement.addEventListener("click", close); document.documentElement.addEventListener("click", close);
let deleteb = ( let deleteb = <button class="btn" onClick={async () => {
<button let delete_modal = new YesNoModal("Delete Vault? Cannot be undone!");
class="btn"
onClick={async () => {
let delete_modal = new YesNoModal(
"Delete Vault? Cannot be undone!"
);
let result = await delete_modal.getResult(); let result = await delete_modal.getResult();
if (result) { if (result) {
Notes.deleteVault(vault.id) Notes.deleteVault(vault.id).then(() => {
.then(() => {
this.updateVaults(); this.updateVaults();
}) }).catch(err => {
.catch((err) => { Notifications.sendError("Error deleting vault!")
Notifications.sendError("Error deleting vault!");
console.error(err); console.error(err);
}); })
} }
}} }}>
>
delete delete
</button> </button>;
);
let delete_key; let delete_key;
if (Notes.getVaultKey(vault.id)) { if (Notes.getVaultKey(vault.id)) {
delete_key = ( delete_key = <button class="btn" onClick={() => {
<button
class="btn"
onClick={() => {
Notes.forgetVaultKey(vault.id); Notes.forgetVaultKey(vault.id);
Notifications.sendSuccess("Forgot password!"); Notifications.sendSuccess("Forgot password!")
}} }}>
>
forget password forget password
</button> </button>;
);
} }
let exportb = ( let exportb = <button class="btn" onClick={async () => {
<button
class="btn"
onClick={async () => {
let key: Uint8Array; let key: Uint8Array;
if (vault.encrypted) { if (vault.encrypted) {
await this.getKey(vault, false); await this.getKey(vault, false)
key = Notes.getVaultKey(vault.id); key = Notes.getVaultKey(vault.id);
} }
let note_vault = await Notes.getVault(vault.id, key); let note_vault = await Notes.getVault(vault.id, key);
let base_notes = await note_vault.getAllNotes(); let base_notes = await note_vault.getAllNotes();
let notes = await Promise.all( let notes = await Promise.all(base_notes.map(e => {
base_notes.map((e) => {
return note_vault.getNote(e._id); return note_vault.getNote(e._id);
}) }));
);
let result = { let result =
{
version: 1, version: 1,
notes: notes.map((e) => { notes: notes.map(e => {
return { return {
content: e.__value, content: e.__value,
time: e.time, time: e.time
}; }
}), })
}; }
var dataStr = var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(result, undefined, 3));
"data:text/json;charset=utf-8," + var downloadAnchorNode = document.createElement('a');
encodeURIComponent(JSON.stringify(result, undefined, 3));
var downloadAnchorNode = document.createElement("a");
downloadAnchorNode.setAttribute("href", dataStr); downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute( downloadAnchorNode.setAttribute("download", "notes_export_" + vault.name + ".json");
"download",
"notes_export_" + vault.name + ".json"
);
document.body.appendChild(downloadAnchorNode); // required for firefox document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click(); downloadAnchorNode.click();
downloadAnchorNode.remove(); downloadAnchorNode.remove();
}} }}>
>
export export
</button> </button>;
);
let context = ( let context = <ContextMenu event={evt} >
<ContextMenu event={evt}>
{deleteb} {deleteb}
{delete_key} {delete_key}
{exportb} {exportb}
</ContextMenu> </ContextMenu>
);
this.setState({ context }); this.setState({ context });
return false; return false;
} }
render() { render() {
let elms = this.state.vaults.map((vault) => { let elms = this.state.vaults.map(vault => {
return ( return <li class="vaults_vault" onClick={() => this.openVault(vault)} onContextMenu={(evt) => this.onContext(evt, vault)}>
<li {vault.encrypted ? <Lock height={undefined} width={undefined} /> : <Unlock height={undefined} width={undefined} />}
class="vaults_vault" <span>
onClick={() => this.openVault(vault)} {vault.name}
onContextMenu={(evt) => this.onContext(evt, vault)} </span>
>
{vault.encrypted ? (
<Lock height={undefined} width={undefined} />
) : (
<Unlock height={undefined} width={undefined} />
)}
<span>{vault.name}</span>
</li> </li>
); })
});
return ( return <div style={{ marginTop: "-12px", paddingTop: "12px" }} >
<div style={{ marginTop: "-12px", paddingTop: "12px" }}>
{/* {this.state.modal} */} {/* {this.state.modal} */}
{this.state.context} {this.state.context}
<header class="header"> <header class="header">
<span></span> <span></span>
<h3 <h3 style="display:inline" onClick={() => Navigation.setPage("/")}>{this.props.selectVault ? "Select Vault for share" : "Your vaults:"}</h3>
style="display:inline" <a class="header-icon-button" onClick={() => Navigation.setPage("/settings")}><Settings height={undefined} width={undefined} /></a>
onClick={() => Navigation.setPage("/")}
>
{this.props.selectVault
? "Select Vault for share"
: "Your vaults:"}
</h3>
<a
class="header-icon-button"
onClick={() => Navigation.setPage("/settings")}
>
<Settings height={undefined} width={undefined} />
</a>
</header> </header>
<AddButton onClick={() => this.addButtonClick()} /> <AddButton onClick={() => this.addButtonClick()} />
<div class="container"> <div class="container">
<ul class="list list-divider list-clickable">{elms}</ul> <ul class="list list-divider list-clickable">
{elms}
</ul>
</div> </div>
</div> </div>
);
} }
} }

View File

@ -1,23 +0,0 @@
import { useEffect, useMemo, useState } from "preact/hooks";
export function usePromise<T>(promiseFnc: () => Promise<T>, params: any[]) {
const promise = useMemo(promiseFnc, params);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(undefined);
const [value, setValue] = useState(undefined);
useEffect(() => {
let canceled = false;
promise
.then((res) => setValue(res))
.catch((err) => setError(err))
.finally(() => setLoading(false));
return () => {
canceled = true;
};
}, [promise]);
return [loading, error, value];
}

View File

@ -1,70 +1,61 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>SecureNotes</title> <title>SecureNotes</title>
<meta charset="utf8" /> <meta charset="utf8" />
<meta name="Description" content="Notes app" /> <meta name="Description" content="Notes app">
<meta name="viewport" content="width=device-width,initial-scale=1" /> <meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="manifest" href="./manifest.webmanifest" /> <link rel="manifest" href="/manifest.json">
<!-- <link rel="shortcut icon" href="/public/icon-72x72.png"> --> <!-- <link rel="shortcut icon" href="/public/icon-72x72.png"> -->
<!-- Add to home screen for Safari on iOS --> <!-- Add to home screen for Safari on iOS -->
<meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="Secure Notes" /> <meta name="apple-mobile-web-app-title" content="Secure Notes">
<!-- sizes="180x180" -->
<!-- href="/public/apple-touch-icon.png" -->
<link <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
rel="apple-touch-icon" <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#1E88E5">
sizes="256x256" <script src="encoding.js"></script>
href="/public/notepad256.png" <meta name="msapplication-TileColor" content="#1E88E5">
/> <meta name="theme-color" content="#1E88E5">
<!-- <link
rel="mask-icon"
href="/public/safari-pinned-tab.svg"
color="#1E88E5"
/> -->
<meta name="msapplication-TileColor" content="#1E88E5" />
<meta name="theme-color" content="#1E88E5" />
</head> </head>
<body> <body>
<noscript> You have to enable JavaScript to use this site! </noscript> <noscript>
You have to enable JavaScript to use this site!
</noscript>
<div id="app"></div> <div id="app"></div>
<script> <script>
// // Check that service workers are registered
// if ('serviceWorker' in navigator) {
// // Use the window load event to keep the page load performant
// window.addEventListener('load', () => {
// navigator.serviceWorker.register('/public/serviceworker.js');
// });
// }
if (navigator.serviceWorker.controller) { if (navigator.serviceWorker.controller) {
if (localStorage.getItem("debug")) { if (localStorage.getItem("debug")) {
console.warn( console.warn("Debuggung and service worker found, make shure to clear cache!");
"Debuggung and service worker found, make shure to clear cache!"
);
} }
console.log("active service worker found, no need to register"); console.log('active service worker found, no need to register')
} else { } else {
if (localStorage.getItem("debug")) { if (localStorage.getItem("debug")) {
console.warn("Disabling Service Worker in debug mode!"); console.warn("Disabling Service Worker in debug mode!")
} else { } else {
// Register the ServiceWorker // Register the ServiceWorker
navigator.serviceWorker navigator.serviceWorker.register('/serviceworker.js', {
.register("serviceworker.js", { scope: '/'
scope: "/", }).then(function (reg) {
}) console.log('Service worker has been registered for scope:' + reg.scope);
.then(function (reg) { navigator.serviceWorker.controller.addEventListener("cleared_cache", evt => {
console.log(
"Service worker has been registered for scope:" +
reg.scope
);
navigator.serviceWorker.controller.addEventListener(
"cleared_cache",
(evt) => {
console.log(evt); console.log(evt);
} })
);
}); });
} }
} }
</script> </script>
<script src="index.tsx"></script>
</body> </body>
</html> </html>

View File

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

View File

@ -3,7 +3,7 @@ import { Lock, Observable } from "@hibas123/utils";
import * as aesjs from "aes-js"; import * as aesjs from "aes-js";
import { IDBPTransaction } from "idb"; import { IDBPTransaction } from "idb";
import { sha256 } from "js-sha256"; import { sha256 } from "js-sha256";
import { v4 as uuidv4 } from "uuid"; import * as uuidv4 from "uuid/v4";
import * as config from "../config.json"; import * as config from "../config.json";
import * as b64 from "./helper/base64"; import * as b64 from "./helper/base64";
import IDB from "./helper/indexeddb"; import IDB from "./helper/indexeddb";
@ -366,6 +366,7 @@ class NotesProvider {
}; };
try { try {
// log(id, "LRO: ", !!local, !!remote, !!oplog)
if (remote && !oplog) { if (remote && !oplog) {
if (local) { if (local) {
let old = let old =
@ -397,7 +398,6 @@ class NotesProvider {
log(id, "DO NOTHING"); log(id, "DO NOTHING");
stats.do_nothing++; stats.do_nothing++;
} }
await this.oplogDB.delete(id);
} catch (err) { } catch (err) {
console.error(err); console.error(err);
stats.error++; stats.error++;
@ -406,6 +406,7 @@ class NotesProvider {
MessageType.ERROR MessageType.ERROR
); );
} }
await this.oplogDB.delete(id);
} }
log("Stats", stats); log("Stats", stats);
this.syncedObservableServer.send( this.syncedObservableServer.send(

View File

@ -1,130 +0,0 @@
function log(...params) {
console.log.apply(this, [
...["%c[SW]: %c", "color: #f4b942;", "color:unset;"],
...params,
]);
}
const CACHE = "offline";
let precacheFiles = ["/", "/index.html"];
//Install stage sets up the cache-array to configure pre-cache content
self.addEventListener("install", (evt) => {
log("The service worker is being installed.");
evt.waitUntil(
precache()
.then(() => {
log("Skip waiting on install");
})
.catch(log)
.then(() => self.skipWaiting())
);
});
//allow sw to control of current page
self.addEventListener("activate", (event) => {
log("Claiming clients for current page");
return self.clients.claim();
});
self.addEventListener("message", (event) => {
log("Clearing cache");
caches.delete(CACHE);
event.waitUntil(precache());
});
var Types;
(function (Types) {
Types[(Types["CACHE"] = 0)] = "CACHE";
Types[(Types["NOCACHE"] = 1)] = "NOCACHE";
Types[(Types["REFRESH"] = 2)] = "REFRESH";
Types[(Types["INDEX"] = 3)] = "INDEX";
})(Types || (Types = {}));
let rules = [
{
match: (url) => {
return url.indexOf("/api/") >= 0;
},
type: Types.NOCACHE,
},
{
match: (url) => {
return url.indexOf("/share") >= 0;
},
type: Types.INDEX,
},
{
match: () => {
return true;
},
type: Types.REFRESH,
},
];
self.addEventListener("fetch", (evt) => {
if (evt.request.method != "GET") return; // Dont care about POST requests
let rule = rules.find((rule) => rule.match(evt.request.url));
evt.respondWith(
(async () => {
log("Cache:", Types[rule.type]);
switch (rule.type) {
case Types.CACHE:
return fromCache(evt.request);
case Types.REFRESH:
return refresh(evt.request).then((r) => {
evt.waitUntil(r.refresh.catch((_) => {}));
return r.result;
});
case Types.NOCACHE:
return fetch(evt.request);
case Types.INDEX:
return refresh(new Request("/")).then((r) => {
evt.waitUntil(r.refresh.catch((_) => {}));
return r.result;
});
}
})()
);
});
async function fromCache(request) {
let cache = await caches.open(CACHE);
let matching = await cache.match(request);
if (matching) return matching;
let res = await fetch(request.clone());
await cache.put(
request,
{
match: (url) => {
return url.indexOf("/version_hash") >= 0;
},
type: Types.NOCACHE,
},
res
);
return await cache.match(request);
}
async function refresh(request) {
let cache = await caches.open(CACHE);
let web = fetch(request.clone()).then((res) => {
return cache.put(request, res).then(() => {
return cache.match(request);
});
});
let matching = await cache.match(request);
return {
result: matching ? matching : web,
refresh: web,
};
}
function precache() {
return caches.open(CACHE).then(function (cache) {
return cache.addAll(precacheFiles);
});
}

View File

@ -1,20 +1,15 @@
import { AwaitStore, Observable } from "@hibas123/utils"; const light = require("!!raw-loader!@hibas123/theme/out/light.css").default;
import { readFileSync } from "fs"; const dark = require("!!raw-loader!@hibas123/theme/out/dark.css").default;
const light = readFileSync(
"node_modules/@hibas123/theme/out/light.css",
"utf-8"
);
const dark = readFileSync("node_modules/@hibas123/theme/out/dark.css", "utf-8");
export enum ThemeStates { export enum ThemeStates {
AUTO, AUTO,
LIGHT, LIGHT,
DARK, DARK
} }
let themeConfig: ThemeStates = Number(localStorage.getItem("theme")); let themeConfig: ThemeStates = Number(localStorage.getItem("theme"));
if (Number.isNaN(themeConfig)) themeConfig = ThemeStates.AUTO; if (Number.isNaN(themeConfig))
themeConfig = ThemeStates.AUTO;
let isDark = false; let isDark = false;
let mediaIsDark = false; let mediaIsDark = false;
@ -22,32 +17,28 @@ let mediaIsDark = false;
if (window.matchMedia) { if (window.matchMedia) {
const mediaq = matchMedia("(prefers-color-scheme: dark)"); const mediaq = matchMedia("(prefers-color-scheme: dark)");
mediaIsDark = mediaq.matches; mediaIsDark = mediaq.matches;
mediaq.onchange = (ev) => { mediaq.onchange = ev => {
mediaIsDark = ev.matches; mediaIsDark = ev.matches;
apply(); apply();
}; }
console.log(mediaq); console.log(mediaq);
} }
const DarkModeStore = new AwaitStore<boolean>(isDark);
let styleElm: HTMLStyleElement; let styleElm: HTMLStyleElement;
function apply(force?: boolean) { function apply(force?: boolean) {
let shouldDark = let shouldDark = themeConfig === ThemeStates.AUTO ? mediaIsDark : themeConfig === ThemeStates.DARK;
themeConfig === ThemeStates.AUTO
? mediaIsDark
: themeConfig === ThemeStates.DARK;
if (force || shouldDark !== isDark) { if (force || shouldDark !== isDark) {
if (styleElm) styleElm.remove(); if (styleElm) styleElm.remove();
styleElm = document.createElement("style"); styleElm = document.createElement("style");
document.head.appendChild(styleElm); document.head.appendChild(styleElm);
styleElm.innerHTML = shouldDark ? dark : light; styleElm.innerHTML = shouldDark ? dark : light;
isDark = shouldDark; isDark = shouldDark;
DarkModeStore.send(isDark);
} }
} }
apply(true); apply(true);
function change(state: ThemeStates) { function change(state: ThemeStates) {
themeConfig = state; themeConfig = state;
localStorage.setItem("theme", String(themeConfig)); localStorage.setItem("theme", String(themeConfig));
@ -56,6 +47,6 @@ function change(state: ThemeStates) {
export default { export default {
active: () => themeConfig, active: () => themeConfig,
change: (state: ThemeStates) => change(state), change: (state: ThemeStates) => change(state)
isDark: DarkModeStore, }
};

10
src/types.d.ts vendored
View File

@ -1,3 +1,13 @@
declare module "*.svg" {
const SVG: (props: {
width?: number | undefined
height?: number | undefined
onClick?: (evt: MouseEvent) => void;
style?: string;
}) => JSX.Element;
export default SVG;
}
interface Window { interface Window {
debug: any; debug: any;
} }