@acoustic-content/ui-extensions 中文文档教程

发布于 3年前 浏览 16 项目主页 更新于 3年前

@acoustic-content/ui-extensions

此模块代表一个 Javascript 库,可用于为声学内容中的元素和/或内容类型创建自定义用户界面。 由于自定义用户界面在 iFrame 中运行,因此可以使用任何 JS 框架(例如 Vanilla JS、JQuery、AngularJS 等)构建它们。

Usage

该库依赖于 post-robot 来启用跨域消息传递。 您可以使用以下任一方法开始编写您自己的 UI 扩展

Option 1: Add the following script tag to your Custom User Interfaces HTML file:

<script src="https://content-eu.goacoustic.com/auth/acoustic-content-ui-extensions.js"></script>

<script src="https://content-us.goacoustic.com/auth/acoustic-content-ui-extensions.js"></script>

: 正确的域:

<script src="https://<Acoustic Content Domain>/auth/acoustic-content-ui-extensions.js"></script>

Option 2: Install the library as a node module through npm:

npm install --save @acoustic-content/ui-extensions

Examples

自定义用户界面的一些开源示例:

Creating your own Custom User Interface

将库添加到所需项目后,您将能够访问变量 wchUIExt,它允许扩展与声学内容通信用户界面。

Methods

How to set element(s)

为了让扩展设置元素的 JSON,请使用 setElement 和/或 setElements 方法。 可在此处找到可能的元素类型和相应 JSON 的更详尽列表:https://developer.goacoustic.com/acoustic-content/docs/acoustic-content/。

// Set values for different element types

// Text Element
wchUIExt.setElement(
    {
        elementType: "text",
        value: "Some text goes here"
    }
);

// Location Element
wchUIExt.setElement(
    {
        elementType: "location",
        latitude: 0.000000,
        longitude: 0.00000
    }
);

自定义用户界面也可以应用于自定义元素内容类型

注意:在设置应用于自定义元素 的自定义用户界面的值时,请记住同时设置typeRef 字段。 这可以从 getDefinition() 中检索(请参阅下面的示例)。

例如,存储选定位置的地图:

// Set value for a CUSTOM ELEMENT that has a Custom User Interface applied with a location element

wchUIExt.getDefinition().then(definition => {
    wchUIExt.setElement({
        elementType: "group",
        typeRef: definition.typeRef,
        value: {
            "locationElementKey": {
                elementType: "location",
                latitude: 0.000000,
                longitude: 0.00000
            }
        }
    });
});
// Set value for a CONTENT TYPE that has a Custom User Interface applied with a location element

wchUIExt.setElement({
    "locationElementKey": {
        elementType: "location",
        latitude: 0.000000,
        longitude: 0.00000
    }
});

可以扩展此行为以允许自定义用户界面封装多个字段。 您可以通过调用方法 setElementsgetElements 来完成此操作。

例如,将位置坐标和国家/地区分别存储为位置和文本元素的地图:

// Set value for a CUSTOM ELEMENT that has a Custom User Interface applied with location and text elements

wchUIExt.getDefinition().then(definition => {
    wchUIExt.setElements({
        elementType: "group",
        typeRef: definition.typeRef,
        value: {
            "locationElementKey": {
                elementType: "location",
                latitude: 0.000000,
                longitude: 0.00000
            },
            "textElementKey": {
                elementType: "text",
                value: "Australia"
            }
        }
    });
});
// Set value for a CONTENT TYPE that has a Custom User Interface applied with location and text elements

wchUIExt.setElements({
    "locationElementKey": {
        elementType: "location",
        latitude: 0.000000,
        longitude: 0.00000
    },
    "textElementKey": {
        elementType: "text",
        value: "Australia"
    }
});

How to get element(s)

获取已使用 setElement 和/或 setElements 设置的元素的元素 JSON 方法,您可以使用 getElement 和/或 getElements 方法。 这些方法用于预填充字段以保存草稿并在发布内容后显示设置值。

// Get the text value that has been set

wchUIExt.getElement().then(element => {
    if (element.value) {
        // Do something with this value
    }
});
// Get value for a CUSTOM ELEMENT that has a Custom User Interface applied with a location element

wchUIExt.getElement().then(element => {
    if (element.value["locationKey"].longitude && element.value["locationKey"].latitude) {
        // Do something with this value
    }
});
// Get value for a CONTENT TYPE that has a Custom User Interface applied with a location element

wchUIExt.getElement().then(element => {
    if (element["locationKey"].longitude && element["locationKey"].latitude) {
        // Do something with this value
    }
});

