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

Keywords custom field #4216

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
462 changes: 231 additions & 231 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"sass-loader": "6.0.6",
"shortid": "2.2.8",
"style-loader": "0.20.2",
"superdesk-ui-framework": "^3.0.2",
"superdesk-ui-framework": "^3.0.5",
"ts-loader": "3.5.0",
"tslint": "5.11.0",
"typescript": "4.5.2",
Expand Down
11 changes: 10 additions & 1 deletion scripts/apps/authoring-react/authoring-integration-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {IArticleActionInteractive} from 'core/interactive-article-actions-panel/
import {ARTICLE_RELATED_RESOURCE_NAMES} from 'core/constants';
import {showModal} from '@superdesk/common';
import {ExportModal} from './toolbar/export-modal';
import {TemplateModal} from './toolbar/template-modal';
import {TranslateModal} from './toolbar/translate-modal';
import {HighlightsModal} from './toolbar/highlights-modal';
import {CompareArticleVersionsModal} from './toolbar/compare-article-versions';
Expand Down Expand Up @@ -332,10 +331,20 @@ export class AuthoringIntegrationWrapper extends React.PureComponent<IPropsWrapp
onClick: () => {
toggleSideWidget(widget.label);
},
id: widget._id,
}));

return (
<Nav.SideBarTabs
activeTab={this.state.sideWidget.name}
onActiveTabChange={(val) => {
this.setState({
sideWidget: {
name: val,
pinned: this.state.sideWidget?.pinned ?? false,
},
});
}}
items={sidebarTabs}
/>
);
Expand Down
2 changes: 2 additions & 0 deletions scripts/apps/authoring-react/field-adapters/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {ContentState, convertToRaw, RawDraftContentState} from 'draft-js';
import {computeEditor3Output} from './utilities/compute-editor3-output';
import {package_items} from './package_items';
import {LINKED_ITEMS_FIELD_TYPE} from '../fields/linked-items';
import {getKeywordsAdapter} from './keywords';
import {dateline} from './dateline';
import {description_text} from './description_text';

Expand All @@ -72,6 +73,7 @@ export function getBaseFieldsAdapter(): IFieldsAdapter<IArticle> {
urgency: urgency,
usageterms: usageterms,
groups: package_items,
keywords: getKeywordsAdapter(),
dateline: dateline,
description_text: description_text,
};
Expand Down
65 changes: 65 additions & 0 deletions scripts/apps/authoring-react/field-adapters/keywords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
IArticle,
IAuthoringFieldV2,
IFieldAdapter,
IDropdownConfigVocabulary,
} from 'superdesk-api';
import {gettext} from 'core/utils';
import {sdApi} from 'api';
import {TAG_INPUT_FIELD_ID} from '../fields/tag-input';

export function getKeywordsAdapter(): IFieldAdapter<IArticle> {
const hasKeywordCV = sdApi.vocabularies.getAll().has('keywords');

if (hasKeywordCV) {
return {
getFieldV2: () => {
const fieldConfig: IDropdownConfigVocabulary = {
source: 'vocabulary',
vocabularyId: 'keywords',
multiple: true,
};

const fieldV2: IAuthoringFieldV2 = {
id: 'keywords',
name: gettext('Keywords'),
fieldType: 'dropdown',
fieldConfig,
};

return fieldV2;
},
retrieveStoredValue: (article) => {
return article.keywords ?? [];
},
storeValue: (val: Array<string>, article) => {
return {
...article,
keywords: val,
};
},
};
} else {
return {
getFieldV2: () => {
const fieldV2: IAuthoringFieldV2 = {
id: 'keywords',
name: gettext('Keywords'),
fieldType: TAG_INPUT_FIELD_ID,
fieldConfig: {},
};

return fieldV2;
},
retrieveStoredValue: (article) => {
return article.keywords;
},
storeValue: (val: Array<string>, article) => {
return {
...article,
keywords: val,
};
},
};
}
}
2 changes: 2 additions & 0 deletions scripts/apps/authoring-react/fields/register-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {getAttachmentsField} from './attachments';
import {getTimeField} from './time';
import {geDurationField} from './duration';
import {getArticlesInPackageField} from './package-items';
import {getTagInputField} from './tag-input';
import {getDatelineField} from './dateline';

