Skip to content

Commit

Permalink
Simplify AttachFile workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
obuchtala committed Apr 23, 2020
1 parent dda2ee6 commit 4bc7fa6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
20 changes: 8 additions & 12 deletions src/components/AttachFileModal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Component, $$, Form, FormRow, Modal, domHelpers,
MultiSelect, AssetModal, HorizontalStack
MultiSelect, HorizontalStack, getFilenameAndExtension
} from 'substance'

export default class AttachFileModal extends Component {
Expand Down Expand Up @@ -91,18 +91,14 @@ export default class AttachFileModal extends Component {
if (option.id === '#create') {
this.send('requestFileSelect', { multiple: false }).then(files => {
if (files.length > 0) {
const { archive, api } = this.context
const file = files[0]
return this.send('requestModal', () => {
return $$(AssetModal, { file })
}).then(modal => {
if (!modal) return
const api = this.context.api
const { filename } = modal.state.data
const fileNode = api.addFile(filename, file)
const selectedFiles = this.state.selectedFiles
selectedFiles.push(fileNode)
this.extendState({ selectedFiles })
})
const filename = archive.getUniqueFileName(file.name)
const [basename] = getFilenameAndExtension(filename)
const fileNode = api.addFile(filename, file, { select: false, title: basename })
const selectedFiles = this.state.selectedFiles
selectedFiles.push(fileNode)
this.extendState({ selectedFiles })
}
})
}
Expand Down
11 changes: 7 additions & 4 deletions src/model/SmartFigureApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export default class SmartFigureApi extends BasicEditorApi {
this.editorSession.updateNodeStates([[panel.id, {}]])
}

addFile (fileName, file) {
return this.insertFileAfter(fileName, file)
addFile (fileName, file, options) {
return this.insertFileAfter(fileName, file, null, options)
}

insertFileAfter (fileName, file, currentFileId) {
insertFileAfter (fileName, file, currentFileId, options = {}) {
const doc = this.getDocument()
const root = doc.root
let insertPos = root.files.length
Expand All @@ -92,11 +92,14 @@ export default class SmartFigureApi extends BasicEditorApi {
const newFileNode = documentHelpers.createNodeFromJson(tx, {
type: 'file',
src: assetId,
title: options.title || '',
legend: [{ type: 'paragraph' }]
})
newNodeId = newFileNode.id
documentHelpers.insertAt(tx, [root.id, 'files'], insertPos, newFileNode.id)
this._selectItem(tx, newFileNode)
if (options.select !== false) {
this._selectItem(tx, newFileNode)
}
})
return doc.get(newNodeId)
}
Expand Down

0 comments on commit 4bc7fa6

Please sign in to comment.