如果用户决定启用自定义用户界面封装多个字段,他们将必须在 Content TypeCustom Element 上设置自定义用户界面。 然后,您可以使用 getElements() 检索每个元素的值。

注意:如果您在内容类型或具有多个元素的自定义元素上应用自定义用户界面,那么您有责任实施每个元素的 UI。

// Get value for a CUSTOM ELEMENT that has a Custom User Interface applied with a location element AND text element

wchUIExt.getElements().then(element => {
    if (element.value["locationKey"].longitude && element.value["locationKey"].latitude) {
        // Do something with this value
    }
    if (element.value["textElementKey"].value) {
        // Do something with the text value
    }
});
// Get value for a CONTENT TYPE that has a Custom User Interface applied with a location element AND text element

wchUIExt.getElements().then(element => {
    if (element["locationKey"].longitude && element["locationKey"].latitude) {
        // Do something with this value
    }
    if (element["textElementKey"].value) {
        // Do something with the text value
    }
});

How to get the full content

内容 JSON 提供有关父内容项的信息。 这允许元素知道内容中其他元素的属性和定义以及关于内容本身的元数据。 示例内容 JSON 的摘录如下:

{
    id: "925d1454-167b-431b-a54c-6cbf0354398d",
    rev: "25-2ba981d0661c3129c31cc4993e569e3f",
    name: "Sample Content",
    description: "An example description of the sample content",
    typeId: "b0798e67-3da2-48b4-b044-016495fa3ead",
    type: "Article",
    lastModified: "2016-11-02T06:28:47Z",
    lastModifierId: "63b800fa-51a7-4602-8cbe-ab3b9cee28b9",
    lastModifier: "Thomas Watson",
    created: "2016-11-02T06:28:47Z",
    creatorId: "8c622bbb-5f5b-45d4-89e1-fce1c054138f",
    creator: "Thomas Watson",
    elements: {
        datekey: {
            elementType: "datetime",
            value: "2016-11-07T10:09:00Z"
        },
        textkey: {
            elementType: "text",
            value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
        },
    },
    tags: [
        "news",
        "sample"
    ],
    links: {
        self: {
            href: "/authoring/v1/content/925d1454-167b-431b-a54c-6cbf0354398d"
        },
        linkedDoc: {
            href: "/authoring/v1/content/fd95f9a53edd5c2ae4ebdf2ba464a0f9"
        }
    }
}
wchUIExt.getContent().then(content => {
    // Your code
});

How to get the element definition

元素的定义提供有关元素属性的信息。 示例文本元素定义如下:

{
    elementType: "text",
    label: "text",
    key: "text",
    required: true,
    minLength: 1,
    disabled: false
}
wchUIExt.getDefinition().then(definition => {
    // Your code
});

key 是给定元素的唯一标识符,可用于在获取/设置多个元素时区分元素。 如果内容已被发布,则disabled字段设置为true,如果内容仍未发布,则设置为false 草稿。 您可以使用此字段为已发布 内容显示不同的视图(例如禁用的文本输入字段)。

How to get the content metadata

内容元数据提供有关元素所属内容的属性的信息。 示例内容元数据如下:

{
    id: "3e1a8bf6-ad09-4d38-a0b2-9875924dff44",
    name: "Content Name",
    status: "draft"
}
wchUIExt.getContentMetadata().then(contentMetadata => {
    // Your code
});

idname 标识应用 ui-extension 的元素的内容。 status 是指内容的当前状态(例如draftpublished)。

How to set iFrame height

由于自定义用户界面显示在 iFrame 内的 UI 中,因此设置此 iFrame 的所需高度非常重要。 例如,下拉菜单可以将较短的高度设置为默认值 (100),并在打开下拉菜单时设置较高的高度。 整数值以像素为单位。

// Function to open dropdwon

function open() {
    // Code to trigger opening dropdown
    wchUIExt.requestResizeFrame(400);
}

// Function to close dropdown

function close() {
    // Code to trigger closing dropdwon
    wchUIExt.requestResizeFrame(100);
}

How to control validation of an element

自定义用户界面可以控制确定有效条目的内容。 如果条目无效,将在 UI 中显示警告,并且无法发布内容。

例如,电子邮件自定义用户界面将基于文本元素。 文本元素允许为电子邮件地址存储任何文本,但自定义用户界面不允许在电子邮件地址无效时发布内容 - 例如,它不包含 @ 符号

为了声明是否在自定义用户界面中输入的值有效,您可以使用 setValid 方法。 以下是验证方法的几个用例:

