Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update article structure plugin to support rdfa editing #358

Merged
merged 16 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 49 additions & 15 deletions addon/plugins/article-structure-plugin/commands/insert-structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import {
NodeSelection,
TextSelection,
} from '@lblod/ember-rdfa-editor';
import { getNodeByRdfaId } from '@lblod/ember-rdfa-editor/plugins/rdfa-info';
import { addProperty } from '@lblod/ember-rdfa-editor/commands';
import recalculateStructureNumbers from './recalculate-structure-numbers';
import { StructureSpec } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/article-structure-plugin';
import wrapStructureContent from './wrap-structure-content';
import IntlService from 'ember-intl/services/intl';
import { findInsertionRange } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/_private/find-insertion-range';
import { SAY } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
import { Backlink } from '@lblod/ember-rdfa-editor/core/rdfa-processor';

const insertStructure = (
structureSpec: StructureSpec,
Expand All @@ -31,28 +35,58 @@ const insertStructure = (
return false;
}
if (dispatch) {
const { node: newStructureNode, selectionConfig } =
structureSpec.constructor({ schema, intl, content, state });
const transaction = state.tr;
const backlinks = insertionRange.containerNode.attrs?.backlinks as
| Backlink[]
| undefined;
const resource = backlinks?.find((backlink) =>
SAY('body').matches(backlink.predicate),
)?.subject;
const {
node: newStructureNode,
selectionConfig,
newResource,
} = structureSpec.constructor({ schema, intl, content, state });
let transaction = state.tr;

transaction.replaceWith(
insertionRange.from,
insertionRange.to,
newStructureNode,
);
const newSelection =
selectionConfig.type === 'node'
? NodeSelection.create(
transaction.doc,
insertionRange.from + selectionConfig.relativePos,
)
: TextSelection.create(
transaction.doc,
insertionRange.from + selectionConfig.relativePos,
);
transaction.setSelection(newSelection);
const target = getNodeByRdfaId(
state.apply(transaction),
selectionConfig.rdfaId,
);
if (target) {
const newSelection =
selectionConfig.type === 'node'
? NodeSelection.create(transaction.doc, target.pos)
: TextSelection.create(transaction.doc, target.pos + 1);
transaction.setSelection(newSelection);
}

transaction.scrollIntoView();
recalculateStructureNumbers(transaction, structureSpec);
recalculateStructureNumbers(transaction, schema, structureSpec);

if (resource) {
const newState = state.apply(transaction);
addProperty({
resource,
property: {
type: 'external',
predicate: (structureSpec.relationshipPredicate ?? SAY('hasPart'))
.prefixed,
object: {
type: 'resource',
resource: newResource,
},
},
transaction,
})(newState, (newTransaction) => {
transaction = newTransaction;
});
}

dispatch(transaction);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import IntlService from 'ember-intl/services/intl';
import { findParentNodeOfType } from '@curvenote/prosemirror-utils';
import { unwrap } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/option';
import { getTranslationFunction } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/translation';

const moveSelectedStructure = (
options: ArticleStructurePluginOptions,
direction: 'up' | 'down',
Expand Down Expand Up @@ -77,7 +78,7 @@ const moveSelectedStructure = (
: TextSelection.create(transaction.doc, newSelectionPos);
transaction.setSelection(newSelection);
transaction.scrollIntoView();
recalculateStructureNumbers(transaction, currentStructureSpec);
recalculateStructureNumbers(transaction, schema, currentStructureSpec);
dispatch(transaction);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import { PNode, Transaction } from '@lblod/ember-rdfa-editor';
import { PNode, Schema, Transaction } from '@lblod/ember-rdfa-editor';
import { Property } from '@lblod/ember-rdfa-editor/core/rdfa-processor';
import { findNodeByRdfaId } from '@lblod/ember-rdfa-editor/utils/rdfa-utils';
import type { Resource } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/namespace';
import { ELI } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
import { StructureSpec } from '..';

function updateNumber(
transaction: Transaction,
schema: Schema,
node: PNode,
number: string,
numberPredicate?: Resource,
) {
const properties = node.attrs?.properties as Property[] | undefined;
const numberProp =
properties &&
properties.find((prop) =>
(numberPredicate ?? ELI('number')).matches(prop.predicate),
);
const numberRdfaId =
numberProp?.type === 'external' &&
numberProp.object.type === 'literal' &&
numberProp.object.rdfaId;
const numberNode =
numberRdfaId && findNodeByRdfaId(transaction.doc, numberRdfaId);
if (numberNode) {
transaction.replaceWith(
numberNode.pos + 1,
numberNode.pos + numberNode.value.nodeSize - 1,
schema.text(number),
);
}
}

export default function recalculateStructureNumbers(
transaction: Transaction,
schema: Schema,
...structureSpecs: StructureSpec[]
) {
const doc = transaction.doc;
Expand All @@ -17,7 +50,17 @@ export default function recalculateStructureNumbers(
indices[i] = 1;
contexts[i] = parent;
}
spec.updateNumber({ number: indices[i], pos, transaction });
if ('convertNumber' in spec.updateNumber) {
updateNumber(
transaction,
schema,
node,
spec.updateNumber.convertNumber(indices[i]),
spec.updateNumber.numberPredicate,
);
} else {
spec.updateNumber({ transaction, pos, number: indices[i] });
}
indices[i] += 1;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const removeStructure = (
const { pos, node } = structure;
const transaction = state.tr;
transaction.replace(pos, pos + node.nodeSize);
recalculateStructureNumbers(transaction, ...options);
recalculateStructureNumbers(transaction, state.schema, ...options);
dispatch(transaction);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const unwrapStructure = (
if (dispatch) {
const transaction = state.tr;
transaction.replaceWith(pos, pos + node.nodeSize, contentToUnwrap);
recalculateStructureNumbers(transaction, ...options);
recalculateStructureNumbers(transaction, state.schema, ...options);
dispatch(transaction);
}
return true;
Expand Down
Loading