Skip to content

Commit

Permalink
Merge pull request #1224 from substance/issue-1220
Browse files Browse the repository at this point in the history
Bring back reference deletion.
  • Loading branch information
Integral committed Mar 8, 2019
2 parents bf0ce63 + 371f2df commit 33a1a46
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 36 deletions.
15 changes: 15 additions & 0 deletions src/article/ArticleAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ export default class ArticleAPI {
})
}

_removeReference (ref) {
this.editorSession.transaction(tx => {
documentHelpers.remove(tx, ['article', 'references'], ref.id)
const referenceManager = this._articleSession.getReferenceManager()
referenceManager._getXrefs().forEach(xref => {
const index = xref.refTargets.indexOf(ref.id)
if (index > -1) {
tx.update([xref.id, 'refTargets'], { type: 'delete', pos: index })
}
})
documentHelpers.deepDeleteNode(tx, ref)
tx.selection = null
})
}

_createModelSelection (modelId) {
return {
type: 'custom',
Expand Down
35 changes: 0 additions & 35 deletions src/article/editor/EditReferenceWorkflow.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/article/metadata/MetadataPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import TranslatableEntryEditor from './TranslatableEntryEditor'
import {
AddCustomMetadataFieldCommand, MoveCustomMetadataFieldCommand, RemoveCustomMetadataFieldCommand
} from '../shared/CustomMetadataFieldCommands'
import RemoveReferenceCommand from './RemoveReferenceCommand'
import SwitchViewCommand from '../shared/SwitchViewCommand'

export default {
Expand Down Expand Up @@ -114,6 +115,9 @@ export default {
config.addCommand('remove-figure-panel', RemoveFigurePanelCommand, {
commandGroup: 'figure-panel'
})
config.addCommand('remove-reference', RemoveReferenceCommand, {
commandGroup: 'reference'
})
config.addCommand('replace-figure-panel-image', ReplaceFigurePanelImageCommand, {
commandGroup: 'figure-panel'
})
Expand Down
21 changes: 21 additions & 0 deletions src/article/metadata/RemoveReferenceCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Command, isNil } from 'substance'

export default class RemoveReferenceCommand extends Command {
getCommandState (params, context) {
return { disabled: this.isDisabled(params, context) }
}

isDisabled (params, context) {
const xpath = params.selectionState.xpath
const isCustomSelection = params.selection.isCustomSelection()
if (!isCustomSelection || isNil(xpath) || xpath.length === 0) return true
// Every reference should be inside article references property
return xpath[xpath.length - 1].property !== 'references'
}

execute (params, context) {
const api = context.api
const reference = params.selectionState.node
api._removeReference(reference)
}
}
1 change: 1 addition & 0 deletions src/article/shared/ArticleToolbarPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ export default {
config.addLabel('edit-author', 'Edit Author')
// Reference tools
config.addLabel('edit-reference', 'Edit Reference')
config.addLabel('remove-reference', 'Remove Reference')
// Context tools
config.addLabel('context-tools', 'Edit')
// Mode
Expand Down
36 changes: 35 additions & 1 deletion test/Reference.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { test } from 'substance-test'
import setupTestApp from './shared/setupTestApp'
import { JATS_BIBR_TYPES_TO_INTERNAL, INTERNAL_BIBR_TYPES } from '../index'
import { openMetadataEditor, setSelection, insertText, openMenuAndFindTool } from './shared/integrationTestHelpers'
import {
openMetadataEditor, openManuscriptEditor, setSelection,
insertText, openContextMenuAndFindTool, openMenuAndFindTool
} from './shared/integrationTestHelpers'
import { doesNotThrowInNodejs } from './shared/testHelpers'
import CSLJSON from './fixture/csl-json/csl-json-example'

const emptyLabel = '???'
const removeReferenceToolSelector = '.sm-remove-reference'

// addding reference is done in a workflow, where the user can choose to import, or select a specific type
// TODO: we should also test the other ways to create reference (actually we should cover all cases)
// For now, I have added only the following tests for adding manually
Expand Down Expand Up @@ -86,6 +92,24 @@ test(`Reference: adding and editing authors`, t => {
t.end()
})

test(`Reference: removing`, t => {
let { app } = setupTestApp(t, { archiveId: 'kitchen-sink' })
let metadataEditor = openMetadataEditor(app)
let card = metadataEditor.find('.sc-card.sm-webpage-ref')
card.el.click()

t.comment('removing reference')
t.ok(_canRemoveReference(metadataEditor), 'remove tool should not be disabled')
t.ok(_removeReference(metadataEditor), 'remove should not throw')

t.comment('check what happened with xrefs')
let manuscriptEditor = openManuscriptEditor(app)
let xref = manuscriptEditor.find('.sc-xref')
t.equal(xref.text(), emptyLabel, 'xref label should not contain reference')

t.end()
})

test(`Reference: upload CSL-JSON set`, t => {
let { app } = setupTestApp(t, { archiveId: 'blank' })
let metadataEditor = openMetadataEditor(app)
Expand Down Expand Up @@ -137,3 +161,13 @@ function _insertReference (editor, bibrType) {
// ... this opens a modal where we click on the button for creating the particular bibr type
editor.find(`.sc-modal-dialog .sc-add-reference .se-type.sm-${bibrType}`).click()
}

function _canRemoveReference (editor) {
let tool = openMenuAndFindTool(editor, 'context-tools', removeReferenceToolSelector)
return tool && !tool.attr('disabled')
}

function _removeReference (editor) {
let tool = openContextMenuAndFindTool(editor, removeReferenceToolSelector)
return tool.el.click()
}

0 comments on commit 33a1a46

Please sign in to comment.