fix: Allow selection of embeds (#1562)
* feat: Support importing .docx or .html files as new documents (#1551) * Support importing .docx as new documents * Add html file support, build types and interface for easily adding file types to importer * fix: Upload embedded images in docx to storage * refactor: Bulk of logic to command * refactor: Do all importing on server, so we're not splitting logic for import into two places * test: Add documentImporter tests Co-authored-by: Lance Whatley <whatl3y@gmail.com> * fix: Accessibility audit * fix: Quick fix, non editable title closes #1560 * fix: Embed selection Co-authored-by: Lance Whatley <whatl3y@gmail.com>
This commit is contained in:
@@ -28,12 +28,13 @@ class ApiClient {
|
||||
fetch = async (
|
||||
path: string,
|
||||
method: string,
|
||||
data: ?Object,
|
||||
data: ?Object | FormData | void,
|
||||
options: Object = {}
|
||||
) => {
|
||||
let body;
|
||||
let modifiedPath;
|
||||
let urlToFetch;
|
||||
let isJson;
|
||||
|
||||
if (method === "GET") {
|
||||
if (data) {
|
||||
@@ -42,7 +43,18 @@ class ApiClient {
|
||||
modifiedPath = path;
|
||||
}
|
||||
} else if (method === "POST" || method === "PUT") {
|
||||
body = data ? JSON.stringify(data) : undefined;
|
||||
body = data || undefined;
|
||||
|
||||
// Only stringify data if its a normal object and
|
||||
// not if it's [object FormData], in addition to
|
||||
// toggling Content-Type to application/json
|
||||
if (
|
||||
typeof data === "object" &&
|
||||
(data || "").toString() === "[object Object]"
|
||||
) {
|
||||
isJson = true;
|
||||
body = JSON.stringify(data);
|
||||
}
|
||||
}
|
||||
|
||||
if (path.match(/^http/)) {
|
||||
@@ -51,14 +63,20 @@ class ApiClient {
|
||||
urlToFetch = this.baseUrl + (modifiedPath || path);
|
||||
}
|
||||
|
||||
// Construct headers
|
||||
const headers = new Headers({
|
||||
let headerOptions: any = {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"cache-control": "no-cache",
|
||||
"x-editor-version": EDITOR_VERSION,
|
||||
pragma: "no-cache",
|
||||
});
|
||||
};
|
||||
// for multipart forms or other non JSON requests fetch
|
||||
// populates the Content-Type without needing to explicitly
|
||||
// set it.
|
||||
if (isJson) {
|
||||
headerOptions["Content-Type"] = "application/json";
|
||||
}
|
||||
const headers = new Headers(headerOptions);
|
||||
|
||||
if (stores.auth.authenticated) {
|
||||
invariant(stores.auth.token, "JWT token not set properly");
|
||||
headers.set("Authorization", `Bearer ${stores.auth.token}`);
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
// @flow
|
||||
import parseTitle from "shared/utils/parseTitle";
|
||||
import DocumentsStore from "stores/DocumentsStore";
|
||||
import Document from "models/Document";
|
||||
|
||||
type Options = {
|
||||
file: File,
|
||||
documents: DocumentsStore,
|
||||
collectionId: string,
|
||||
documentId?: string,
|
||||
};
|
||||
|
||||
const importFile = async ({
|
||||
documents,
|
||||
file,
|
||||
documentId,
|
||||
collectionId,
|
||||
}: Options): Promise<Document> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = async (ev) => {
|
||||
let text = ev.target.result;
|
||||
let title;
|
||||
|
||||
// If the first line of the imported file looks like a markdown heading
|
||||
// then we can use this as the document title
|
||||
if (text.trim().startsWith("# ")) {
|
||||
const result = parseTitle(text);
|
||||
title = result.title;
|
||||
text = text.replace(`# ${title}\n`, "");
|
||||
|
||||
// otherwise, just use the filename without the extension as our best guess
|
||||
} else {
|
||||
title = file.name.replace(/\.[^/.]+$/, "");
|
||||
}
|
||||
|
||||
let document = new Document(
|
||||
{
|
||||
parentDocumentId: documentId,
|
||||
collectionId,
|
||||
text,
|
||||
title,
|
||||
},
|
||||
documents
|
||||
);
|
||||
try {
|
||||
document = await document.save({ publish: true });
|
||||
resolve(document);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
reader.readAsText(file);
|
||||
});
|
||||
};
|
||||
|
||||
export default importFile;
|
||||
Reference in New Issue
Block a user