<commit_before>import {ISuperdesk, IExtension, IRestApiResponse} from 'superdesk-api';
import {getAnnotationInputWithKnowledgeBase} from './AnnotationInputWithKnowledgeBase';
import {getAnnotationsLibraryPage} from './annotations-library-page';
import {getFields} from './GetFields';
import {IKnowledgeBaseItem} from './interfaces';

const RESOURCE = 'concept_items';

function annotationFromLibraryTabSelectedByDefault(
    superdesk: ISuperdesk,
    annotationText: string,
    mode: 'create' | 'edit',
) {
    const {dataApi} = superdesk;
    const {assertNever} = superdesk.helpers;
    const {nameField} = getFields(superdesk);
    const {generateFilterForServer} = superdesk.forms;

    if (mode === 'edit') {
        return Promise.resolve(false);
    } else if (mode === 'create') {
        return dataApi.query<IKnowledgeBaseItem>(
            RESOURCE,
            1,
            {field: 'name', direction: 'ascending'},
            {name: generateFilterForServer(nameField.type, annotationText)},
        ).then((res: IRestApiResponse<IKnowledgeBaseItem>) => res._items.length > 0);
    } else {
        return assertNever(mode);
    }
}

var extension: IExtension = {
    activate: (superdesk: ISuperdesk) => {
        const {gettext} = superdesk.localization;

        return Promise.resolve({
            contributions: {
                editor3: {
                    annotationInputTabs: [
                        {
                            label: gettext('Annotations library'),
                            component: getAnnotationInputWithKnowledgeBase(superdesk),
                            selectedByDefault: (annotationText: string, mode: 'create' | 'edit') =>
                                annotationFromLibraryTabSelectedByDefault(superdesk, annotationText, mode),
                            onAnnotationCreate: (language: string, annotationText: string, definitionHtml: string) => {
                                superdesk.dataApi.create(RESOURCE, {
                                    language: language,
                                    name: annotationText,
                                    definition_html: definitionHtml,
                                    cpnat_type: 'cpnat:abstract',
                                });
                            },
                        },
                    ],
                },
                pages: [
                    {
                        title: gettext('Annotations library'),
                        url: '/annotations-library',
                        component: getAnnotationsLibraryPage(superdesk),
                    },
                ],
            },
        });
    },
};

export default extension;<commit_msg>Don't duplicate annotation from library when reusing it (#3200)<commit_after>import {ISuperdesk, IExtension, IRestApiResponse} from 'superdesk-api';
import {getAnnotationInputWithKnowledgeBase} from './AnnotationInputWithKnowledgeBase';
import {getAnnotationsLibraryPage} from './annotations-library-page';
import {getFields} from './GetFields';
import {IKnowledgeBaseItem} from './interfaces';

const RESOURCE = 'concept_items';

function annotationExistsInKnowledgeBase(superdesk: ISuperdesk, annotationText: string) {
    const {dataApi} = superdesk;
    const {nameField} = getFields(superdesk);
    const {generateFilterForServer} = superdesk.forms;

    return dataApi.query<IKnowledgeBaseItem>(
        RESOURCE,
        1,
        {field: 'name', direction: 'ascending'},
        {name: generateFilterForServer(nameField.type, annotationText)},
    ).then((res: IRestApiResponse<IKnowledgeBaseItem>) => res._items.length > 0);
}

function annotationFromLibraryTabSelectedByDefault(
    superdesk: ISuperdesk,
    annotationText: string,
    mode: 'create' | 'edit',
) {
    const {assertNever} = superdesk.helpers;

    if (mode === 'edit') {
        return Promise.resolve(false);
    } else if (mode === 'create') {
        return annotationExistsInKnowledgeBase(superdesk, annotationText);
    } else {
        return assertNever(mode);
    }
}

function onAnnotationCreate(superdesk: ISuperdesk, language: string, annotationText: string, definitionHtml: string) {
    annotationExistsInKnowledgeBase(superdesk, annotationText)
        .then((exists) => {
            // Don't create a knowledge base item for that annotation if it already exists
            if (!exists) {
                superdesk.dataApi.create(RESOURCE, {
                    language: language,
                    name: annotationText,
                    definition_html: definitionHtml,
                    cpnat_type: 'cpnat:abstract',
                });
            }
        });
}

var extension: IExtension = {
    activate: (superdesk: ISuperdesk) => {
        const {gettext} = superdesk.localization;

        return Promise.resolve({
            contributions: {
                editor3: {
                    annotationInputTabs: [
                        {
                            label: gettext('Annotations library'),
                            component: getAnnotationInputWithKnowledgeBase(superdesk),
                            selectedByDefault: (annotationText: string, mode: 'create' | 'edit') =>
                                annotationFromLibraryTabSelectedByDefault(superdesk, annotationText, mode),
                            onAnnotationCreate: (language: string, annotationText: string, definitionHtml: string) => {
                                return onAnnotationCreate(superdesk, language, annotationText, definitionHtml);
                            },
                        },
                    ],
                },
                pages: [
                    {
                        title: gettext('Annotations library'),
                        url: '/annotations-library',
                        component: getAnnotationsLibraryPage(superdesk),
                    },
                ],
            },
        });
    },
};

export default extension;
{'fraction_non_alphanumeric': 0.09866888519134775, 'fraction_numerical': 0.0016638935108153079, 'mean_word_length': 1.866475917978064, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7055759', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1435, 'n_words': 303}