2018-09-16 20:37:31 +02:00
|
|
|
const React = require('react');
|
2020-11-07 17:59:37 +02:00
|
|
|
const { _ } = require('@joplin/lib/locale');
|
|
|
|
const { themeStyle } = require('@joplin/lib/theme');
|
|
|
|
const time = require('@joplin/lib/time').default;
|
2021-05-13 18:57:37 +02:00
|
|
|
const DialogButtonRow = require('./DialogButtonRow').default;
|
2018-09-16 20:37:31 +02:00
|
|
|
const Datetime = require('react-datetime');
|
2021-01-22 19:41:11 +02:00
|
|
|
const Note = require('@joplin/lib/models/Note').default;
|
2018-09-16 20:37:31 +02:00
|
|
|
const formatcoords = require('formatcoords');
|
2021-10-01 20:35:27 +02:00
|
|
|
const bridge = require('@electron/remote').require('./bridge').default;
|
2020-11-07 17:59:37 +02:00
|
|
|
const shim = require('@joplin/lib/shim').default;
|
2021-03-29 22:25:17 +02:00
|
|
|
const { clipboard } = require('electron');
|
2018-09-16 20:37:31 +02:00
|
|
|
|
|
|
|
class NotePropertiesDialog extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
this.revisionsLink_click = this.revisionsLink_click.bind(this);
|
2019-12-13 03:16:34 +02:00
|
|
|
this.buttonRow_click = this.buttonRow_click.bind(this);
|
2019-01-16 22:19:48 +02:00
|
|
|
this.okButton = React.createRef();
|
2018-09-16 20:37:31 +02:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
formNote: null,
|
|
|
|
editedKey: null,
|
|
|
|
editedValue: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.keyToLabel_ = {
|
|
|
|
id: _('ID'),
|
|
|
|
user_created_time: _('Created'),
|
|
|
|
user_updated_time: _('Updated'),
|
2020-01-18 15:53:59 +02:00
|
|
|
todo_completed: _('Completed'),
|
2018-09-16 20:37:31 +02:00
|
|
|
location: _('Location'),
|
|
|
|
source_url: _('URL'),
|
2019-05-06 22:35:29 +02:00
|
|
|
revisionsLink: _('Note History'),
|
2019-07-17 23:49:40 +02:00
|
|
|
markup_language: _('Markup'),
|
2018-09-16 20:37:31 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-12 23:52:23 +02:00
|
|
|
componentDidMount() {
|
|
|
|
this.loadNote(this.props.noteId);
|
2018-09-16 20:37:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-16 22:19:48 +02:00
|
|
|
componentDidUpdate() {
|
2022-07-23 11:33:12 +02:00
|
|
|
if (this.state.editedKey === null) {
|
2019-01-16 22:19:48 +02:00
|
|
|
this.okButton.current.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 20:37:31 +02:00
|
|
|
async loadNote(noteId) {
|
|
|
|
if (!noteId) {
|
|
|
|
this.setState({ formNote: null });
|
|
|
|
} else {
|
|
|
|
const note = await Note.load(noteId);
|
|
|
|
const formNote = this.noteToFormNote(note);
|
|
|
|
this.setState({ formNote: formNote });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
latLongFromLocation(location) {
|
|
|
|
const o = {};
|
|
|
|
const l = location.split(',');
|
2022-07-23 11:33:12 +02:00
|
|
|
if (l.length === 2) {
|
2018-09-16 20:37:31 +02:00
|
|
|
o.latitude = l[0].trim();
|
|
|
|
o.longitude = l[1].trim();
|
|
|
|
} else {
|
|
|
|
o.latitude = '';
|
|
|
|
o.longitude = '';
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
noteToFormNote(note) {
|
|
|
|
const formNote = {};
|
|
|
|
|
|
|
|
formNote.user_updated_time = time.formatMsToLocal(note.user_updated_time);
|
|
|
|
formNote.user_created_time = time.formatMsToLocal(note.user_created_time);
|
2020-01-18 15:53:59 +02:00
|
|
|
|
|
|
|
if (note.todo_completed) {
|
|
|
|
formNote.todo_completed = time.formatMsToLocal(note.todo_completed);
|
|
|
|
}
|
|
|
|
|
2018-09-16 20:37:31 +02:00
|
|
|
formNote.source_url = note.source_url;
|
|
|
|
|
|
|
|
formNote.location = '';
|
|
|
|
if (Number(note.latitude) || Number(note.longitude)) {
|
2019-09-19 23:51:18 +02:00
|
|
|
formNote.location = `${note.latitude}, ${note.longitude}`;
|
2018-09-16 20:37:31 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
formNote.revisionsLink = note.id;
|
2019-07-17 23:49:40 +02:00
|
|
|
formNote.markup_language = Note.markupLanguageToLabel(note.markup_language);
|
2018-09-16 20:37:31 +02:00
|
|
|
formNote.id = note.id;
|
|
|
|
|
|
|
|
return formNote;
|
|
|
|
}
|
|
|
|
|
|
|
|
formNoteToNote(formNote) {
|
|
|
|
const note = Object.assign({ id: formNote.id }, this.latLongFromLocation(formNote.location));
|
|
|
|
note.user_created_time = time.formatLocalToMs(formNote.user_created_time);
|
|
|
|
note.user_updated_time = time.formatLocalToMs(formNote.user_updated_time);
|
2020-01-18 15:53:59 +02:00
|
|
|
|
|
|
|
if (formNote.todo_completed) {
|
|
|
|
note.todo_completed = time.formatMsToLocal(formNote.todo_completed);
|
|
|
|
}
|
|
|
|
|
2018-09-16 20:37:31 +02:00
|
|
|
note.source_url = formNote.source_url;
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
styles(themeId) {
|
|
|
|
const styleKey = themeId;
|
|
|
|
if (styleKey === this.styleKey_) return this.styles_;
|
|
|
|
|
|
|
|
const theme = themeStyle(themeId);
|
|
|
|
|
|
|
|
this.styles_ = {};
|
|
|
|
this.styleKey_ = styleKey;
|
|
|
|
|
|
|
|
this.styles_.button = {
|
|
|
|
minWidth: theme.buttonMinWidth,
|
|
|
|
minHeight: theme.buttonMinHeight,
|
|
|
|
marginLeft: 5,
|
2018-11-20 00:48:10 +02:00
|
|
|
color: theme.color,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
border: '1px solid',
|
|
|
|
borderColor: theme.dividerColor,
|
2018-09-16 20:37:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.styles_.editPropertyButton = {
|
|
|
|
color: theme.color,
|
|
|
|
textDecoration: 'none',
|
2018-11-20 00:48:10 +02:00
|
|
|
backgroundColor: theme.backgroundColor,
|
2020-01-23 22:18:21 +02:00
|
|
|
padding: '.14em',
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
marginLeft: '0.5em',
|
2018-11-20 00:48:10 +02:00
|
|
|
};
|
2019-07-29 14:13:23 +02:00
|
|
|
|
2018-11-20 00:48:10 +02:00
|
|
|
this.styles_.input = {
|
2019-07-29 14:13:23 +02:00
|
|
|
display: 'inline-block',
|
2018-11-20 00:48:10 +02:00
|
|
|
color: theme.color,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
border: '1px solid',
|
|
|
|
borderColor: theme.dividerColor,
|
2018-09-16 20:37:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return this.styles_;
|
|
|
|
}
|
|
|
|
|
|
|
|
async closeDialog(applyChanges) {
|
|
|
|
if (applyChanges) {
|
|
|
|
await this.saveProperty();
|
|
|
|
const note = this.formNoteToNote(this.state.formNote);
|
|
|
|
note.updated_time = Date.now();
|
|
|
|
await Note.save(note, { autoTimestamp: false });
|
|
|
|
} else {
|
|
|
|
await this.cancelProperty();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.onClose) {
|
|
|
|
this.props.onClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 03:16:34 +02:00
|
|
|
buttonRow_click(event) {
|
|
|
|
this.closeDialog(event.buttonName === 'ok');
|
2018-09-16 20:37:31 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
revisionsLink_click() {
|
|
|
|
this.closeDialog(false);
|
|
|
|
if (this.props.onRevisionLinkClick) this.props.onRevisionLinkClick();
|
|
|
|
}
|
|
|
|
|
2018-09-16 20:37:31 +02:00
|
|
|
editPropertyButtonClick(key, initialValue) {
|
|
|
|
this.setState({
|
|
|
|
editedKey: key,
|
|
|
|
editedValue: initialValue,
|
|
|
|
});
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
shim.setTimeout(() => {
|
2018-09-16 20:37:31 +02:00
|
|
|
if (this.refs.editField.openCalendar) {
|
|
|
|
this.refs.editField.openCalendar();
|
|
|
|
} else {
|
|
|
|
this.refs.editField.focus();
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveProperty() {
|
|
|
|
if (!this.state.editedKey) return;
|
|
|
|
|
2019-09-13 00:16:42 +02:00
|
|
|
return new Promise((resolve) => {
|
2018-09-16 20:37:31 +02:00
|
|
|
const newFormNote = Object.assign({}, this.state.formNote);
|
|
|
|
|
|
|
|
if (this.state.editedKey.indexOf('_time') >= 0) {
|
|
|
|
const dt = time.anythingToDateTime(this.state.editedValue, new Date());
|
|
|
|
newFormNote[this.state.editedKey] = time.formatMsToLocal(dt.getTime());
|
|
|
|
} else {
|
|
|
|
newFormNote[this.state.editedKey] = this.state.editedValue;
|
|
|
|
}
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
formNote: newFormNote,
|
|
|
|
editedKey: null,
|
|
|
|
editedValue: null,
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
);
|
2018-09-16 20:37:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async cancelProperty() {
|
2019-09-13 00:16:42 +02:00
|
|
|
return new Promise((resolve) => {
|
2019-01-16 22:19:48 +02:00
|
|
|
this.okButton.current.focus();
|
2019-12-13 03:16:34 +02:00
|
|
|
this.setState({
|
|
|
|
editedKey: null,
|
|
|
|
editedValue: null,
|
|
|
|
}, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
2018-09-16 20:37:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createNoteField(key, value) {
|
2020-09-15 15:01:07 +02:00
|
|
|
const styles = this.styles(this.props.themeId);
|
|
|
|
const theme = themeStyle(this.props.themeId);
|
2020-02-25 11:43:31 +02:00
|
|
|
const labelComp = <label style={Object.assign({}, theme.textStyle, theme.controlBoxLabel)}>{this.formatLabel(key)}</label>;
|
2018-09-16 20:37:31 +02:00
|
|
|
let controlComp = null;
|
|
|
|
let editComp = null;
|
|
|
|
let editCompHandler = null;
|
|
|
|
let editCompIcon = null;
|
|
|
|
|
2020-05-21 10:14:33 +02:00
|
|
|
const onKeyDown = event => {
|
2018-09-16 20:37:31 +02:00
|
|
|
if (event.keyCode === 13) {
|
|
|
|
this.saveProperty();
|
|
|
|
} else if (event.keyCode === 27) {
|
|
|
|
this.cancelProperty();
|
|
|
|
}
|
2019-07-29 14:13:23 +02:00
|
|
|
};
|
2018-09-16 20:37:31 +02:00
|
|
|
|
|
|
|
if (this.state.editedKey === key) {
|
|
|
|
if (key.indexOf('_time') >= 0) {
|
2019-07-29 14:13:23 +02:00
|
|
|
controlComp = (
|
|
|
|
<Datetime
|
|
|
|
ref="editField"
|
|
|
|
defaultValue={value}
|
|
|
|
dateFormat={time.dateFormat()}
|
|
|
|
timeFormat={time.timeFormat()}
|
|
|
|
inputProps={{
|
2020-05-21 10:14:33 +02:00
|
|
|
onKeyDown: event => onKeyDown(event, key),
|
2019-07-29 14:13:23 +02:00
|
|
|
style: styles.input,
|
|
|
|
}}
|
2020-05-21 10:14:33 +02:00
|
|
|
onChange={momentObject => {
|
2019-07-29 14:13:23 +02:00
|
|
|
this.setState({ editedValue: momentObject });
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
editCompHandler = () => {
|
|
|
|
this.saveProperty();
|
|
|
|
};
|
2018-09-16 20:37:31 +02:00
|
|
|
editCompIcon = 'fa-save';
|
|
|
|
} else {
|
2019-07-29 14:13:23 +02:00
|
|
|
controlComp = (
|
|
|
|
<input
|
|
|
|
defaultValue={value}
|
|
|
|
type="text"
|
|
|
|
ref="editField"
|
2020-05-21 10:14:33 +02:00
|
|
|
onChange={event => {
|
2019-07-29 14:13:23 +02:00
|
|
|
this.setState({ editedValue: event.target.value });
|
|
|
|
}}
|
2020-05-21 10:14:33 +02:00
|
|
|
onKeyDown={event => onKeyDown(event)}
|
2019-07-29 14:13:23 +02:00
|
|
|
style={styles.input}
|
|
|
|
/>
|
|
|
|
);
|
2018-09-16 20:37:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let displayedValue = value;
|
|
|
|
|
|
|
|
if (key === 'location') {
|
|
|
|
try {
|
|
|
|
const dms = formatcoords(value);
|
2020-02-11 11:49:48 +02:00
|
|
|
displayedValue = dms.format('DD MM ss X', { latLonSeparator: ', ', decimalPlaces: 2 });
|
2018-09-16 20:37:31 +02:00
|
|
|
} catch (error) {
|
|
|
|
displayedValue = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['source_url', 'location'].indexOf(key) >= 0) {
|
|
|
|
let url = '';
|
|
|
|
if (key === 'source_url') url = value;
|
|
|
|
if (key === 'location') {
|
|
|
|
const ll = this.latLongFromLocation(value);
|
|
|
|
url = Note.geoLocationUrlFromLatLong(ll.latitude, ll.longitude);
|
|
|
|
}
|
2020-05-09 12:53:16 +02:00
|
|
|
const urlStyle = Object.assign({}, theme.urlStyle, { maxWidth: '180px', overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'ellipsis' });
|
2019-07-29 14:13:23 +02:00
|
|
|
controlComp = (
|
2020-05-09 12:53:16 +02:00
|
|
|
<a href="#" onClick={() => bridge().openExternal(url)} style={urlStyle}>
|
2019-07-29 14:13:23 +02:00
|
|
|
{displayedValue}
|
|
|
|
</a>
|
|
|
|
);
|
2019-05-06 22:35:29 +02:00
|
|
|
} else if (key === 'revisionsLink') {
|
2019-07-29 14:13:23 +02:00
|
|
|
controlComp = (
|
|
|
|
<a href="#" onClick={this.revisionsLink_click} style={theme.urlStyle}>
|
|
|
|
{_('Previous versions of this note')}
|
|
|
|
</a>
|
|
|
|
);
|
2018-09-16 20:37:31 +02:00
|
|
|
} else {
|
2020-02-25 11:43:31 +02:00
|
|
|
controlComp = <div style={Object.assign({}, theme.textStyle, theme.controlBoxValue)}>{displayedValue}</div>;
|
2018-09-16 20:37:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 23:49:40 +02:00
|
|
|
if (['id', 'revisionsLink', 'markup_language'].indexOf(key) < 0) {
|
2019-07-29 14:13:23 +02:00
|
|
|
editCompHandler = () => {
|
|
|
|
this.editPropertyButtonClick(key, value);
|
|
|
|
};
|
2018-09-16 20:37:31 +02:00
|
|
|
editCompIcon = 'fa-edit';
|
|
|
|
}
|
2021-03-29 22:25:17 +02:00
|
|
|
|
|
|
|
// Add the copy icon and the 'copy on click' event
|
|
|
|
if (key === 'id') {
|
|
|
|
editCompIcon = 'fa-copy';
|
|
|
|
editCompHandler = () => clipboard.writeText(value);
|
|
|
|
}
|
2018-09-16 20:37:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (editCompHandler) {
|
|
|
|
editComp = (
|
|
|
|
<a href="#" onClick={editCompHandler} style={styles.editPropertyButton}>
|
2020-05-17 16:34:42 +02:00
|
|
|
<i className={`fas ${editCompIcon}`} aria-hidden="true"></i>
|
2018-09-16 20:37:31 +02:00
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-02-25 11:43:31 +02:00
|
|
|
<div key={key} style={theme.controlBox} className="note-property-box">
|
2019-07-29 14:13:23 +02:00
|
|
|
{labelComp}
|
|
|
|
{controlComp}
|
|
|
|
{editComp}
|
2018-09-16 20:37:31 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
formatLabel(key) {
|
|
|
|
if (this.keyToLabel_[key]) return this.keyToLabel_[key];
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatValue(key, note) {
|
|
|
|
if (key === 'location') {
|
|
|
|
if (!Number(note.latitude) && !Number(note.longitude)) return null;
|
2019-07-29 14:13:23 +02:00
|
|
|
const dms = formatcoords(Number(note.latitude), Number(note.longitude));
|
2018-09-16 20:37:31 +02:00
|
|
|
return dms.format('DDMMss', { decimalPlaces: 0 });
|
|
|
|
}
|
|
|
|
|
2020-01-18 15:53:59 +02:00
|
|
|
if (['user_updated_time', 'user_created_time', 'todo_completed'].indexOf(key) >= 0) {
|
2018-09-16 20:37:31 +02:00
|
|
|
return time.formatMsToLocal(note[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return note[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-09-15 15:01:07 +02:00
|
|
|
const theme = themeStyle(this.props.themeId);
|
2018-09-16 20:37:31 +02:00
|
|
|
const formNote = this.state.formNote;
|
|
|
|
|
|
|
|
const noteComps = [];
|
|
|
|
|
|
|
|
if (formNote) {
|
2020-03-14 01:46:14 +02:00
|
|
|
for (const key in formNote) {
|
2018-09-16 20:37:31 +02:00
|
|
|
if (!formNote.hasOwnProperty(key)) continue;
|
|
|
|
const comp = this.createNoteField(key, formNote[key]);
|
|
|
|
noteComps.push(comp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-04-01 21:43:13 +02:00
|
|
|
<div style={theme.dialogModalLayer}>
|
|
|
|
<div style={theme.dialogBox}>
|
|
|
|
<div style={theme.dialogTitle}>{_('Note properties')}</div>
|
2018-09-16 20:37:31 +02:00
|
|
|
<div>{noteComps}</div>
|
2020-09-15 15:01:07 +02:00
|
|
|
<DialogButtonRow themeId={this.props.themeId} okButtonRef={this.okButton} onClick={this.buttonRow_click}/>
|
2018-09-16 20:37:31 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 02:24:28 +02:00
|
|
|
module.exports = NotePropertiesDialog;
|