// Declare the element to be valid
wchUIExt.setValid(true);

// Declare the element to be invalid but don't show the invalidation message in the UI
wchUIExt.setValid(false, false);

// Declare the element to be invalid and show a custom invalidation message in the UI
wchUIExt.setValid(false, true, "The input value is an invalid value");

How to get the tenant configuration

租户配置提供详细信息,例如 tenantId 和所有其他相关的 URL 端点。 示例租户配置如下:

{
    tenantId: "3e1a8bf6-ad09-4d38-a0b2-9875924dff44",
    apiUrl: "https://content-YY-X.content-cms.com/api/XXX",
    resourceUrl: "https://content-YY-X.content-cms.com/api/XXX",
    host: "content-YY-X.content-cms.com"
}

其中 YY 是 euus

wchUIExt.getTenantConfig().then(tenantConfig => {
    // Your code
});

How to get the current user information

用户对象提供信息,例如 user-id角色external-id。 示例用户对象如下:

{
    externalId: "user@email.com",
    displayName: "User_Name",
    roles: ["admin"],
    id: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
}
wchUIExt.getUser().then(user => {
    // Your code
});

How to get the UI locale

UI 语言环境提供了当前的声音内容 UI 语言环境。 示例 UI Locale 如下:

en-US
wchUIExt.getCurrentLocale().then(locale => {
    // Your code
});

Events

我们支持向内容表单中的事件添加侦听器的能力。 目前,此支持扩展到内容更新元素验证

用户可以通过 on() 方法订阅事件。 此方法接受您要订阅的事件的名称,以及当事件被触发时要执行的回调方法。 当前支持的事件名称列表是:

  1. contentUpdate
  2. validate

Subscribing to content updates

此事件将在发生内容更新时触发。 然后,用户可以通过调用 getContent() 发出请求以获取更新的内容。

wchUIExt.on("contentUpdate", () => {
    // This callback will get triggered when content gets updated

    // User can then get the updated content
    wchUIExt.getContent().then(content => {
        console.log(content);
    });
});

Subscribing to element validate

当对内容项进行验证检查时,将触发此事件。 然后,用户可以在其自定义用户界面 中根据用户自己的验证检查来设置验证。

wchUIExt.on("validate", () => {
    // This callback will get triggered when validation check is done on the content item

    // User can then set validation
    wchUIExt.setValid(true);
});

返回顶部

@acoustic-content/ui-extensions

This module represents a Javascript library that can be used to create custom user interfaces for Elements and/or Content Types in Acoustic Content. Since Custom User Interfaces run within an iFrame, they can be built using any JS framework (e.g. Vanilla JS, JQuery, AngularJS, etc…).

Usage

This library depends on post-robot to enable cross domain messaging. You can use either of the following methods to get started writing your own UI extension:

Option 1: Add the following script tag to your Custom User Interfaces HTML file:

<script src="https://content-eu.goacoustic.com/auth/acoustic-content-ui-extensions.js"></script>

or

<script src="https://content-us.goacoustic.com/auth/acoustic-content-ui-extensions.js"></script>

NOTE: For Acoustic Content developers - If testing UI extensions on an internal environment, the above URL should be modified to point to the correct domain:

<script src="https://<Acoustic Content Domain>/auth/acoustic-content-ui-extensions.js"></script>

Option 2: Install the library as a node module through npm:

npm install --save @acoustic-content/ui-extensions

Examples

Some open source examples of Custom User Interfaces:

Creating your own Custom User Interface

Once the library has been added to your desired project, you will be able to access the variable wchUIExt which allows the extension to communicate with the Acoustic Content UI.

Methods

How to set element(s)

In order for the extension to set the JSON of an element, use the setElement and/or setElements methods. A more thorough list of the possible element types and corresponding JSON can be found here: https://developer.goacoustic.com/acoustic-content/docs/acoustic-content/ .

// Set values for different element types

// Text Element
wchUIExt.setElement(
    {
        elementType: "text",
        value: "Some text goes here"
    }
);

// Location Element
wchUIExt.setElement(
    {
        elementType: "location",
        latitude: 0.000000,
        longitude: 0.00000
    }
);

Custom User Interfaces can also be applied to a custom element or a content type.

NOTE: When setting the value of a Custom User Interface that is applied on a custom element, please remember to set the typeRef field as well. This can be retrieved from getDefinition() (refer to the example below).

For example, a map that stores a selected location:

// Set value for a CUSTOM ELEMENT that has a Custom User Interface applied with a location element

