Skip to content

Commit

Permalink
fix: file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Dec 8, 2023
1 parent 29cf551 commit 6ecae4f
Showing 1 changed file with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SNTag } from '@standardnotes/models'
import { FileItem, SNTag } from '@standardnotes/models'
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import utc from 'dayjs/plugin/utc'
Expand Down Expand Up @@ -80,7 +80,12 @@ export class EvernoteConverter implements Converter {
this.removeUnnecessaryTopLevelBreaks(noteElement)

const mediaElements = Array.from(noteElement.getElementsByTagName('en-media'))
await this.replaceMediaElementsWithResources(mediaElements, resources, canUploadFiles, uploadFile)
const { uploadedFiles } = await this.replaceMediaElementsWithResources(
mediaElements,
resources,
canUploadFiles,
uploadFile,
)

// Some notes have <font> tags that contain separate <span> tags with text
// which causes broken paragraphs in the note.
Expand Down Expand Up @@ -111,6 +116,10 @@ export class EvernoteConverter implements Converter {
useSuperIfPossible: canUseSuper,
})

for (const uploadedFile of uploadedFiles) {
await linkItems(note, uploadedFile)
}

const xmlTags = xmlNote.getElementsByTagName('tag')
for (const tagXml of Array.from(xmlTags)) {
const tagName = tagXml.childNodes[0].nodeValue
Expand Down Expand Up @@ -293,8 +302,12 @@ export class EvernoteConverter implements Converter {
resources: EvernoteResource[],
canUploadFiles: boolean,
uploadFile: UploadFileFn,
): Promise<number> {
let replacedElements = 0
): Promise<{
replacedElements: HTMLElement[]
uploadedFiles: FileItem[]
}> {
const replacedElements: HTMLElement[] = []
const uploadedFiles: FileItem[] = []
for (const mediaElement of mediaElements) {
const hash = mediaElement.getAttribute('hash')
const resource = resources.find((resource) => resource && resource.hash === hash)
Expand All @@ -307,16 +320,21 @@ export class EvernoteConverter implements Converter {
const fileToUpload = canUploadFiles ? await this.getFileFromResource(resource) : undefined
const fileItem = fileToUpload ? await uploadFile(fileToUpload) : undefined
if (fileItem) {
const fileElement = document.createElement('span')
const fileElement = document.createElement('div')
fileElement.setAttribute('data-lexical-file-uuid', fileItem.uuid)
mediaElement.parentNode.replaceChild(fileElement, mediaElement)
} else {
const resourceElement = this.getHTMLElementFromResource(resource)
mediaElement.parentNode.replaceChild(resourceElement, mediaElement)
replacedElements.push(fileElement)
uploadedFiles.push(fileItem)
continue
}
replacedElements++
const resourceElement = this.getHTMLElementFromResource(resource)
mediaElement.parentNode.replaceChild(resourceElement, mediaElement)
replacedElements.push(resourceElement)
}
return {
replacedElements,
uploadedFiles,
}
return replacedElements
}

loadXMLString(string: string, type: 'html' | 'xml') {
Expand Down

0 comments on commit 6ecae4f

Please sign in to comment.