Hooks
This commit is contained in:
@@ -17,6 +17,8 @@ import {
|
|||||||
matchDocumentEdit,
|
matchDocumentEdit,
|
||||||
matchDocumentMove,
|
matchDocumentMove,
|
||||||
} from 'utils/routeHelpers';
|
} from 'utils/routeHelpers';
|
||||||
|
import { uploadFile } from 'utils/uploadFile';
|
||||||
|
import isInternalUrl from 'utils/isInternalUrl';
|
||||||
|
|
||||||
import Document from 'models/Document';
|
import Document from 'models/Document';
|
||||||
import Actions from './components/Actions';
|
import Actions from './components/Actions';
|
||||||
@@ -201,9 +203,32 @@ class DocumentScene extends React.Component {
|
|||||||
this.props.history.push(url);
|
this.props.history.push(url);
|
||||||
};
|
};
|
||||||
|
|
||||||
renderNotFound() {
|
onUploadImage = async (file: File) => {
|
||||||
return <Search notFound />;
|
const result = await uploadFile(file);
|
||||||
}
|
return result.url;
|
||||||
|
};
|
||||||
|
|
||||||
|
onSearchLink = async (term: string) => {
|
||||||
|
const resultIds = await this.props.documents.search(term);
|
||||||
|
|
||||||
|
return resultIds.map((id, index) => {
|
||||||
|
const document = this.props.documents.getById(id);
|
||||||
|
if (!document) return {};
|
||||||
|
|
||||||
|
return {
|
||||||
|
title: document.title,
|
||||||
|
url: document.url,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
onClickLink = (href: string) => {
|
||||||
|
if (isInternalUrl(href)) {
|
||||||
|
this.props.history.push(href);
|
||||||
|
} else {
|
||||||
|
window.open(href, '_blank');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const Editor = this.editorComponent;
|
const Editor = this.editorComponent;
|
||||||
@@ -214,7 +239,7 @@ class DocumentScene extends React.Component {
|
|||||||
this.props.collections.titleForDocument(this.props.location.pathname);
|
this.props.collections.titleForDocument(this.props.location.pathname);
|
||||||
|
|
||||||
if (this.notFound) {
|
if (this.notFound) {
|
||||||
return this.renderNotFound();
|
return <Search notFound />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -240,8 +265,11 @@ class DocumentScene extends React.Component {
|
|||||||
bodyPlaceholder="…the rest is your canvas"
|
bodyPlaceholder="…the rest is your canvas"
|
||||||
defaultValue={document.text}
|
defaultValue={document.text}
|
||||||
pretitle={document.emoji}
|
pretitle={document.emoji}
|
||||||
|
uploadImage={this.onUploadImage}
|
||||||
onImageUploadStart={this.onImageUploadStart}
|
onImageUploadStart={this.onImageUploadStart}
|
||||||
onImageUploadStop={this.onImageUploadStop}
|
onImageUploadStop={this.onImageUploadStop}
|
||||||
|
onSearchLink={this.onSearchLink}
|
||||||
|
onClickLink={this.onClickLink}
|
||||||
onChange={this.onChange}
|
onChange={this.onChange}
|
||||||
onSave={this.onSave}
|
onSave={this.onSave}
|
||||||
onCancel={this.onDiscard}
|
onCancel={this.onDiscard}
|
||||||
|
|||||||
12
app/utils/isInternalUrl.js
Normal file
12
app/utils/isInternalUrl.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
// @flow
|
||||||
|
export default function isInternalUrl(href: string) {
|
||||||
|
if (href[0] === '/') return true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const outline = new URL(BASE_URL);
|
||||||
|
const parsed = new URL(href);
|
||||||
|
return parsed.hostname === outline.hostname;
|
||||||
|
} catch (err) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user