wchUIExt.getDefinition().then(definition => {
    wchUIExt.setElement({
        elementType: "group",
        typeRef: definition.typeRef,
        value: {
            "locationElementKey": {
                elementType: "location",
                latitude: 0.000000,
                longitude: 0.00000
            }
        }
    });
});
// Set value for a CONTENT TYPE that has a Custom User Interface applied with a location element

wchUIExt.setElement({
    "locationElementKey": {
        elementType: "location",
        latitude: 0.000000,
        longitude: 0.00000
    }
});

This behaviour can be extended to enable multiple fields to be encapsulated by the Custom User Interface. You can do this by calling the methods setElements and getElements.

For example, a map that stores both the location coordinates and country as a location and text element respectively:

// Set value for a CUSTOM ELEMENT that has a Custom User Interface applied with location and text elements

wchUIExt.getDefinition().then(definition => {
    wchUIExt.setElements({
        elementType: "group",
        typeRef: definition.typeRef,
        value: {
            "locationElementKey": {
                elementType: "location",
                latitude: 0.000000,
                longitude: 0.00000
            },
            "textElementKey": {
                elementType: "text",
                value: "Australia"
            }
        }
    });
});
// Set value for a CONTENT TYPE that has a Custom User Interface applied with location and text elements

wchUIExt.setElements({
    "locationElementKey": {
        elementType: "location",
        latitude: 0.000000,
        longitude: 0.00000
    },
    "textElementKey": {
        elementType: "text",
        value: "Australia"
    }
});

How to get element(s)

To get the element JSON of the elements that have been set using the setElement and/or setElements methods, you can use the getElement and/or getElements methods. These methods are used to prepopulate fields for saving drafts and for displaying the set values once a piece of content has been published.

// Get the text value that has been set

wchUIExt.getElement().then(element => {
    if (element.value) {
        // Do something with this value
    }
});
// Get value for a CUSTOM ELEMENT that has a Custom User Interface applied with a location element

wchUIExt.getElement().then(element => {
    if (element.value["locationKey"].longitude && element.value["locationKey"].latitude) {
        // Do something with this value
    }
});
// Get value for a CONTENT TYPE that has a Custom User Interface applied with a location element

wchUIExt.getElement().then(element => {
    if (element["locationKey"].longitude && element["locationKey"].latitude) {
        // Do something with this value
    }
});

If the user decideds to enable multiple fields to be encapsulated by the Custom User Interface, they will either have to set the custom user interface on the Content Type or a Custom Element. You can then retrieve the values for each element using getElements().

NOTE: If you are applying the Custom User Interface on a Content Type or Custom Element with multiple elements, then it is your responsiblity to implement the UI for each of these elements.

// Get value for a CUSTOM ELEMENT that has a Custom User Interface applied with a location element AND text element

wchUIExt.getElements().then(element => {
    if (element.value["locationKey"].longitude && element.value["locationKey"].latitude) {
        // Do something with this value
    }
    if (element.value["textElementKey"].value) {
        // Do something with the text value
    }
});
// Get value for a CONTENT TYPE that has a Custom User Interface applied with a location element AND text element

wchUIExt.getElements().then(element => {
    if (element["locationKey"].longitude && element["locationKey"].latitude) {
        // Do something with this value
    }
    if (element["textElementKey"].value) {
        // Do something with the text value
    }
});

How to get the full content

The content JSON provides information about the parent content item. This allows the element to know the properties and definitions of other elements in the content as well as metadata about the content itself. An extract of a sample content JSON is as follows:

{
    id: "925d1454-167b-431b-a54c-6cbf0354398d",
    rev: "25-2ba981d0661c3129c31cc4993e569e3f",
    name: "Sample Content",
    description: "An example description of the sample content",
    typeId: "b0798e67-3da2-48b4-b044-016495fa3ead",
    type: "Article",
    lastModified: "2016-11-02T06:28:47Z",
    lastModifierId: "63b800fa-51a7-4602-8cbe-ab3b9cee28b9",
    lastModifier: "Thomas Watson",
    created: "2016-11-02T06:28:47Z",
    creatorId: "8c622bbb-5f5b-45d4-89e1-fce1c054138f",
    creator: "Thomas Watson",
    elements: {
        datekey: {
            elementType: "datetime",
            value: "2016-11-07T10:09:00Z"
        },
        textkey: {
            elementType: "text",
            value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
        },
    },
    tags: [
        "news",
        "sample"
    ],
    links: {
        self: {
            href: "/authoring/v1/content/925d1454-167b-431b-a54c-6cbf0354398d"
        },
        linkedDoc: {
            href: "/authoring/v1/content/fd95f9a53edd5c2ae4ebdf2ba464a0f9"
        }
    }
}
wchUIExt.getContent().then(content => {
    // Your code
});

