Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
Editor selection
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Dec 30, 2018
1 parent a6dbcab commit 4414469
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 129 deletions.
4 changes: 3 additions & 1 deletion src/app.js
Expand Up @@ -159,6 +159,8 @@ export default class App extends Component {
return null;
}

return <AppContainer /* persistenceKey="if-you-want-it" */ />;
return (
<AppContainer /* persistenceKey="if-you-want-it" */ />
)
}
}
69 changes: 29 additions & 40 deletions src/containers/NoteCell.js
Expand Up @@ -3,6 +3,7 @@ import { StyleSheet, View, Text, TouchableWithoutFeedback } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import StyleKit from "../style/StyleKit"
import ActionSheet from 'react-native-actionsheet'
import ActionSheetWrapper from "@Style/ActionSheetWrapper"
import ItemActionManager from '../lib/itemActionManager'
import ThemedPureComponent from "@Components/ThemedPureComponent";

Expand Down Expand Up @@ -47,41 +48,37 @@ export default class NoteCell extends ThemedPureComponent {
}
}

static ActionSheetCancelIndex = 0;
static ActionSheetDestructiveIndex = 4;

actionSheetActions() {
var pinAction = this.props.item.pinned ? "Unpin" : "Pin";
let pinEvent = pinAction == "Pin" ? ItemActionManager.PinEvent : ItemActionManager.UnpinEvent;

var archiveOption = this.props.item.archived ? "Unarchive" : "Archive";
let archiveEvent = archiveOption == "Archive" ? ItemActionManager.ArchiveEvent : ItemActionManager.UnarchiveEvent;

return [
['Cancel', ""],
[pinAction, pinEvent],
[archiveOption, archiveEvent],
['Share', ItemActionManager.ShareEvent],
['Delete', ItemActionManager.DeleteEvent]
];
}

showActionSheet = () => {
this.actionSheet.show();
}

