Adobe Workfront API中的富文本字段

Adobe Workfront中的某些对象允许存储具有富文本格式的文本。 在Workfront API中,使用开源框架Draft.js将RTF存储为JSON。

概述示例

具有富文本格式的自定义字段称为​ 具有富文本格式的字段,可以具有与其关联的以下值:

示例: ​用于检索自定义表单字段​ 包含富文本字段 ​的值的基本GET请求:

<OBJ Code><OBJ ID><OBJ Code><OBJ ID>

示例: ​此请求将返回在​ parameterValue DE:Field with rich text ​中存储的JSON中包含RTF的​ Field ​的值

{
data: {
    ID: "5e85e3f10000b99e8cef10af4e5c6c7b",
    name: "Zoom Integration Test",
    objCode: "PROJ",
    parameterValues: {
        "DE:Field with rich text": "{"blocks":[{"key":"dpfce","text":"This is a regular text","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"a9ic6","text":"Bold text and regular text","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":9,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"1b2a4","text":"Italic text and regular text","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":11,"style":"ITALIC"}],"entityRanges":[],"data":{}},{"key":"4dle1","text":"Underline text and regular text","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":14,"style":"UNDERLINE"}],"entityRanges":[],"data":{}},{"key":"10mip","text":"With all formating and regular text","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":18,"style":"BOLD"},{"offset":0,"length":18,"style":"ITALIC"},{"offset":0,"length":18,"style":"UNDERLINE"}],"entityRanges":[],"data":{}}],"entityMap":{}}",
        "DE:Other Type of Field": "Value",
        }
    }
}

示例: ​这是上图所示响应的格式化版本

{
   "blocks":[
      {
         "key":"dpfce",
         "text":"This is a regular text",
         "type":"unstyled",
         "depth":0,
         "inlineStyleRanges":[

         ],
         "entityRanges":[

         ],
         "data":{

         }
      },
      {
         "key":"a9ic6",
         "text":"Bold text and regular text",
         "type":"unstyled",
         "depth":0,
         "inlineStyleRanges":[
            {
               "offset":0,
               "length":9,
               "style":"BOLD"
            }
         ],
         "entityRanges":[

         ],
         "data":{

         }
      },
      {
         "key":"1b2a4",
         "text":"Italic text and regular text",
         "type":"unstyled",
         "depth":0,
         "inlineStyleRanges":[
            {
               "offset":0,
               "length":11,
               "style":"ITALIC"
            }
         ],
         "entityRanges":[

         ],
         "data":{

         }
      },
      {
         "key":"4dle1",
         "text":"Underline text and regular text",
         "type":"unstyled",
         "depth":0,
         "inlineStyleRanges":[
            {
               "offset":0,
               "length":14,
               "style":"UNDERLINE"
            }
         ],
         "entityRanges":[

         ],
         "data":{

         }
      },
      {
         "key":"10mip",
         "text":"With all formating and regular text",
         "type":"unstyled",
         "depth":0,
         "inlineStyleRanges":[
            {
               "offset":0,
               "length":18,
               "style":"BOLD"
            },
            {
               "offset":0,
               "length":18,
               "style":"ITALIC"
            },
            {
               "offset":0,
               "length":18,
               "style":"UNDERLINE"
            }
         ],
         "entityRanges":[

         ],
         "data":{

         }
      }
   ],
   "entityMap":{

   }
}

个块

存储富文本内容的JSON对象由两个主要部分组成: ​和​ entityMaps

块是一个JSON对象,它表示一行带格式的文本。 由于单个自定义字段可以包含多行文本,因此每行文本都有自己的块,每个块在名为​ ​的父数组中表示为一个元素。

示例: ​在此,自定义字段中的每一行文本映射到数组块中的块元素

由于每个块元素也是JSON对象,因此每个块由元素组成:keytexttypedepthinlineStyleRangesentityRanges ​和​ data。 其中每个元素均具有以下功能:

  • Key ​是该块的唯一标识符。 键用于通过entityMap映射一行文本。 有关entityMap的详细信息,请参阅本文档的entityMap部分。
  • 文本 ​是从自定义字段中存储的文本内容行。
  • 类型 ​描述所表示的文本类型。 例如,存储在块中的文本行可以是列表的一部分。 如果该行文本属于未排序列表,则其类型将定义为:unordered-list-item。
  • 列表当前不受支持,但应该很快就会可用。
  • 深度 ​当折线是已排序或未排序列表的嵌套部分时,此参数定义折线条的深度。
  • inlineStyleRanges ​是一个数组,它描述应用于当前块表示的文本行的格式类型。