How to get the element definition

The definition of an element provides information about the properties of the element. A sample text element definition is as follows:

{
    elementType: "text",
    label: "text",
    key: "text",
    required: true,
    minLength: 1,
    disabled: false
}
wchUIExt.getDefinition().then(definition => {
    // Your code
});

The key is a unique identifier of the given element and can be used to distinguish elements when getting/setting multiple elements. The disabled field is set to true if the content has been published and set to false if the content is still a draft. You can use this field in order to display a different view for published content (e.g. disabled text input field).

How to get the content metadata

The content metadata provides information about the properties of the content that the element is a part of. A sample content metadata is as follows:

{
    id: "3e1a8bf6-ad09-4d38-a0b2-9875924dff44",
    name: "Content Name",
    status: "draft"
}
wchUIExt.getContentMetadata().then(contentMetadata => {
    // Your code
});

The id and name identify the content that the element which the ui-extension is applied to. The status refers to the current status of the content (e.g. draft, published).

How to set iFrame height

Since the Custom User Interface is displayed in the UI within an iFrame, it is important to set the desired height of this iFrame. E.g. dropdowns can have a shorter height set as default (100) and a taller height set when the dropdown is toggled open. The integer values are in pixels.

// Function to open dropdwon

function open() {
    // Code to trigger opening dropdown
    wchUIExt.requestResizeFrame(400);
}

// Function to close dropdown

function close() {
    // Code to trigger closing dropdwon
    wchUIExt.requestResizeFrame(100);
}

How to control validation of an element

The Custom User Interface can control what determines a valid entry. If the entry is invalid, a warning will be shown in the UI, and the content cannot be published.

For example, an email Custom User Interface would be based on a text element. The text element allows any text to be stored for the email address, but the Custom User Interface does not allow content to be published if the email address is not valid - eg., it does not contain an @ symbol

In order to declare whether the value entered in the Custom User Interface is valid, you can use the setValid method. The following are several use cases for the validate method:

// Declare the element to be valid
wchUIExt.setValid(true);

// Declare the element to be invalid but don't show the invalidation message in the UI
wchUIExt.setValid(false, false);

// Declare the element to be invalid and show a custom invalidation message in the UI
wchUIExt.setValid(false, true, "The input value is an invalid value");

How to get the tenant configuration

The tenant configuration provides details such as the tenantId and all other relevant URL endpoints. A sample tenant config is as follows:

{
    tenantId: "3e1a8bf6-ad09-4d38-a0b2-9875924dff44",
    apiUrl: "https://content-YY-X.content-cms.com/api/XXX",
    resourceUrl: "https://content-YY-X.content-cms.com/api/XXX",
    host: "content-YY-X.content-cms.com"
}

where YY is eu or us

wchUIExt.getTenantConfig().then(tenantConfig => {
    // Your code
});

How to get the current user information

The user object provides information such as the user-id, roles and external-id. A sample user object is as follows:

{
    externalId: "user@email.com",
    displayName: "User_Name",
    roles: ["admin"],
    id: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
}
wchUIExt.getUser().then(user => {
    // Your code
});

How to get the UI locale

The UI locale provides the current UI Locale of Acoustic Content. A sample UI Locale is as follows:

en-US
wchUIExt.getCurrentLocale().then(locale => {
    // Your code
});

Events

We support the ability to add a listener to events within the content form. Currently this support extends to content updates and element validations.

A user can subscribe to an event through the on() method. This method takes in the name of the event that you want to subscribe to and the callback method to execute when the event gets triggered. Currently the supported list of event names are:

  1. contentUpdate
  2. validate

Subscribing to content updates

This event will trigger when a content update has occured. The user can then make a request in order to get the updated content by calling getContent().

wchUIExt.on("contentUpdate", () => {
    // This callback will get triggered when content gets updated

    // User can then get the updated content
    wchUIExt.getContent().then(content => {
        console.log(content);
    });
});

Subscribing to element validate

This event will trigger when a validation check has been made on the content item. The user can then set the validation based on the users own validation checks in their custom user interface.

wchUIExt.on("validate", () => {
    // This callback will get triggered when validation check is done on the content item

    // User can then set validation
    wchUIExt.setValid(true);
});

back to top

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文