handleActionSheetPress = (index) => {
if(index == 0) {
return;
let callbackForOption = (option) => {
ItemActionManager.handleEvent(option.key, this.props.item, () => {
this.forceUpdate();
}, () => {
// afterConfirmCallback
// We want to show "Deleting.." on top of note cell after the user confirms the dialogue
this.forceUpdate();
});
}

ItemActionManager.handleEvent(this.actionSheetActions()[index][1], this.props.item, () => {
this.forceUpdate();
}, () => {
// afterConfirmCallback
// We want to show "Deleting.." on top of note cell after the user confirms the dialogue
this.forceUpdate();
var pinLabel = this.props.item.pinned ? "Unpin" : "Pin";
let pinEvent = pinLabel == "Pin" ? ItemActionManager.PinEvent : ItemActionManager.UnpinEvent;

var archiveLabel = this.props.item.archived ? "Unarchive" : "Archive";
let archiveEvent = archiveLabel == "Archive" ? ItemActionManager.ArchiveEvent : ItemActionManager.UnarchiveEvent;

let sheet = new ActionSheetWrapper({
title: this.props.item.safeTitle(),
options: [
ActionSheetWrapper.BuildOption({text: pinLabel, key: pinEvent, callback: callbackForOption}),
ActionSheetWrapper.BuildOption({text: archiveLabel, key: archiveEvent, callback: callbackForOption}),
ActionSheetWrapper.BuildOption({text: "Share", key: ItemActionManager.ShareEvent, callback: callbackForOption}),
ActionSheetWrapper.BuildOption({text: "Delete", key: ItemActionManager.DeleteEvent, destructive: true, callback: callbackForOption}),
], onCancel: () => {
this.setState({actionSheet: null});
}
});

this.setState({actionSheet: sheet.actionSheetElement()});
sheet.show();
}

render() {
Expand Down Expand Up @@ -150,15 +147,7 @@ export default class NoteCell extends ThemedPureComponent {
</Text>
}

<ActionSheet
ref={o => this.actionSheet = o}
title={note.safeTitle()}
options={this.actionSheetActions().map((action) => {return action[0]})}
cancelButtonIndex={NoteCell.ActionSheetCancelIndex}
destructiveButtonIndex={NoteCell.ActionSheetDestructiveIndex}
onPress={this.handleActionSheetPress}
{...StyleKit.actionSheetStyles()}
/>
{this.state.actionSheet && this.state.actionSheet}
</View>
</TouchableWithoutFeedback>
)
Expand Down
27 changes: 23 additions & 4 deletions src/lib/componentManager.js
Expand Up @@ -366,6 +366,14 @@ export default class ComponentManager {
this.sendMessageToComponent(component, {action: "themes", data: data})
}

getEditors() {
return this.componentsForArea("editor-editor");
}

getDefaultEditor() {
return this.getEditors().filter((e) => {return e.content.isMobileDefault})[0];
}

editorForNote(note) {
let editors = ModelManager.get().validItemsForContentType("SN|Component").filter(function(component){
return component.area == "editor-editor";
Expand All @@ -376,6 +384,17 @@ export default class ComponentManager {
return editor;
}
}

// No editor found for note. Use default editor, if note does not prefer system editor
if(!note.content.mobilePrefersPlainEditor) {
return editors.filter((e) => {return e.content.isMobileDefault})[0];
}
}

setEditorAsMobileDefault(editor, isDefault) {
editor.content.isMobileDefault = isDefault;
editor.setDirty(true);
Sync.get().sync();
}

associateEditorWithNote(editor, note) {
Expand All @@ -392,8 +411,8 @@ export default class ComponentManager {
}

if(editor) {
if(note.getAppDataItem("prefersPlainEditor") == true) {
note.setAppDataItem("prefersPlainEditor", false);
if(note.content.mobilePrefersPlainEditor == true) {
note.content.mobilePrefersPlainEditor = false;
note.setDirty(true);
}

Expand All @@ -406,8 +425,8 @@ export default class ComponentManager {
editor.setDirty(true);
} else {
// Note prefers plain editor
if(!note.getAppDataItem("prefersPlainEditor")) {
note.setAppDataItem("prefersPlainEditor", true);
if(!note.content.mobilePrefersPlainEditor) {
note.content.mobilePrefersPlainEditor = true;
note.setDirty(true);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/itemActionManager.js
Expand Up @@ -23,6 +23,8 @@ export default class ItemActionManager {

static handleEvent(event, item, callback, afterConfirmCallback) {

console.log("Handling event", event);

if(event == this.DeleteEvent) {
var title = `Delete ${item.displayName}`;
var message = `Are you sure you want to delete this ${item.displayName.toLowerCase()}?`;
Expand Down
41 changes: 29 additions & 12 deletions src/screens/Compose.js
Expand Up @@ -50,11 +50,15 @@ export default class Compose extends Abstract {
if(noteId) { note = ModelManager.get().findItem(noteId);}
if(!note) {
note = ModelManager.get().createItem({content_type: "Note", dummy: true, text: ""});
// We needed to add the item originally for default editors to work, but default editors was removed
// So the only way to select an editor is to make a change to the note, which will add it.
// The problem with adding it here is that if you open Compose and close it without making changes, it will save an empty note.
// ModelManager.get().addItem(note);
note.dummy = true;
// Editors need a valid note with uuid and modelmanager mapped in order to interact with it
// Note that this can create dummy notes that aren't deleted automatically.
// Also useful to keep right menu enabled at all times. If the note has a uuid and is a dummy,
// it will be removed locally on blur
note.initUUID().then(() => {
ModelManager.get().addItem(note);
this.forceUpdate();
})
}

this.note = note;
Expand Down Expand Up @@ -99,8 +103,7 @@ export default class Compose extends Abstract {
iconName: "ios-menu-outline",
onPress: () => {
this.props.navigation.openRightDrawer();
},
disabled: !this.note.uuid
}
}
})
}
Expand All @@ -117,10 +120,6 @@ export default class Compose extends Abstract {

componentWillFocus() {
super.componentWillFocus();
if(this.needsEditorReload) {
this.forceUpdate();
this.needsEditorReload = false;
}

if(this.note.dirty) {
// We want the "Saving..." / "All changes saved..." subtitle to be visible to the user, so we delay
Expand Down Expand Up @@ -149,8 +148,17 @@ export default class Compose extends Abstract {
}

SideMenuManager.get().setHandlerForRightSideMenu({
getCurrentNote: () => {
return this.note
},
onEditorSelect: (editor) => {
this.needsEditorReload = true;
if(editor) {
ComponentManager.get().associateEditorWithNote(editor, this.note);
} else {
ComponentManager.get().clearEditorForNote(this.note);
}
this.forceUpdate();
this.props.navigation.closeRightDrawer();
},
onTagSelect: (tag) => {
let selectedTags = this.note.tags;
Expand All @@ -172,6 +180,14 @@ export default class Compose extends Abstract {
})
}

componentWillBlur() {
super.componentWillBlur();
if(this.note.uuid && this.note.dummy) {
// A dummy note created to work with default external editor. Safe to delete.
ModelManager.get().removeItemLocally(this.note);
}
}

componentDidBlur() {
super.componentDidBlur();

Expand Down Expand Up @@ -312,7 +328,8 @@ export default class Compose extends Abstract {

var noteEditor = ComponentManager.get().editorForNote(this.note);
let windowWidth = this.state.windowWidth || Dimensions.get('window').width;
var shouldDisplayEditor = noteEditor != null;
// If new note with default editor, note.uuid may not be ready
var shouldDisplayEditor = noteEditor != null && this.note.uuid;

return (
<View style={[this.styles.container, StyleKit.styles().container]}>
Expand Down
53 changes: 0 additions & 53 deletions src/screens/NoteOptions.js
Expand Up @@ -173,32 +173,11 @@ export default class NoteOptions extends Abstract {
})
}

onEditorSelect = (editor) => {
if(editor) {
ComponentManager.get().associateEditorWithNote(editor, this.note);
} else {
ComponentManager.get().clearEditorForNote(this.note);
}

this.getProp("onEditorSelect") && this.getProp("onEditorSelect")(editor);
this.dismiss();
}

getEditors() {
return ModelManager.get().validItemsForContentType("SN|Component").filter((component) => {
return component.area == "editor-editor";
})
}

clearTags = (close) => {
this.setSelectedTags([]);
if(close) { this.dismiss(); }
}

openExternalEditorsLink() {
Linking.openURL("https://standardnotes.org/extensions");
}

render() {
var viewStyles = [StyleKit.styles().container];

Expand All @@ -221,9 +200,6 @@ export default class NoteOptions extends Abstract {
var lockOption = this.note.locked ? "Unlock" : "Lock";
let lockEvent = lockOption == "Lock" ? ItemActionManager.LockEvent : ItemActionManager.UnlockEvent;

let editors = this.getEditors();
let selectedEditor = ComponentManager.get().editorForNote(this.note);

return (
<View style={viewStyles}>
<ScrollView style={StyleKit.styles().view}>
Expand Down Expand Up @@ -267,35 +243,6 @@ export default class NoteOptions extends Abstract {
/>
</TableSection>

<TableSection style={StyleKit.styles().view}>
<SectionHeader
title={"Edit With"}
buttonText={selectedEditor && "Use Plain"}
buttonAction={() => {this.onEditorSelect(null)}}
/>
{editors.map((editor, i) => {
return (
<SectionedAccessoryTableCell
onPress={() => {this.onEditorSelect(editor)}}
text={editor.name}
key={editor.uuid}
first={i == 0}
selected={() => {return editor == selectedEditor}}
buttonCell={true}
/>
)
})}

{editors.length == 0 &&
<SectionedAccessoryTableCell
onPress={() => {this.openExternalEditorsLink()}}
text={"Get Editors →"}
first={true}
buttonCell={true}
/>
}
</TableSection>

<TagList
tags={this.tags}
selected={this.state.selectedTags}
Expand Down

0 comments on commit 4414469

Please sign in to comment.