export function registerAuthoringReactFields() {
Expand Down Expand Up @@ -50,6 +51,7 @@ export function registerAuthoringReactFields() {
getLinkedItemsField(),
getAttachmentsField(),
getArticlesInPackageField(),
getTagInputField(),
getDatelineField(),
],
},
Expand Down
20 changes: 20 additions & 0 deletions scripts/apps/authoring-react/fields/tag-input/difference.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import {IDifferenceComponentProps, ITagInputFieldConfig, ITagInputValueOperational} from 'superdesk-api';
import {DifferenceGeneric} from '../difference-generic';

type IProps = IDifferenceComponentProps<ITagInputValueOperational, ITagInputFieldConfig>;

export class Difference extends React.PureComponent<IProps> {
render() {
const {value1, value2} = this.props;

return (
<DifferenceGeneric
items1={value1 == null ? [] : value1}
items2={value2 == null ? [] : value2}
getId={(item) => JSON.stringify(item)}
template={({item}) => <span>{item}</span>}
/>
);
}
}
27 changes: 27 additions & 0 deletions scripts/apps/authoring-react/fields/tag-input/editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {gettext} from 'core/utils';
import React from 'react';
import {
IEditorComponentProps,
ITagInputFieldConfig,
ITagInputUserPreferences,
ITagInputValueOperational,
} from 'superdesk-api';
import {TagInput} from 'superdesk-ui-framework/react';

type IProps = IEditorComponentProps<ITagInputValueOperational, ITagInputFieldConfig, ITagInputUserPreferences>;

export class Editor extends React.PureComponent<IProps> {
render() {
const Container = this.props.container;

return (
<Container>
<TagInput
placeholder={gettext('Input tags here')}
onChange={this.props.onChange}
value={this.props.value}
/>
</Container>
);
}
}
35 changes: 35 additions & 0 deletions scripts/apps/authoring-react/fields/tag-input/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
ICustomFieldType,
ITagInputValueOperational,
ITagInputValueStorage,
ITagInputUserPreferences,
ITagInputFieldConfig,
} from 'superdesk-api';
import {gettext} from 'core/utils';
import {Editor} from './editor';
import {Preview} from './preview';
import {Difference} from './difference';

export const TAG_INPUT_FIELD_ID = 'tag-input';

type TagInputFieldType = ICustomFieldType<
ITagInputValueOperational,
ITagInputValueStorage,
ITagInputFieldConfig,
ITagInputUserPreferences
>;

export function getTagInputField(): TagInputFieldType {
const field: TagInputFieldType = {
id: TAG_INPUT_FIELD_ID,
label: gettext('Tag-input (authoring-react)'),
editorComponent: Editor,
previewComponent: Preview,
differenceComponent: Difference,
hasValue: (valueOperational) => (valueOperational?.length ?? 0) > 0,
getEmptyValue: () => null,
configComponent: () => null,
};

return field;
}
22 changes: 22 additions & 0 deletions scripts/apps/authoring-react/fields/tag-input/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import {ITagInputFieldConfig, ITagInputValueOperational, IPreviewComponentProps} from 'superdesk-api';
import {TagInput} from 'superdesk-ui-framework/react';

type IProps = IPreviewComponentProps<ITagInputValueOperational, ITagInputFieldConfig>;

export class Preview extends React.PureComponent<IProps> {
render() {
if (this.props.value == null) {
return null;
}

return (
<TagInput
disabled
value={this.props.value}
onChange={() => null}
placeholder=""
/>
);
}
}
12 changes: 8 additions & 4 deletions scripts/core/superdesk-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ declare module 'superdesk-api' {
}

export interface IFieldAdapter<T> {
getFieldV2: (
fieldEditor,
fieldSchema,
) => IAuthoringFieldV2;
getFieldV2: (fieldEditor, fieldSchema) => IAuthoringFieldV2;

/**
* Allows to customize where values are stored.
Expand Down Expand Up @@ -277,6 +274,13 @@ declare module 'superdesk-api' {
};
export type ITimeUserPreferences = never;

// AUTHORING-REACT FIELD TYPES - tag input

export type ITagInputValueOperational = Array<string>;
export type ITagInputValueStorage = ITagInputValueOperational;
export interface ITagInputFieldConfig extends ICommonFieldConfig {};
export type ITagInputUserPreferences = never;

// AUTHORING-REACT FIELD TYPES - duration

export type IDurationValueOperational = number; // number of seconds
Expand Down