如何在自适应卡片上使用 CSS 样式?

How to use CSS styling on adaptive cards?

我正在尝试使用 CSS 在更细粒度的级别上设置自适应卡片的样式,但是默认 CSS 类 的相似名称使其难以定位单个元素。

我尝试用特定的参数来定位它,但它们最终还是在某种程度上违反了规则。我试过在自适应卡片构建过程中使用唯一 ID 或唯一CSS选择器,但似乎在它到达前端时它们都被剥离了。

以下是我迄今为止尝试过的一些示例。

li div .content .attachment .attachment div .ac-container:not(:first-child) .ac-columnSet:hover{
    background: #e6eaed;
    color: #323232;
}

li div .content .attachment .attachment div .ac-container:not(:first-child) .ac-columnSet:hover .ac-container .ac-textBlock p{
    color: #323232;
}```

首先,我想提一下,此功能已经是 BotFramework-WebChat 存储库中正在处理的功能请求。我不知道这方面的预计到达时间,所以不要计划很快。但是,请留意。

这可以通过一些黑客攻击来实现。简而言之,这些是您的步骤:

  1. 在您的自适应卡片中创建一个 "trigger" 属性 并为其分配一个唯一值。
  2. 创建一个查找触发值的 activityMiddleware。收到后,然后通过附加 id.
  3. 更新将保存传入自适应卡的元素
  4. 将纯 CSS 添加到您的 html,根据步骤 2 中附加的 id 设计卡片样式。
  5. activityMiddleware 对象添加到直线。

希望得到帮助!


示例代码如下:

mainDialog.js - 从我的机器人发送的自适应卡片

  async basicAdaptiveCard ( stepContext ) {
    const card = {
      "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
      "type": "AdaptiveCard",
      "version": "1.0",
      "trigger": "ServiceCardTrigger",
      "body": [
        {
          "type": "TextBlock",
          "text": "Hi!! How can I help you today?",
          "weight": "Bolder",
          "size": "Medium"
        }
      ],
      "actions": [
        {
          "type": "Action.Submit",
          "title": "Placeholder Message",
          "data": "close"
        }
      ]
    }

    const ac_card = CardFactory.adaptiveCard( card );
    await stepContext.context.sendActivity(
      {
        attachments: [ ac_card ]
      }
    );
    return { status: DialogTurnStatus.waiting };
  }

index.html - 仅显示基本位。在此,我通过一个包含 "Service details" 值的按钮进行跟踪。它很简单,但适用于演示。根据需要更新。

<style>
  #ServiceCard {
    background-color: #e6eaed;
    color: #323232;
  }

  #ServiceCard:hover p {
    color: red
  }
</style>

[...]

<script type="text/babel">
  [...]

  const activityMiddleware = () => next => card => {
    const { activity: { from, type, attachments } } = card;
    if (type === 'message' && from.role === 'bot') {
      if(attachments && attachments[0].content.trigger === 'ServiceCardTrigger') {
        let children = document.getElementsByClassName('ac-adaptiveCard');
        for (let i = 0, j = children.length; i <= j; i++) {
          if(i === j - 1) {
            let child = children[i];
            if(child.lastChild.innerHTML.includes('Service details')) {
              child.id = 'ServiceCard';
            }
          }
        };
      }
    } 
    else {
      return next(card)
    }
  }

  [...]

  window.ReactDOM.render(
    <ReactWebChat
      activityMiddleware={ activityMiddleware }
      directLine={ directLine }
      username={'johndoe'}
    />,
    document.getElementById( 'webchat' )
  );

  [...]
</script>