示例: ​以下是一个inlineStyleRanges数组,它描述了字符级别的每个样式。 在本例中:从索引0 (偏移量: 0)开始的9个字符(长度: 9)应用了样式​ 粗体

如果将多种类型的格式应用于单行,则样式将映射到 ​ inlineStyleRanges ​数组中的其他元素。

示例: ​下面是存储包含混合格式的文本行时块的外观:粗体文本和斜体

{
         "key":"a9ic6",
         "text":"Bold text and Italics",
         "type":"unstyled",
         "depth":0,
         "inlineStyleRanges":[
            {
               "offset":0,
               "length":21,
               "style":"BOLD"
            },
            {
               "offset":14,
               "length":7,
               "style":"ITALIC"
            }
         ],
         ...
      },
NOTE
20.3版本之后的所有版本都将支持粗体、斜体和下划线格式选项。

entityMap和entityRanges

数据块可能包含实体,例如超链接或其他类型的样式化格式,这些实体已连接到位于自定义文本字段之外的数据源。

示例

从JSON检索纯文本

提交具有富文本格式的自定义字段时,所有文本都存储在数组​ ​中。 但是,全文的每一行都存储在组成父数组​ ​的每个单独块元素中的​ 文本参数 ​中。 因此,为了检索全文,需要提取每一行单独的文本并将其拼合在一起。 这可以通过循环遍历块中的所有元素并将每个文本参数连接在一起并使用行分隔符(\n)来实现。

示例: ​以下是您的JS可能如下所示:

const parameterValue = JSON.parse('{"blocks":[{"key":"dpfce","text":"This is a regular text","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"a9ic6","text":"Bold text and regular text","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":9,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"1b2a4","text":"Italic text and regular text","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":11,"style":"ITALIC"}],"entityRanges":[],"data":{}},{"key":"4dle1","text":"Underline text and regular text","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":14,"style":"UNDERLINE"}],"entityRanges":[],"data":{}},{"key":"10mip","text":"With all formating and regular text","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":18,"style":"BOLD"},{"offset":0,"length":18,"style":"ITALIC"},{"offset":0,"length":18,"style":"UNDERLINE"}],"entityRanges":[],"data":{}}],"entityMap":{}}');
let plainText = "";
parameterValue.blocks.forEach((block) => {
        plainText = plainText.concat(block.text, "\n");
})
console.log(plainText)

使用Workfront API保存富文本字段值

要使用Workfront API保存富文本字段的以下值,请执行以下操作:

        世界您好!!!
        这是我的第一个富文本
  1. 构造表示您尝试捕获的富文本字段值的JSON,方法是在数组​ ​中将每一行文本组织为一个块元素

    code language-none
    {
        "blocks":[
                    {
                        "key":"0",
                        "text":"Hello World!!!",
                    },
                    {
                        "key":"1",
                        "text":"This is my first Rich Text",
                    }
                ]
    }
    
    
  2. 使用​ inlineStyleRanges ​参数捕获富文本格式

    code language-none
    {
        "blocks":[
                    {
                        "key":"0",
                        "text":"Hello World!!!",
                        "inlineStyleRanges": [
                                                {
                                                    "offset":6,
                                                    "length":11,
                                                    "style":"BOLD"
                                                }
                                            ]
                    },
                    {
                        "key":"1",
                        "text":"This is my first Rich Text",
                    }
                ]
    }
    
  3. 要捕获第二行,文本“富文本”的格式必须同时使用粗体和斜体。

    code language-none
    {
        "blocks":[
                    {
                        "key":"0",
                        "text":"Hello World!!!",
                        "inlineStyleRanges": [
                                                {
                                                    "offset":6,
                                                    "length":11,
                                                    "style":"BOLD"
                                                }
                                            ]
                    },
                    {
                        "key":"1",
                        "text":"This is my first Rich Text",
                        "inlineStyleRanges": [
                                                {
                                                    "offset":17,
                                                    "length":26,
                                                    "style":"BOLD"
                                                },
                                                {
                                                    "offset":17,
                                                    "length":26,
                                                    "style":"ITALIC"
                                                }
                                            ]
                    }
                ],
        "entityMap":{
    
                    }
    }
    
    
    note note
    NOTE
    尽管在初始版本中不支持entityMap功能,但在请求中传递此JSON时仍需要填写此字段
  4. 使用上面列出的JSON上的​ stringify ​方法发出​ PUT ​请求并发送更新

    <OBJ Code><OBJ ID>
    
recommendation-more-help
5f00cc6b-2202-40d6-bcd0-3ee0c2316b43