Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
fix sorting of citation items in tooltips.
Browse files Browse the repository at this point in the history
Note: this fix affects only the new block editor. Legacy editor is still
affected.

closes #421
  • Loading branch information
dsifford committed Jan 2, 2019
1 parent 9d96a26 commit 012dfe8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/js/gutenberg/blocks/bibliography/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BlockConfig, createBlock } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';

import { Bibliography } from 'utils/processor';
import Processor from 'utils/processor';

import BibliographyEdit from './edit';
import BibliographySave from './save';

export interface Attributes {
items: Bibliography['items'];
items: Processor.BibItem[];
heading: string;
headingLevel: number;
isToggleable: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/js/gutenberg/blocks/static-bibliography/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { BlockConfig, createBlock } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import uuid from 'uuid/v4';

import { Bibliography } from 'utils/processor';
import Processor from 'utils/processor';

import { stripListItem } from '../';
import edit from './edit';
import save from './save';

export interface Attributes {
items: Bibliography['items'];
items: Processor.BibItem[];
orderedList: boolean;
}

Expand Down
10 changes: 5 additions & 5 deletions src/js/stores/data/actions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Block, createBlock, parse } from '@wordpress/blocks';
import { dispatch, select, subscribe } from '@wordpress/data';
import { RebuildProcessorStateData } from 'citeproc';

import { getEditorDOM, removeItems } from 'utils/editor';
import Processor, { Bibliography } from 'utils/processor';
import Processor from 'utils/processor';

import { Style } from './';
import { Actions } from './constants';
Expand Down Expand Up @@ -99,7 +98,7 @@ function* save() {
yield dispatch('core/editor').savePost();
}

function* setBibliography({ items, meta }: Bibliography) {
function* setBibliography({ items, meta }: Processor.Bibliography) {
const blocksList = select<Block[]>('core/editor').getBlocks();
const bibliographyBlock = blocksList.find(
block => block.name === 'abt/bibliography',
Expand All @@ -124,14 +123,15 @@ function* setBibliography({ items, meta }: Bibliography) {
}
}

function* updateEditorCitations(citations: RebuildProcessorStateData[]) {
function* updateEditorCitations(citations: Processor.CitationMeta[]) {
const doc = getEditorDOM();
for (const [id, , html] of citations) {
for (const { html, id, sortedItems } of citations) {
const node = doc.querySelector<HTMLElement>(
`.abt-citation[id="${id}"]`,
);
if (node) {
node.innerHTML = html;
node.dataset.items = sortedItems;
if (node.childElementCount > 0) {
node.dataset.hasChildren = 'true';
}
Expand Down
4 changes: 2 additions & 2 deletions src/js/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { select } from '@wordpress/data';
import _ from 'lodash';
import uuid from 'uuid/v4';

import { Bibliography } from 'utils/processor';
import Processor from 'utils/processor';

const OBJECT_REPLACEMENT_CHARACTER = '\ufffc';

Expand All @@ -28,7 +28,7 @@ export function parseDataAttrs({
maxoffset,
linespacing,
secondFieldAlign,
}: { [k in keyof Bibliography['meta']]?: string }) {
}: { [k in keyof Processor.BibMeta]?: string }) {
return {
...(entryspacing
? { 'data-entryspacing': `${entryspacing}` }
Expand Down
21 changes: 18 additions & 3 deletions types/citeproc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ declare module 'citeproc' {
'second-field-align': 'flush' | 'margin' | false;
}

// I have literally no idea what any of this is for...
interface SortedItemMeta {
'first-reference-note-number': number;
id: string;
item: CSL.Data;
'near-note': boolean;
position: number;
section_form_override: string;
sortkeys: string[];
}

export interface Citation {
/** ID of the HTMLSpanElement of the single citation element */
citationID: string;
Expand All @@ -132,19 +143,23 @@ declare module 'citeproc' {
};
}

interface StatefulCitation extends Citation {
sortedItems: Array<[CSL.Data, SortedItemMeta]>;
}

interface CitationRegistry {
/**
* Retrieve citation(s) by a HTMLSpanElement ID
*/
citationById: Record<string, Citation>;
citationById: Record<string, StatefulCitation>;
/**
* Array of `Citation` ordered ascending by use in the document
*/
citationByIndex: Citation[];
citationByIndex: StatefulCitation[];
/**
* Retrieve citation by the unique citation ID
*/
citationsByItemId: Record<string, Citation[]>;
citationsByItemId: Record<string, StatefulCitation[]>;
}

interface Registry {
Expand Down

0 comments on commit 012dfe8

Please sign in to comment.