forked from substance/sourcedata-smartfigure-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request substance#40 from substance/references-i1
References
- Loading branch information
Showing
16 changed files
with
229 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { nodeHelpers } from 'substance' | ||
|
||
const { getLabel } = nodeHelpers | ||
|
||
// FIXME: here are race conditions, because the ReferenceLabelManager is called later then this one | ||
// TODO: instead of having two managers, we should merge | ||
export default class CitationLabelManager { | ||
constructor (editorSession) { | ||
this.editorSession = editorSession | ||
|
||
this.initialize() | ||
} | ||
|
||
initialize () { | ||
const editorSession = this.editorSession | ||
const doc = editorSession.getDocument() | ||
editorSession.getEditorState().addObserver(['document'], this._onChange, this, { | ||
stage: 'update' | ||
}) | ||
this._referencesPath = [doc.root.id, 'references'] | ||
} | ||
|
||
getReferenceLabel (reference) { | ||
return `${reference.getPosition() + 1}` | ||
} | ||
|
||
getCitationLabel (cite) { | ||
const references = cite.resolve('references') | ||
const refLabels = references.map(ref => getLabel(ref)) | ||
refLabels.sort() | ||
return `[${refLabels.join(',')}]` | ||
} | ||
|
||
update () { | ||
const doc = this.editorSession.getDocument() | ||
const references = doc.resolve(this._referencesPath) | ||
const citations = doc.findAll('cite') | ||
// first update references | ||
let stateUpdates = references.map(ref => { | ||
const label = this.getReferenceLabel(ref) | ||
return [ref.id, { label }] | ||
}) | ||
if (stateUpdates.length > 0) { | ||
this.editorSession.updateNodeStates(stateUpdates, { silent: true }) | ||
} | ||
// then update citations | ||
stateUpdates = citations.map(cite => { | ||
const label = this.getCitationLabel(cite) | ||
return [cite.id, { label }] | ||
}) | ||
if (stateUpdates.length > 0) { | ||
this.editorSession.updateNodeStates(stateUpdates, { silent: true }) | ||
} | ||
} | ||
|
||
_onChange (change) { | ||
// Note: if the reference list has changed, recompute all labels | ||
if (change.hasUpdated(this._referencesPath)) { | ||
this.update() | ||
// otherwise update the label for created citations (e.g. pasted, or after undo) | ||
// or when the citation has been updated | ||
} else { | ||
const ops = change.primitiveOps | ||
const stateUpdates = [] | ||
// TODO: with the new op system, we should also try to generalize the API for writing listeners | ||
for (const op of ops) { | ||
if ( | ||
(op.isCreate() && op.val.type === 'cite') || | ||
(op.isSet() && op.path[1] === 'references') | ||
) { | ||
const doc = this.editorSession.getDocument() | ||
const id = op.path[0] | ||
const cite = doc.get(id) | ||
if (cite) { | ||
stateUpdates.push([id, { label: this.getCitationLabel(cite) }]) | ||
} | ||
} | ||
} | ||
if (stateUpdates.length > 0) { | ||
this.editorSession.updateNodeStates(stateUpdates, { silent: true }) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.sc-citation-popover { | ||
width: 500px; | ||
margin: -4px 0; | ||
} | ||
|
||
.sc-citation-popover > .se-reference { | ||
@include ts-small-interface; | ||
font-weight: 500; | ||
padding: var(--half-spacing) var(--default-spacing); | ||
word-break: break-all; | ||
} | ||
|
||
.sc-citation-popover > .se-reference > .se-label { | ||
align-self: baseline; | ||
font-weight: 600; | ||
} | ||
|
||
.sc-citation-popover > .sc-horizontal-stack { | ||
padding: var(--half-spacing) var(--default-spacing); | ||
} | ||
|
||
.sc-citation-popover > .se-footer { | ||
@include ts-small-interface; | ||
} | ||
|
||
.sc-citation-popover > .se-footer > .se-label { | ||
@include ts-quiet; | ||
} | ||
|
||
.sc-citation-popover > .sc-horizontal-stack > .sc-button { | ||
width: 100px; | ||
margin: 0 var(--half-spacing); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.sc-inline-node.sm-cite { | ||
color: var(--link-color); | ||
white-space: nowrap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.sc-inline-node { | ||
cursor: default; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.sc-reference-list > .se-references > * + * { | ||
margin-top: var(--default-spacing); | ||
} |
Oops, something went wrong.