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: handle attachments in merge conflict editor
ammarahm-ed96731cd1c56d
diff --git a/apps/mobile/src/components/Button/index.js b/apps/mobile/src/components/Button/index.jsindex ce5043d68..e4c0c519a 100644--- a/apps/mobile/src/components/Button/index.js+++ b/apps/mobile/src/components/Button/index.js@@ -76,7 +76,7 @@ export const Button = ({flexDirection: 'row',...style}}>- {loading && <ActivityIndicator color={textColor} />}+ {loading && <ActivityIndicator color={textColor} size={fontSize + 4} />}{icon && !loading && iconPosition === 'left' ? (<Iconname={icon}diff --git a/apps/mobile/src/components/MergeEditor/index.js b/apps/mobile/src/components/MergeEditor/index.jsindex 99e52d683..399836a2f 100644--- a/apps/mobile/src/components/MergeEditor/index.js+++ b/apps/mobile/src/components/MergeEditor/index.js@@ -1,16 +1,16 @@import KeepAwake from '@sayem314/react-native-keep-awake';+import {EVENTS} from 'notes-core/common';import React, {createRef, useEffect, useState} from 'react';-import {Modal, SafeAreaView, Text, TouchableOpacity, View} from 'react-native';-import Animated, {Easing} from 'react-native-reanimated';+import {Modal, SafeAreaView, Text, View} from 'react-native';+import Animated from 'react-native-reanimated';import {useSafeAreaInsets} from 'react-native-safe-area-context';-import Icon from 'react-native-vector-icons/MaterialCommunityIcons';import WebView from 'react-native-webview';import {useTracked} from '../../provider';import {DDS} from '../../services/DeviceDetection';import {eSendEvent,eSubscribeEvent,- eUnSubscribeEvent,+ eUnSubscribeEvent} from '../../services/EventManager';import Navigation from '../../services/Navigation';import Sync from '../../services/Sync';@@ -20,7 +20,7 @@ import diff from '../../utils/differ';import {eApplyChanges,eShowMergeDialog,- refreshNotesPage,+ refreshNotesPage} from '../../utils/Events';import {openLinkInBrowser} from '../../utils/functions';import {normalize, SIZE} from '../../utils/SizeUtils';@@ -28,8 +28,9 @@ import {timeConverter} from '../../utils/TimeUtils';import {getNote,sourceUri,- updateNoteInEditor,+ updateNoteInEditor} from '../../views/Editor/Functions';+import {ActionIcon} from '../ActionIcon';import {Button} from '../Button';import BaseDialog from '../Dialog/base-dialog';import DialogButtons from '../Dialog/dialog-buttons';@@ -44,56 +45,20 @@ const secondWebViewHeight = new Value(dHeight * 0.5 - 50);const primaryWebView = createRef();const secondaryWebView = createRef();let note = null;--function openEditorAnimation(- heightToAnimate,- extendedHeight = null,- siblingStatus,- insets,-) {- let openConfig = {- duration: 300,- toValue: !siblingStatus- ? dHeight - (100 + insets.top)- : dHeight * 0.5 - (50 + insets.top / 2),- easing: Easing.inOut(Easing.ease),- };-- let extendConfig = {- duration: 300,- toValue: dHeight * 0.5 - (50 + insets.top / 2),- easing: Easing.inOut(Easing.ease),- };-- if (extendedHeight) {- timing(extendedHeight, extendConfig).start();- }- timing(heightToAnimate, openConfig).start();-}--function closeEditorAnimation(heightToAnimate, heightToExtend = null, insets) {- let closeConfig = {- duration: 300,- toValue: 0,- easing: Easing.inOut(Easing.ease),- };-- let extendConfig = {- duration: 300,- toValue: dHeight - (100 + insets.top),- easing: Easing.inOut(Easing.ease),- };- if (heightToExtend) {- timing(heightToExtend, extendConfig).start();- }-- timing(heightToAnimate, closeConfig).start();-}-let primaryData = null;-let secondaryData = null;+function onMediaLoaded({hash, src}) {+ let inject = `+ (function(){+ const elements = document.querySelectorAll("img[data-hash=${hash}]");+ if (!elements || !elements.length) return;+ for (let element of elements) element.setAttribute("src", "${src}");+ })();`;+ primaryWebView.current?.injectJavaScript(inject);+ secondaryWebView.current?.injectJavaScript(inject);+}+const MergeEditor = () => {const [state, dispatch] = useTracked();const {colors} = state;@@ -104,40 +69,26 @@ const MergeEditor = () => {const [copyToSave, setCopyToSave] = useState(null);const [disardedContent, setDiscardedContent] = useState(null);const [dialogVisible, setDialogVisible] = useState(false);-+ const [loadingAttachments, setLoadingAttachments] = useState(false);const insets = useSafeAreaInsets();- const onPrimaryWebViewLoad = () => {- // console.log('on load called')- //return;- // console.log("on load called")- // let htmlDiff = {- // before: primaryData.data,- // };- // console.log(secondaryData.data?.length,primaryData.data?.length);- // console.log('before entering')- // if (secondaryData.data) {- // console.log("getting diff");- // htmlDiff = diff.diff_dual_pane(primaryData.data, secondaryData.data);- // console.log("diff generated");- // }- // console.log('posting message')- postMessage(primaryWebView, 'htmldiff', primaryData?.data);+ const onPrimaryWebViewLoad = async () => {+ let content = await db.content.insertPlaceholders(+ primaryData,+ 'placeholder.svg'+ );+ postMessage(primaryWebView, 'htmldiff', content.data);let theme = {...colors};theme.factor = normalize(1);postMessage(primaryWebView, 'theme', JSON.stringify(theme));};- const onSecondaryWebViewLoad = () => {- // console.log('onload2')- //return;- // let htmlDiff = {- // before: primaryData.data,- // };- // if (secondaryData.data) {- // htmlDiff = diff.diff_dual_pane(primaryData.data, secondaryData.data);- // }- postMessage(secondaryWebView, 'htmldiff', secondaryData?.data);+ const onSecondaryWebViewLoad = async () => {+ let content = await db.content.insertPlaceholders(+ secondaryData,+ 'placeholder.svg'+ );+ postMessage(secondaryWebView, 'htmldiff', content?.data);let theme = {...colors};theme.factor = normalize(1);postMessage(secondaryWebView, 'theme', JSON.stringify(theme));@@ -146,7 +97,7 @@ const MergeEditor = () => {function postMessage(webview, type, value = null) {let message = {type: type,- value,+ value};webview.current?.postMessage(JSON.stringify(message));}@@ -159,8 +110,8 @@ const MergeEditor = () => {title: 'Failed to open link',message: e.message,type: 'success',- context: 'local',- }),+ context: 'local'+ })).then(r => {console.log('closed');@@ -172,89 +123,40 @@ const MergeEditor = () => {}};- const onMessageFromPrimaryWebView = evt => {- if (evt.nativeEvent.data !== '') {- let data = JSON.parse(evt.nativeEvent.data);-- if (data.type === 'tiny') {- primaryData = {- type: 'tiny',- data: data.value,- };- }- }- };-- const onMessageFromSecondaryWebView = evt => {- if (evt.nativeEvent.data === '') {- let data = JSON.parse(evt.nativeEvent.data);- if (data.type === 'tiny') {- secondaryData = {- type: 'tiny',- data: data.value,- };- }- }- };-const applyChanges = async () => {- if (keepContentFrom === 'primary') {+ let content = keepContentFrom === 'primary' ? primaryData : secondaryData;+ let keepCopy =+ copyToSave === 'primary'+ ? primaryData+ : copyToSave === 'secondary'+ ? secondaryData+ : null;++ await db.notes.add({+ content: {+ data: content.data,+ type: content.type,+ dateEdited: content.dateEdited,+ remote: true,+ dateResolved: content.dateEdited+ },+ id: note.id,+ conflicted: false+ });+ if (keepCopy) {await db.notes.add({content: {- data: primaryData.data- ? diff.clean(primaryData.data)- : primaryData.data,- type: primaryData.type,- dateEdited:primary.dateEdited,- remote:true,- dateResolved:secondaryData.dateEdited+ data: keepCopy.data,+ type: keepCopy.type},- id: note.id,- conflicted: false,- });- } else if (keepContentFrom === 'secondary') {- await db.notes.add({- content: {- data: secondaryData.data- ? diff.clean(secondaryData.data)- : secondaryData.data,- type: secondaryData.type,- remote:true,- dateEdited:secondaryData.dateEdited,- dateResolved:secondaryData.dateEdited- },- id: note.id,- conflicted: false,- });- }-- if (copyToSave === 'primary') {- await db.notes.add({- content: {- data: primaryData.data- ? diff.clean(primaryData.data)- : primaryData.data,- type: primaryData.type,-- },- id: null,- });- } else if (copyToSave === 'secondary') {- await db.notes.add({- content: {- data: secondaryData.data- ? diff.clean(secondaryData.data)- : secondaryData.data,- type: secondaryData.type,- },- id: null,+ id: null});}eSendEvent(refreshNotesPage);Navigation.setRoutesToUpdate([Navigation.routeNames.NotesPage,Navigation.routeNames.Favorites,- Navigation.routeNames.Notes,+ Navigation.routeNames.Notes]);if (getNote()?.id === note.id) {updateNoteInEditor();@@ -265,19 +167,13 @@ const MergeEditor = () => {const show = async item => {note = item;- console.log('getting raw data');- let noteData = await db.content.raw(note.contentId);- console.log('got raw data');+ let content = await db.content.raw(note.contentId);switch (noteData.type) {case 'tiny':- primaryData = noteData;- secondaryData = noteData.conflicted;+ primaryData = content;+ secondaryData = content.conflicted;}setVisible(true);- console.log('display');- firstWebViewHeight.setValue(dHeight / 2 - (50 + insets.top / 2));- secondWebViewHeight.setValue(dHeight / 2 - (50 + insets.top / 2));- openEditorAnimation(firstWebViewHeight, secondWebViewHeight, true, insets);};useEffect(() => {@@ -292,15 +188,8 @@ const MergeEditor = () => {const onPressKeepFromPrimaryWebView = () => {if (keepContentFrom == 'primary') {setKeepContentFrom(null);- openEditorAnimation(- firstWebViewHeight,- secondWebViewHeight,- false,- insets,- );} else {setKeepContentFrom('primary');- closeEditorAnimation(firstWebViewHeight, secondWebViewHeight, insets);}};@@ -312,15 +201,8 @@ const MergeEditor = () => {const onPressKeepFromSecondaryWebView = () => {if (keepContentFrom == 'secondary') {setKeepContentFrom(null);- openEditorAnimation(- secondWebViewHeight,- firstWebViewHeight,- false,- insets,- );} else {setKeepContentFrom('secondary');- closeEditorAnimation(secondWebViewHeight, firstWebViewHeight, insets);}};@@ -340,6 +222,8 @@ const MergeEditor = () => {};const close = () => {+ db.fs.cancel(primaryData.noteId);+ eUnSubscribeEvent(EVENTS.mediaAttachmentDownloaded, onMediaLoaded);setVisible(false);setPrimary(true);setSecondary(true);@@ -352,7 +236,20 @@ const MergeEditor = () => {primaryText = null;secondaryText = null;note = null;- openEditorAnimation(firstWebViewHeight, secondWebViewHeight, true, insets);+ };++ const onLoadImages = async () => {+ try {+ setLoadingAttachments(true);+ eSubscribeEvent(EVENTS.mediaAttachmentDownloaded, onMediaLoaded);+ await db.content.downloadMedia(primaryData.data.noteId, primaryData);+ await db.content.downloadMedia(primaryData.data.noteId, secondaryData);+ eUnSubscribeEvent(EVENTS.mediaAttachmentDownloaded, onMediaLoaded);+ setLoadingAttachments(false);+ } catch (e) {+ setLoadingAttachments(false);+ eUnSubscribeEvent(EVENTS.mediaAttachmentDownloaded, onMediaLoaded);+ }};return !visible ? null : (@@ -367,7 +264,7 @@ const MergeEditor = () => {<SafeAreaViewstyle={{backgroundColor: colors.bg,- paddingTop: insets.top,+ paddingTop: insets.top}}><KeepAwake />{dialogVisible && (@@ -391,105 +288,73 @@ const MergeEditor = () => {style={{height: '100%',width: '100%',- backgroundColor: DDS.isLargeTablet() ? 'rgba(0,0,0,0.3)' : null,+ backgroundColor: DDS.isLargeTablet() ? 'rgba(0,0,0,0.3)' : null}}><Viewstyle={{- backgroundColor: colors.nav,width: '100%',height: 50,flexDirection: 'row',justifyContent: 'space-between',alignItems: 'center',paddingHorizontal: 12,+ paddingLeft: 6}}><Viewstyle={{flexDirection: 'row',alignItems: 'center',justifyContent: 'space-between',- maxWidth: '50%',+ flexShrink: 1}}>- <Icon- style={{- textAlign: 'center',- textAlignVertical: 'center',- marginLeft: -8,- paddingRight: 10,- paddingVertical: 10,- }}+ <ActionIcononPress={close}- size={SIZE.xxl}color={colors.pri}name="arrow-left"/>- <TouchableOpacity- onPress={() => {- if (keepContentFrom === 'primary') return;- if (!primary) {- openEditorAnimation(- firstWebViewHeight,- secondary && keepContentFrom !== 'secondary'- ? secondWebViewHeight- : null,- secondary && keepContentFrom !== 'secondary',- insets,- );- setPrimary(true);- } else {- closeEditorAnimation(- firstWebViewHeight,- secondary && keepContentFrom !== 'secondary'- ? secondWebViewHeight- : null,- insets,- );- setPrimary(false);- }- }}- style={{- flexDirection: 'row',- alignItems: 'center',- justifyContent: 'space-between',- }}>- <Paragraph- style={{maxWidth: '80%'}}- color={colors.icon}- size={SIZE.xs}>- <Text style={{color: colors.accent, fontWeight: 'bold'}}>- (This Device)- </Text>{' '}- Saved on {timeConverter(primaryData?.dateEdited)}- </Paragraph>- <Icon- size={SIZE.lg}- name={primary ? 'chevron-up' : 'chevron-down'}- color={colors.pri}- />- </TouchableOpacity>+ <Paragraph+ style={{flexWrap: 'wrap'}}+ color={colors.icon}+ size={SIZE.xs}>+ <Text style={{color: colors.accent, fontWeight: 'bold'}}>+ (This Device)+ </Text>+ {'\n'}+ {timeConverter(primaryData?.dateEdited)}+ </Paragraph></View><Viewstyle={{flexDirection: 'row',alignItems: 'center',- justifyContent: 'flex-end',+ justifyContent: 'flex-end'}}>{keepContentFrom === 'secondary' ? (<Button- width={null}onPress={onPressSaveCopyFromPrimaryWebView}- title="Save copy"+ title="Save a copy"+ type="grayBg"+ height={30}+ style={{+ borderRadius: 100,+ paddingHorizontal: 12+ }}+ fontSize={SIZE.xs + 1}/>) : null}<View style={{width: 10}} />{keepContentFrom === 'secondary' ? (<Button- width={null}- height={40}title="Discard"type="accent"accentColor="red"+ height={30}+ style={{+ borderRadius: 100,+ paddingHorizontal: 12+ }}+ fontSize={SIZE.xs + 1}accentText="light"color={colors.errorText}onPress={onPressDiscardFromPrimaryWebView}@@ -497,17 +362,32 @@ const MergeEditor = () => {) : null}{keepContentFrom === 'secondary' ? null : (<>- <View style={{width: 10}} /><Button- width={null}- height={40}+ type="grayBg"+ title="Load images"+ onPress={onLoadImages}+ height={30}+ loading={loadingAttachments}+ fontSize={SIZE.xs}+ icon="download"+ style={{+ borderRadius: 100,+ paddingHorizontal: 12,+ minWidth: 60+ }}+ />+ <Button+ height={30}+ style={{+ borderRadius: 100,+ paddingHorizontal: 12,+ minWidth: 60,+ marginLeft: 10+ }}+ type="accent"+ fontSize={SIZE.xs + 1}title={keepContentFrom === 'primary' ? 'Undo' : 'Keep'}onPress={onPressKeepFromPrimaryWebView}- color={- keepContentFrom === 'primary'- ? colors.errorText- : 'accent'- }/></>)}@@ -516,8 +396,10 @@ const MergeEditor = () => {<Animated.Viewstyle={{- height: firstWebViewHeight,+ height: dHeight / 2 - (50 + insets.top / 2),backgroundColor: colors.bg,+ borderBottomWidth: 1,+ borderBottomColor: colors.nav}}><WebViewonLoad={onPrimaryWebViewLoad}@@ -525,7 +407,7 @@ const MergeEditor = () => {style={{width: '100%',height: '100%',- backgroundColor: 'transparent',+ backgroundColor: 'transparent'}}onShouldStartLoadWithRequest={_onShouldStartLoadWithRequest}cacheMode="LOAD_DEFAULT"@@ -540,97 +422,72 @@ const MergeEditor = () => {originWhitelist={['*']}javaScriptEnabled={true}cacheEnabled={true}- onMessage={onMessageFromPrimaryWebView}source={{- uri: sourceUri + 'plaineditor.html',+ uri: sourceUri + 'plaineditor.html'}}/></Animated.View><Viewstyle={{- backgroundColor: colors.nav,width: '100%',height: 50,flexDirection: 'row',justifyContent: 'space-between',alignItems: 'center',- paddingHorizontal: 12,+ paddingHorizontal: 12}}><Viewstyle={{flexDirection: 'row',alignItems: 'center',justifyContent: 'space-between',- maxWidth: '50%',+ flexShrink: 1}}>- <TouchableOpacity- onPress={() => {- if (keepContentFrom === 'secondary') return;- if (!secondary) {- openEditorAnimation(- secondWebViewHeight,- primary && keepContentFrom !== 'primary'- ? firstWebViewHeight- : null,- primary && keepContentFrom !== 'primary',- insets,- );- setSecondary(true);- } else {- closeEditorAnimation(- secondWebViewHeight,- primary && keepContentFrom !== 'primary'- ? firstWebViewHeight- : null,- insets,- );- setSecondary(false);- }- }}- style={{- flexDirection: 'row',- alignItems: 'center',- justifyContent: 'space-between',- }}>- <Paragraph- style={{maxWidth: '80%'}}- color={colors.icon}- size={SIZE.xs}>- <Text style={{color: 'red', fontWeight: 'bold'}}>- (Incoming)- </Text>{' '}- Saved on {timeConverter(secondaryData?.dateEdited)}- </Paragraph>- <Icon- size={SIZE.lg}- name={secondary ? 'chevron-up' : 'chevron-down'}- color={colors.pri}- />- </TouchableOpacity>+ <Paragraph+ style={{flexWrap: 'wrap'}}+ color={colors.icon}+ size={SIZE.xs}>+ <Text style={{color: 'red', fontWeight: 'bold'}}>+ (Incoming)+ </Text>+ {'\n'}+ {timeConverter(secondaryData?.dateEdited)}+ </Paragraph></View><Viewstyle={{flexDirection: 'row',alignItems: 'center',- justifyContent: 'flex-end',+ justifyContent: 'flex-end'}}>{keepContentFrom === 'primary' ? (<Button- width={null}- height={40}+ height={30}+ style={{+ borderRadius: 100,+ paddingHorizontal: 12,+ minWidth: 60+ }}+ type="accent"+ fontSize={SIZE.xs + 1}onPress={onPressSaveCopyFromSecondaryWebView}- title="Save copy"+ title="Save a copy"/>) : null}<View style={{width: 10}} />{keepContentFrom === 'primary' ? (<Button- width={null}- height={40}title="Discard"type="accent"+ height={30}+ style={{+ borderRadius: 100,+ paddingHorizontal: 12,+ minWidth: 60+ }}+ fontSize={SIZE.xs + 1}accentColor="red"accentText="light"onPress={onPressDiscardFromSecondaryWebView}@@ -639,16 +496,31 @@ const MergeEditor = () => {{keepContentFrom === 'primary' ? null : (<>- <View style={{width: 10}} /><Button- width={null}+ type="grayBg"+ title="Load images"+ height={30}+ loading={loadingAttachments}+ fontSize={SIZE.xs}+ icon="download"+ style={{+ borderRadius: 100,+ paddingHorizontal: 12,+ minWidth: 60+ }}+ />+ <Button+ height={30}+ style={{+ borderRadius: 100,+ paddingHorizontal: 12,+ minWidth: 60,+ marginLeft: 10+ }}+ type="accent"+ fontSize={SIZE.xs + 1}title={keepContentFrom === 'secondary' ? 'Undo' : 'Keep'}onPress={onPressKeepFromSecondaryWebView}- color={- keepContentFrom === 'secondary'- ? colors.errorText- : 'accent'- }/></>)}@@ -657,7 +529,9 @@ const MergeEditor = () => {<Animated.Viewstyle={{- height: secondWebViewHeight,+ height: dHeight / 2 - (50 + insets.top / 2),+ backgroundColor: colors.bg,+ borderRadius: 10}}><WebViewonLoad={onSecondaryWebViewLoad}@@ -665,7 +539,7 @@ const MergeEditor = () => {style={{width: '100%',height: '100%',- backgroundColor: 'transparent',+ backgroundColor: 'transparent'}}onShouldStartLoadWithRequest={_onShouldStartLoadWithRequest}cacheMode="LOAD_DEFAULT"@@ -680,9 +554,8 @@ const MergeEditor = () => {originWhitelist={['*']}javaScriptEnabled={true}cacheEnabled={true}- onMessage={onMessageFromSecondaryWebView}source={{- uri: sourceUri + 'plaineditor.html',+ uri: sourceUri + 'plaineditor.html'}}/></Animated.View>