Skip to content

Commit

Permalink
Show which panels files and resources are attached to.
Browse files Browse the repository at this point in the history
  • Loading branch information
obuchtala committed Apr 23, 2020
1 parent bb4e782 commit f6daade
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/components/AttachedToComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Component, $$, domHelpers } from 'substance'
import getLabel from './_getLabel'

export default class AttachedToComponent extends Component {
constructor (...args) {
super(...args)

this._relationshipIndex = this.props.document.getIndex('relationships')
this._lastVersion = this._relationshipIndex.sha
}

didMount () {
this.context.editorState.addObserver(['document'], this._onDocumentChange, this, { stage: 'render' })
}

dispose () {
this.context.editorState.off(this)
}

render () {
const { document, nodeId } = this.props
const relationships = Array.from(this._relationshipIndex.get(nodeId))
const el = $$('div', { class: 'sc-attached-to' })
if (relationships.length > 0) {
const items = relationships.map(targetId => {
// ATTENTION: this assumes that we have ony Figure panels where files are attached to
const target = document.get(targetId)
const targetLabel = `Panel ${getLabel(target)}`
return { id: targetId, label: targetLabel }
})
if (items.length > 1) {
items.sort((a, b) => a.label.localeCompare(b.label))
}
el.append($$('span', { class: 'se-label' }, 'Attached to: '))
for (const item of items) {
el.append(
$$('button', { class: 'se-panel-link' }, item.label).on('click', (e) => {
domHelpers.stopAndPrevent(e)
this.context.api.selectItem(item.id)
}).ref(item.id)
)
}
}
return el
}

_onDocumentChange () {
if (this._relationshipIndex.sha !== this._lastVersion) {
this._lastVersion = this._relationshipIndex.sha
this.rerender()
}
}
}
7 changes: 6 additions & 1 deletion src/components/FileComponent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SelectableNodeComponent, $$, renderProperty, domHelpers } from 'substance'
import { SelectableNodeComponent, $$, renderProperty, domHelpers, HorizontalStack } from 'substance'
import Section from './Section'
import getLabel from './_getLabel'
import AttachedToComponent from './AttachedToComponent'

export default class FileComponent extends SelectableNodeComponent {
didMount () {
Expand Down Expand Up @@ -38,6 +39,10 @@ export default class FileComponent extends SelectableNodeComponent {
)
)

el.append(
$$(AttachedToComponent, { document, nodeId: node.id })
)

el.on('mousedown', this._onMousedown)

return el
Expand Down
5 changes: 5 additions & 0 deletions src/components/ResourceComponent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SelectableNodeComponent, $$, renderProperty, domHelpers } from 'substance'
import Section from './Section'
import getLabel from './_getLabel'
import AttachedToComponent from './AttachedToComponent'

export default class ResourceComponent extends SelectableNodeComponent {
render () {
Expand Down Expand Up @@ -31,6 +32,10 @@ export default class ResourceComponent extends SelectableNodeComponent {
)
)

el.append(
$$(AttachedToComponent, { document, nodeId: node.id })
)

el.on('mousedown', this._onMousedown)

return el
Expand Down
13 changes: 13 additions & 0 deletions styles/_attached-to.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.sc-attached-to {
@include ts-interface;
margin-top: var(--default-spacing);

button {
display: inline-block;
color: var(--link-color);
}

button + button::before {
content: ', '
}
}
1 change: 1 addition & 0 deletions styles/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@import './_attach-resource-modal.scss';
@import './_attached-file.scss';
@import './_attached-resource.scss';
@import './_attached-to.scss';
@import './_author-modal.scss';
@import './_authors-list.scss';
@import './_button.scss';
Expand Down

0 comments on commit f6daade

Please sign in to comment.