A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
feat: add conflict resolution ui & logic
thecodrr471be3c05f00
diff --git a/apps/web/src/app.js b/apps/web/src/app.jsindex 5137242db..efcfcafb2 100644--- a/apps/web/src/app.js+++ b/apps/web/src/app.js@@ -1,21 +1,21 @@import React, { useEffect } from "react";import "./app.css";-import Editor from "./components/editor";import { Flex, Box } from "rebass";import ThemeProvider from "./components/theme-provider";import { usePersistentState } from "./utils/hooks";import { useStore } from "./stores/app-store";import { useStore as useAppStore } from "./stores/app-store";+import { useStore as useEditorStore } from "./stores/editor-store";import { useStore as useUserStore } from "./stores/user-store";import Animated from "./components/animated";import NavigationMenu from "./components/navigationmenu";-import { EditorNavigator } from "./navigation/navigators";function App() {const [show, setShow] = usePersistentState("isContainerVisible", true);- const refreshColors = useStore(store => store.refreshColors);- const isFocusMode = useAppStore(store => store.isFocusMode);- const initUser = useUserStore(store => store.init);+ const refreshColors = useStore((store) => store.refreshColors);+ const isFocusMode = useAppStore((store) => store.isFocusMode);+ const initUser = useUserStore((store) => store.init);+ const openLastSession = useEditorStore((store) => store.openLastSession);useEffect(() => {refreshColors();@@ -32,8 +32,8 @@ function App() {}, [isFocusMode]);useEffect(() => {- EditorNavigator.navigate("editor");- }, []);+ openLastSession();+ }, [openLastSession]);return (<ThemeProvider>@@ -48,12 +48,12 @@ function App() {width: show ? "30%" : "0%",scaleY: show ? 1 : 0.8,opacity: show ? 1 : 0,- zIndex: show ? 0 : -1+ zIndex: show ? 0 : -1,}}transition={{ duration: 0.3, ease: "easeOut" }}sx={{borderRight: "1px solid",- borderColor: "border"+ borderColor: "border",}}/><Flex width="100%" className="EditorNavigator" />diff --git a/apps/web/src/components/editor/header.js b/apps/web/src/components/editor/header.jsindex ddbfae967..7fa193f1e 100644--- a/apps/web/src/components/editor/header.js+++ b/apps/web/src/components/editor/header.js@@ -15,27 +15,27 @@ const TextSeperator = () => {};function Header() {- const title = useStore(store => store.session.title);- const dateEdited = useStore(store => store.session.dateEdited);- const id = useStore(store => store.session.id);- const text = useStore(store => store.session.content.text);- const isSaving = useStore(store => store.session.isSaving);- const sessionState = useStore(store => store.session.state);- const setSession = useStore(store => store.setSession);+ const title = useStore((store) => store.session.title);+ const dateEdited = useStore((store) => store.session.dateEdited);+ const id = useStore((store) => store.session.id);+ const text = useStore((store) => store.session.content.text);+ const isSaving = useStore((store) => store.session.isSaving);+ const sessionState = useStore((store) => store.session.state);+ const setSession = useStore((store) => store.setSession);return (<><TitleBoxshouldFocus={sessionState === SESSION_STATES.new}title={title}- setTitle={title =>- setSession(state => {+ setTitle={(title) =>+ setSession((state) => {state.session.title = title;})}sx={{paddingTop: 2,- paddingBottom: 0+ paddingBottom: 0,}}/><Text@@ -46,7 +46,7 @@ function Header() {display: "flex",alignItems: "center",marginTop: dateEdited || text.length || id.length ? 0 : 2,- marginBottom: dateEdited || text.length || id.length ? 2 : 0+ marginBottom: dateEdited || text.length || id.length ? 2 : 0,}}>{dateEdited > 0 ? (diff --git a/apps/web/src/components/editor/index.js b/apps/web/src/components/editor/index.jsindex 3a96adf1d..be672c7cc 100644--- a/apps/web/src/components/editor/index.js+++ b/apps/web/src/components/editor/index.js@@ -10,13 +10,12 @@ import EditorMenu from "./editormenu";import Header from "./header";function Editor() {- const id = useStore(store => store.session.id);- const delta = useStore(store => store.session.content.delta);- const sessionState = useStore(store => store.session.state);- const setSession = useStore(store => store.setSession);- const saveSession = useStore(store => store.saveSession);- const openLastSession = useStore(store => store.openLastSession);- const isFocusMode = useAppStore(store => store.isFocusMode);+ const id = useStore((store) => store.session.id);+ const delta = useStore((store) => store.session.content.delta);+ const sessionState = useStore((store) => store.session.state);+ const setSession = useStore((store) => store.setSession);+ const saveSession = useStore((store) => store.saveSession);+ const isFocusMode = useAppStore((store) => store.isFocusMode);const quillRef = useRef();useEffect(() => {@@ -28,22 +27,19 @@ function Editor() {}}, []);- useEffect(() => {- openLastSession();- }, [openLastSession]);-return (<Animated.Flexwidth={["0%", "0%", "100%"]}initial={{ marginRight: 0 }}animate={{- marginRight: isFocusMode ? "25%" : 0+ marginRight: isFocusMode ? "25%" : 0,}}transition={{ duration: 0.3, ease: "easeIn" }}sx={{marginLeft: isFocusMode ? "25%" : 0,- position: "relative"+ position: "relative",}}+ flex="1 1 auto"><Flexvariant="columnFill"@@ -65,11 +61,11 @@ function Editor() {onSave={() => {saveSession();}}- onChange={editor => {- setSession(state => {+ onChange={(editor) => {+ setSession((state) => {state.session.content = {delta: { ops: editor.getContents().ops },- text: editor.getText()+ text: editor.getText(),};});}}diff --git a/apps/web/src/components/editor/react-quill.js b/apps/web/src/components/editor/react-quill.jsindex 04604e292..9278bb774 100644--- a/apps/web/src/components/editor/react-quill.js+++ b/apps/web/src/components/editor/react-quill.js@@ -1,13 +1,14 @@import React, { Component } from "react";import Quill from "quill";import "quill/dist/quill.snow.css";+import "quill/dist/quill.core.css";import MarkdownShortcuts from "./modules/markdown";import MagicUrl from "quill-magic-url";Quill.register("modules/markdownShortcuts", MarkdownShortcuts);Quill.register("modules/magicUrl", MagicUrl);-const modules = {+const quillModules = {toolbar: [[{ header: [1, 2, 3, 4, 5, 6] }],["bold", "italic", "underline", "strike", "blockquote"],@@ -15,23 +16,23 @@ const modules = {{ align: "" },{ align: "center" },{ align: "right" },- { align: "justify" }+ { align: "justify" },],[{ list: "ordered" },{ list: "bullet" },{ indent: "-1" },- { indent: "+1" }+ { indent: "+1" },],[{ size: ["small", false, "large", "huge"] }],["code-block", { script: "sub" }, { script: "super" }],[{ color: [] }, { background: [] }],["link", "image", "video"],- [{ direction: "rtl" }, "clean"]+ [{ direction: "rtl" }, "clean"],],syntax: true,markdownShortcuts: {},- magicUrl: true+ magicUrl: true,};export default class ReactQuill extends Component {@@ -65,15 +66,16 @@ export default class ReactQuill extends Component {initialContent,onChange,onSave,- id+ modules,+ id,} = this.props;this.quill = new Quill("#" + id, {placeholder,bounds: container,- modules,+ modules: modules || quillModules,theme: "snow",- readOnly+ readOnly,});if (initialContent) {@@ -88,7 +90,7 @@ export default class ReactQuill extends Component {this.quill.keyboard.addBinding({key: "S",- shortKey: true+ shortKey: true,},onSave.bind(this, this.quill));diff --git a/apps/web/src/components/spliteditor/deltatoggle.js b/apps/web/src/components/spliteditor/deltatoggle.jsnew file mode 100644index 000000000..6125f3fc3--- /dev/null+++ b/apps/web/src/components/spliteditor/deltatoggle.js@@ -0,0 +1,86 @@+import React from "react";+import { Flex, Text, Button } from "rebass";+import { timeConverter } from "../../utils/time";+import DeltaTransformer from "quill/core/delta";+import { useStore } from "../../stores/mergestore";++const deltaTransformer = new DeltaTransformer();++function DeltaToggle(props) {+ const {+ isSelected,+ isOtherSelected,+ onToggle,+ direction,+ sx,+ label,+ dateEdited,+ editors,+ } = props;+ const resolveConflict = useStore((store) => store.resolveConflict);++ return (+ <Flex flexDirection="column" sx={sx}>+ <Flex>+ {isOtherSelected && (+ <Button+ variant="primary"+ mr={2}+ onClick={async () => {+ const { selectedEditor, otherEditor } = editors();+ await resolveConflict(+ {+ delta: deltaTransformer.cleanDifference(+ selectedEditor.getContents()+ ),+ text: selectedEditor.getText(),+ },+ {+ delta: deltaTransformer.cleanDifference(+ otherEditor.getContents()+ ),+ text: otherEditor.getText(),+ }+ );+ }}+ >+ Save copy+ </Button>+ )}+ <Button+ variant="primary"+ onClick={async () => {+ if (isOtherSelected) {+ const { selectedEditor } = editors();+ await resolveConflict({+ delta: deltaTransformer.cleanDifference(+ selectedEditor.getContents()+ ),+ text: selectedEditor.getText(),+ });+ } else {+ onToggle();+ }+ }}+ bg={isOtherSelected ? "error" : "primary"}+ >+ {isSelected ? "Undo" : isOtherSelected ? "Discard" : "Keep"}+ </Button>+ </Flex>+ <Flex+ flexDirection={direction}+ alignItems="center"+ mt={1}+ sx={{ fontSize: "subBody", color: "fontTertiary" }}+ >+ {label}+ {/* eslint-disable-next-line jsx-a11y/accessible-emoji */}+ <Text as="span" role="img" fontSize={7} mx={1}>+ ⚫+ </Text>+ {timeConverter(dateEdited, true)}+ </Flex>+ </Flex>+ );+}+export default DeltaToggle;diff --git a/apps/web/src/components/spliteditor/index.js b/apps/web/src/components/spliteditor/index.jsnew file mode 100644index 000000000..f307ab810--- /dev/null+++ b/apps/web/src/components/spliteditor/index.js@@ -0,0 +1,102 @@+import React, { useState, useRef } from "react";+import { Flex, Box, Text } from "rebass";+import SimpleEditor from "./simpleeditor";+import DeltaTransformer from "quill/core/delta";+import DeltaToggle from "./deltatoggle";+import { useStore } from "../../stores/mergestore";++const deltaTransformer = new DeltaTransformer();++function SplitEditor(props) {+ const conflictedNote = useStore((store) => store.conflictedNote);+ const remoteDelta = useStore((store) => store.remoteDelta);+ const localDelta = useStore((store) => store.localDelta);+ const [localEditor, remoteEditor] = [useRef(), useRef()];+ const [selectedDelta, setSelectedDelta] = useState(-1);+ if (!conflictedNote) return null;+ return (+ <Flex width="100%" flex="1 1 auto" flexDirection="column">+ <Flex+ sx={{+ borderBottom: "1px solid",+ borderColor: "border",+ position: "relative",+ }}+ justifyContent="stretch"+ alignItems="center"+ p={2}+ >+ <DeltaToggle+ sx={{ flex: "0.1 1 auto" }}+ label="Current note"+ dateEdited={localDelta.dateEdited}+ isSelected={selectedDelta === 0}+ isOtherSelected={selectedDelta === 1}+ onToggle={() => setSelectedDelta((s) => (s === 0 ? -1 : 0))}+ editors={() => ({+ selectedEditor: remoteEditor.current.quill,+ otherEditor: localEditor.current.quill,+ })}+ />+ <Text+ flex="0.8 1 auto"+ variant="heading"+ textAlign="center"+ sx={{+ whiteSpace: "nowrap",+ overflow: "hidden",+ textOverflow: "ellipsis",+ }}+ >+ {conflictedNote.title}+ </Text>+ <DeltaToggle+ sx={{ flex: "0.1 1 auto", alignItems: "flex-end" }}+ direction="row-reverse"+ label="Incoming note"+ isSelected={selectedDelta === 1}+ isOtherSelected={selectedDelta === 0}+ dateEdited={remoteDelta.dateEdited}+ onToggle={() => setSelectedDelta((s) => (s === 1 ? -1 : 1))}+ editors={() => ({+ selectedEditor: localEditor.current.quill,+ otherEditor: remoteEditor.current.quill,+ })}+ />+ </Flex>+ <Flex flex="1 1 auto">+ <Box+ className="firstEditor"+ width="50%"+ flex="1 1 auto"+ sx={{ borderRight: "1px solid", borderColor: "border" }}+ >+ <SimpleEditor+ pref={localEditor}+ container=".firstEditor"+ id="firstQuill"+ delta={deltaTransformer.highlightDifference(+ localDelta.data,+ remoteDelta.data,+ "#FDB0C0"+ )}+ />+ </Box>+ <Box className="secondEditor" flex="1 1 auto" width="50%">+ <SimpleEditor+ pref={remoteEditor}+ container=".secondEditor"+ id="secondQuill"+ delta={deltaTransformer.highlightDifference(+ remoteDelta.data,+ localDelta.data,+ "#CAFFFB"+ )}+ />+ </Box>+ </Flex>+ </Flex>+ );+}++export default SplitEditor;diff --git a/apps/web/src/components/spliteditor/simpleeditor.js b/apps/web/src/components/spliteditor/simpleeditor.jsnew file mode 100644index 000000000..bf7c22a54--- /dev/null+++ b/apps/web/src/components/spliteditor/simpleeditor.js@@ -0,0 +1,21 @@+import React, { useEffect } from "react";+import ReactQuill from "../editor/react-quill";++function SimpleEditor(props) {+ const { delta, container, id, pref } = props;+ useEffect(() => {+ const toolbar = document.querySelector(`${container} .ql-toolbar.ql-snow`);+ toolbar.remove();+ }, [container]);+ return (+ <ReactQuill+ ref={pref}+ id={id}+ modules={{ toolbar: [] }}+ initialContent={delta}+ placeholder="Type anything here"+ container={container}+ />+ );+}+export default SimpleEditor;diff --git a/apps/web/src/navigation/index.js b/apps/web/src/navigation/index.jsindex 4483b43fe..57d2430e1 100644--- a/apps/web/src/navigation/index.js+++ b/apps/web/src/navigation/index.js@@ -38,7 +38,6 @@ class Navigator {this.history.push(this.lastRoute);}this.lastRoute = route;-return this.renderRoute(route);}@@ -95,17 +94,17 @@ class Navigator {_mergeParams(route, params) {return {...route,- params: { ...route.params, ...params }+ params: { ...route.params, ...params },};}}export default Navigator;function NavigationContainer(props) {- const toggleSideMenu = useStore(store => store.toggleSideMenu);- const isSelectionMode = useStore(store => store.isSelectionMode);- const exitSelectionMode = useStore(store => store.exitSelectionMode);- const selectAll = useStore(store => store.selectAll);+ const toggleSideMenu = useStore((store) => store.toggleSideMenu);+ const isSelectionMode = useStore((store) => store.isSelectionMode);+ const exitSelectionMode = useStore((store) => store.exitSelectionMode);+ const selectAll = useStore((store) => store.selectAll);return (<ThemeProvider><Flex flexDirection="column" px={2}>@@ -128,7 +127,7 @@ function NavigationContainer(props) {height={38}ml={-5}sx={{- display: ["block", "none", "none"]+ display: ["block", "none", "none"],}}><Icon.Menu size={38} />@@ -142,7 +141,7 @@ function NavigationContainer(props) {</Flex>{props.route.options && isSelectionMode && (<Flex>- {props.route.options.map(option => (+ {props.route.options.map((option) => (<Boxkey={option.icon.name}onClick={option.onClick}@@ -161,7 +160,7 @@ function NavigationContainer(props) {color="primary"sx={{marginBottom: 2,- cursor: isSelectionMode ? "pointer" : "normal"+ cursor: isSelectionMode ? "pointer" : "normal",}}>{props.route.params.subtitle}diff --git a/apps/web/src/navigation/navigators/editornavigator.js b/apps/web/src/navigation/navigators/editornavigator.jsindex 37cae80f1..3e4d1652c 100644--- a/apps/web/src/navigation/navigators/editornavigator.js+++ b/apps/web/src/navigation/navigators/editornavigator.js@@ -1,12 +1,14 @@import Editor from "../../components/editor";+import SplitEditor from "../../components/spliteditor";import Navigator from "../index";import { createRoute } from "../routes";const routes = {- ...createRoute("editor", Editor)+ ...createRoute("editor", Editor),+ ...createRoute("split", SplitEditor),};const EditorNavigator = new Navigator("EditorNavigator", routes, {- backButtonEnabled: false+ defaultRoute: "editor",});export default EditorNavigator;diff --git a/apps/web/src/navigation/navigators/index.js b/apps/web/src/navigation/navigators/index.jsindex 060b5601e..387d31100 100644--- a/apps/web/src/navigation/navigators/index.js+++ b/apps/web/src/navigation/navigators/index.js@@ -2,4 +2,4 @@ export const NotebookNavigator = require("./nbnavigator").default;export const RootNavigator = require("./rootnavigator").default;export const SettingsNavigator = require("./settingnavigator").default;export const TagNavigator = require("./tagnavigator").default;-export const EditorNavigator = require("./editornavigator").default;+//export const EditorNavigator = require("./editornavigator").default;diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.jsindex e9a076146..3afdbf530 100644--- a/apps/web/src/stores/editor-store.js+++ b/apps/web/src/stores/editor-store.js@@ -2,14 +2,16 @@ import createStore from "../common/store";import { store as noteStore } from "./note-store";import { store as appStore } from "./app-store";import { store as tagStore } from "./tag-store";+import { store as mergeStore } from "./mergestore";import { db } from "../common";import BaseStore from ".";import Vault from "../common/vault.js";+import EditorNavigator from "../navigation/navigators/editornavigator";const SESSION_STATES = { stale: "stale", new: "new" };const DEFAULT_SESSION = {notebook: undefined,- state: SESSION_STATES.new,+ state: undefined,isSaving: false,title: "",timeout: 0,@@ -30,19 +32,27 @@ const DEFAULT_SESSION = {class EditorStore extends BaseStore {session = DEFAULT_SESSION;- openLastSession = () => {+ openLastSession = async () => {const id = localStorage.getItem("lastOpenedNote");- if (!id) return;- this.openSession(db.notes.note(id).data);+ if (!id) {+ return EditorNavigator.navigate("editor");+ }+ await this.openSession(db.notes.note(id).data);};openSession = async (note) => {clearTimeout(this.get().session.timeout);+ if (note.conflicted) {+ return await mergeStore.openConflict(note);+ } else {+ EditorNavigator.navigate("editor");+ }+let content = {};if (!note.locked) {content = {- text: note.content.text,+ text: await db.notes.note(note).text(),delta: await db.notes.note(note).delta(),};} else {@@ -55,9 +65,9 @@ class EditorStore extends BaseStore {...DEFAULT_SESSION,...note,content,+ state: SESSION_STATES.new,};});-noteStore.setSelectedNote(note.id);};@@ -87,11 +97,13 @@ class EditorStore extends BaseStore {};newSession = (context = {}) => {+ EditorNavigator.navigate("editor");clearTimeout(this.get().session.timeout);this.set(function (state) {state.session = {...DEFAULT_SESSION,...context,+ state: SESSION_STATES.new,};});saveLastOpenedNote();diff --git a/apps/web/src/stores/mergestore.js b/apps/web/src/stores/mergestore.jsindex ff1f1639d..b9bf6196b 100644--- a/apps/web/src/stores/mergestore.js+++ b/apps/web/src/stores/mergestore.js@@ -1,6 +1,8 @@import createStore from "../common/store";import { db } from "../common";-import Navigators from "../navigation/navigators";+import { store as noteStore } from "./note-store";+import { store as editorStore } from "./editor-store";+import EditorNavigator from "../navigation/navigators/editornavigator";import BaseStore from "./index";class MergeStore extends BaseStore {@@ -8,14 +10,49 @@ class MergeStore extends BaseStore {localDelta;remoteDelta;- openConflict = async note => {+ openConflict = async (note) => {+ noteStore.setSelectedNote(note.id);const delta = await db.delta.raw(note.content.delta);- this.set(state => {+ this.set((state) => {state.conflictedNote = note;state.localDelta = { ...delta, conflicted: false };state.remoteDelta = delta.conflicted;});- Navigators.EditorNavigator.navigate("split");+ EditorNavigator.navigate("split");+ };++ resolveConflict = async (selectedContent, otherContent) => {+ const note = this.get().conflictedNote;+ selectedContent.delta = {+ data: { ops: selectedContent.delta },+ resolved: true,+ };+ await db.notes.add({+ id: note.id,+ content: selectedContent,+ conflicted: false,+ });+ if (otherContent) {+ otherContent.delta = {+ data: { ops: otherContent.delta },+ };+ await db.notes.add({+ ...note,+ content: otherContent,+ id: undefined,+ dateCreated: undefined,+ dateEdited: undefined,+ title: note.title + " (DUPLICATE)",+ });+ }+ this.set((state) => {+ state.conflictedNote = undefined;+ state.localDelta = undefined;+ state.remoteDelta = undefined;+ });+ noteStore.refresh();+ noteStore.setSelectedNote(note.id);+ await editorStore.openSession(note.id);};}diff --git a/apps/web/yarn.lock b/apps/web/yarn.lockindex a5dd4da00..88a909f93 100644--- a/apps/web/yarn.lock+++ b/apps/web/yarn.lock@@ -6910,7 +6910,7 @@ normalize-url@^3.0.0, normalize-url@^3.0.1:"notes-core@git+ssh://[email protected]:thecodrr/notes-core.git":version "1.2.0"- resolved "git+ssh://[email protected]:thecodrr/notes-core.git#2030036bee3a9e433a4e1716731052d7e10f18b2"+ resolved "git+ssh://[email protected]:thecodrr/notes-core.git#069226c64718e4c4eb0e773a7c613db8435b65a8"dependencies:fast-sort "^2.0.1"fuzzysearch "^1.0.3"@@ -8374,7 +8374,7 @@ quill-markdown-shortcuts@^0.0.10:quill@^1.3.1, "quill@https://github.com/thecodrr/quill.git#1.3.7":version "1.3.7"- resolved "https://github.com/thecodrr/quill.git#60cfa5a319b49760a0b0c7c4434068e2f117092f"+ resolved "https://github.com/thecodrr/quill.git#adbb4f12348764ceca69deaf3c507d8601fdea6e"dependencies:"@mdi/svg" "^5.0.45"clone "^2.1.1"