Skip to content

Commit

Permalink
Use a special name if you only have one article in the document (#486)
Browse files Browse the repository at this point in the history
* Use a special name if you only have one article in the document

* changeset

* added shortened article behaviour

* update changeset

---------

Co-authored-by: Arne Bertrand <[email protected]>
  • Loading branch information
lagartoverde and abeforgit authored Oct 17, 2024
1 parent 7452911 commit 630ee77
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/six-ants-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lblod/ember-rdfa-editor-lblod-plugins': minor
---

Use a special name if you only have one article in the document
The title from the second article is shortened to Art.
21 changes: 18 additions & 3 deletions addon/components/structure-plugin/_private/structure.gts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ export default class Structure extends Component<Sig> {
get structureName() {
const docLang = this.controller.mainEditorState.doc.attrs.lang;
if (this.displayStructureName) {
return getNameForStructureType(this.structureType, this.intl, docLang);
return getNameForStructureType(
this.structureType,
this.number,
this.intl,
docLang,
);
} else {
return '';
}
Expand All @@ -122,6 +127,17 @@ export default class Structure extends Component<Sig> {
return this.node.attrs.hasTitle;
}

get title() {
if (this.node.attrs.isOnlyArticle) {
const docLang = this.controller.mainEditorState.doc.attrs.lang;
return this.intl.t('structure-plugin.only-article-title', {
locale: docLang,
});
} else {
return `${this.structureName} ${this.number}.`;
}
}

@action
onAttrsUpdate() {
if (this.titleAttr !== this.innerEditor?.htmlContent) {
Expand Down Expand Up @@ -161,8 +177,7 @@ export default class Structure extends Component<Sig> {
<div class='say-structure__header' contenteditable='false'>

{{#let (element this.headerTag) as |Tag|}}
<Tag>{{this.structureName}}
{{this.number}}.
<Tag>{{this.title}}

{{#if this.hasTitle}}
<span
Expand Down
31 changes: 27 additions & 4 deletions addon/plugins/structure-plugin/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ const rdfaAware = true;

export function getNameForStructureType(
structureType: StructureType,
number: number,
intl?: IntlService,
locale?: string,
) {
if (intl?.exists(`structure-plugin.types.${structureType}`, locale)) {
return intl?.t(`structure-plugin.types.${structureType}`, { locale });
if (structureType === 'article' && number !== 1) {
return intl?.t(`structure-plugin.shortened-article`, { locale });
} else {
return intl?.t(`structure-plugin.types.${structureType}`, { locale });
}
} else {
return structureType;
}
Expand Down Expand Up @@ -71,6 +76,9 @@ export const emberNodeConfig: () => EmberNodeConfig = () => {
number: {
default: 1,
},
isOnlyArticle: {
default: false,
},
structureType: {},
displayStructureName: {
default: false,
Expand All @@ -86,21 +94,29 @@ export const emberNodeConfig: () => EmberNodeConfig = () => {
const tag = node.attrs.headerTag;
const structureType = node.attrs.structureType as StructureType;
const number = node.attrs.number as number;
const isOnlyArticle = node.attrs.isOnlyArticle as boolean;
const hasTitle = node.attrs.hasTitle as boolean;
const titleHTML = hasTitle
? parser.parseFromString(node.attrs.title, 'text/html').body
.firstElementChild
: null;
const displayStructureName = node.attrs.displayStructureName as boolean;
const intlService = getIntlService(state);
const structureName = displayStructureName
? getNameForStructureType(
structureType,
getIntlService(state),
number,
intlService,
state.doc.attrs.lang,
)
: null;
let headerSpec;

const onlyArticleTitle = intlService
? intlService.t('structure-plugin.only-article-title', {
locale: state.doc.attrs.lang,
})
: structureName;
if (titleHTML) {
headerSpec = [
tag,
Expand Down Expand Up @@ -133,13 +149,14 @@ export const emberNodeConfig: () => EmberNodeConfig = () => {
[
'span',
{ 'data-say-structure-header-name': true },
`${structureName} `,
`${isOnlyArticle ? onlyArticleTitle : structureName} `,
],
]
: []),
[
'span',
{
style: isOnlyArticle ? 'display: none;' : '',
'data-say-structure-header-number': true,
property: ELI('number').full,
},
Expand All @@ -158,6 +175,7 @@ export const emberNodeConfig: () => EmberNodeConfig = () => {
'data-say-display-structure-name': displayStructureName,
'data-say-header-tag': tag,
'data-say-number': number,
'data-say-is-only-article': isOnlyArticle,
},
content: [
'div',
Expand All @@ -181,6 +199,9 @@ export const emberNodeConfig: () => EmberNodeConfig = () => {
) {
const hasTitle =
node.dataset.sayHasTitle && node.dataset.sayHasTitle !== 'false';
const isOnlyArticle =
node.dataset.sayIsOnlyArticle &&
node.dataset.sayIsOnlyArticle !== 'false';
// strict selector here to avoid false positives when structures are nested
// :scope refers to the element on which we call querySelector
const titleElement = node.querySelector(
Expand All @@ -201,7 +222,8 @@ export const emberNodeConfig: () => EmberNodeConfig = () => {
structureType: node.dataset.sayStructureType,
displayStructureName: node.dataset.sayDisplayStructureName,
headerTag: node.dataset.sayHeaderTag,
number: node.dataset.sayNumber,
number: Number(node.dataset.sayNumber),
isOnlyArticle,
title,
};
}
Expand Down Expand Up @@ -252,6 +274,7 @@ export const emberNodeConfig: () => EmberNodeConfig = () => {
structureType: 'article',
displayStructureName: true,
hasTitle: false,
isOnlyArticle: false,
number,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@ export function recalculateNumbers(
const tr = state.tr;
const doc = tr.doc;
let counter = 0;
let lastNodePos;
doc.descendants((node, pos) => {
if (
node.type.name === 'structure' &&
hasOutgoingNamedNodeTriple(node.attrs, RDF('type'), BESLUIT('Artikel'))
) {
counter += 1;
lastNodePos = pos;
if (node.attrs.isOnlyArticle === true) {
tr.setNodeAttribute(pos, 'isOnlyArticle', false);
}
if (counter !== Number(node.attrs.number)) {
tr.setNodeAttribute(pos, 'number', counter);
}
}
return true;
});
if (lastNodePos !== undefined && counter === 1) {
tr.setNodeAttribute(lastNodePos, 'isOnlyArticle', true);
}
return { transaction: tr, result: true, initialState: state };
}
2 changes: 2 additions & 0 deletions translations/en-US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ common:
structure-plugin:
move-up: 'Move {structureName} up'
move-down: 'Move {structureName} down'
only-article-title: Only article
shortened-article: Art.
types:
article: Article
title: Title
Expand Down
2 changes: 2 additions & 0 deletions translations/nl-BE.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ common:
structure-plugin:
move-up: '{structureName} naar boven verplaatsen'
move-down: '{structureName} naar beneden verplaatsen'
only-article-title: Enig artikel
shortened-article: Art.
types:
article: Artikel
title: Titel
Expand Down

0 comments on commit 630ee77

Please sign in to comment.