Skip to content

Commit 42ac3e8

Browse files
raphaelweisnitram35
andcommitted
FEATURE: Upload a picture into a document (see #153).
Co-Authored-By: Martin Gandon <105852593+nitram35@users.noreply.github.com>
1 parent 897b750 commit 42ac3e8

4 files changed

Lines changed: 22 additions & 14 deletions

File tree

frontend/src/components/EditableText.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { v4 as uuid } from 'uuid';
1010
function EditableText({ id, text, rubric, isPartOf, links, fragment, setFragment, setHighlightedText, backend, setLastUpdate }) {
1111
const [beingEdited, setBeingEdited] = useState(false);
1212
const [editedDocument, setEditedDocument] = useState();
13-
const [editedText, setEditedText] = useState();
14-
const PASSAGE = new RegExp(`\\{${rubric}} ?([^{]*)`);
15-
const FIRST_PASSAGE = new RegExp('\\{[^}]+} ?([^{]*)');
13+
const [editedText, setEditedText] = useState(text);
14+
const PASSAGE = new RegExp(`\\{${rubric}} ?([^{]+)`);
15+
const FIRST_PASSAGE = new RegExp('\\{[^}]+} ?([^{]+)');
1616

1717
let parsePassage = (rawText) => rubric ? rawText.match(PASSAGE)[1] : rawText;
1818

@@ -47,12 +47,20 @@ function EditableText({ id, text, rubric, isPartOf, links, fragment, setFragment
4747
updateEditedDocument()
4848
.then((x) => {
4949
setEditedText(parsePassage(x.text));
50+
})
51+
.catch(() => {
52+
setEditedDocument({
53+
_id: uuid(),
54+
text: `{${rubric}} ${text}`,
55+
isPartOf,
56+
links
57+
});
5058
});
5159
};
5260

5361
let handleImageUrl = (imageTag) => {
5462
backend.getDocument(id).then((editedDocument) => {
55-
let parsedText = parsePassage(editedDocument.text) + imageTag;
63+
let parsedText = parseFirstPassage(editedText) + imageTag;
5664
let text = (rubric)
5765
? editedDocument.text.replace(PASSAGE, `{${rubric}} ${parsedText}`)
5866
: parsedText;

frontend/src/components/PassageMarginMenu.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import '../styles/PassageMarginMenu.css';
2-
31
import { Dropdown } from 'react-bootstrap';
42
import { forwardRef, useRef } from 'react';
53
import { ThreeDotsVertical } from 'react-bootstrap-icons';
@@ -14,7 +12,7 @@ function PassageMarginMenu({ id, backend, handleImageUrl }) {
1412
const handleFileChange = (event) => {
1513
const file = event.target.files[0];
1614
if (file) backend.putAttachment(id, file, (response) => {
17-
handleImageUrl(`![<IMAGE DESCRIPTION>](${response.url})`);
15+
handleImageUrl(`![${file.name}](${response.url})`);
1816
});
1917
};
2018

@@ -37,7 +35,7 @@ function PassageMarginMenu({ id, backend, handleImageUrl }) {
3735
);
3836
}
3937

40-
const BlockMenuButton = forwardRef(({ children, onClick }, ref) => (
38+
const BlockMenuButton = React.forwardRef(({ children, onClick }, ref) => (
4139
<ThreeDotsVertical
4240
onClick={(e) => {
4341
e.preventDefault();

frontend/src/hyperglosae.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ function Hyperglosae(logger) {
4242
return x;
4343
});
4444

45-
this.getDocumentMetadata = (id) =>
46-
fetch(`${service}/${id}`, {
45+
this.getDocumentMetadata = (id) => {
46+
return fetch(`${service}/${id}`, {
4747
method: 'HEAD',
4848
headers: basicAuthentication({ force: false })
4949
});
50+
};
5051

51-
this.putAttachment = (id, attachment, callback) =>
52+
this.putAttachment = (id, attachment, callback) => {
5253
this.getDocumentMetadata(id).then(x => {
5354
const reader = new FileReader();
5455
reader.readAsArrayBuffer(attachment);
@@ -67,9 +68,10 @@ function Hyperglosae(logger) {
6768
}).then(response => callback(response));
6869
};
6970
});
71+
};
7072

71-
this.authenticate = ({name, password}) => {
72-
this.credentials = {name, password};
73+
this.authenticate = ({ name, password }) => {
74+
this.credentials = { name, password };
7375
return fetch(`${service}`, {
7476
method: 'GET',
7577
headers: basicAuthentication({ force: true })

frontend/src/styles/EditableText.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.content {
1+
.editable {
22
display: flex;
33
}
44

0 commit comments

Comments
 (0)