【问题标题】:How to style the attributes of a label when defining a joint.dia.Link?定义joint.dia.Link时如何设置标签属性的样式?
【发布时间】:2018-01-12 17:50:59
【问题描述】:

我通过几个示例查看了dia.Link.prototype.attr,并了解可以通过这种方式直接定义链接属性:

joint.dia.Link.define('flow.Link', {
    router: {
      name: 'normal'
    },
    connector: {
      name: 'normal'
    },
    attrs: {
      '.tool-options': {
        'data-tooltip-class-name': 'small',
        'data-tooltip': 'Inspect me',
        'data-tooltip-position': 'left'
      },
      '.marker-source': {
        fill: 'none',
        stroke: 'none'
      },
      '.connection-wrap': {
        fill: 'none'
      },
      '.connection' : {
        stroke: '#0000ff',
        strokeWidth: 2,
        strokeDasharray: '0',
        fill: 'none'
      },
      '.marker-target': {
        fill: '#0000ff',
        stroke: '#0000ff',
        d: 'M 10 0 L 0 5 L 10 10 z'
      },
    }
});

但是有没有一种方法可以在这里定义默认的dia.Link.prototype.label 属性?例如:

joint.dia.Link.define('flow.Link', {
    labels: {
      '.label': {
        position: 1, // label at the target
        attrs: {
          text: { fill: 'blue', text: 'My default link label' },
          rect: { fill: 'yellow' },
        }
      }
    },
   // other properties ...
});

我尝试了上面代码的几种变体但没有成功,但是由于.labels是一组链接,这样的事情不可能吗?

我尝试的另一种方法是通过link.label(index, properties, opt) 以编程方式设置第一个默认标签的样式,但是一旦我通过检查器向链接添加了一个标签,两个标签属性都会丢失(前者和加了一个)...

【问题讨论】:

    标签: css json properties jointjs


    【解决方案1】:

    目前无法更改默认标签属性(除非dia.LinkView.prototype.updateLabels() 方法被覆盖)。我在 JointJS 存储库中创建了一个issue

    如果您通过ui.Inspector 插件添加标签,您可以修改标签检查器定义,以便每个新标签都具有所需的属性。为此,请使用 defaultValue 字段选项并使检查器字段不可见,如下例所示。

    labels: {
      type: 'list',
      item: {
        type: 'object',
        properties: {
          attrs: {
            text: {
              text: {
                type: 'text',
                defaultValue: 'label',
              },
              // `labels/{n}/attrs/text/fill` fake field
              fill: {
                type: 'text',
                // The value of the property,
                // which is set when a new label is created
                defaultValue: 'blue',
                // Make this field invisible
                // So the user won't be able to change it
                attrs: { '.field': { style: 'display:none' }}
              }
            },
            rect: {
              // `labels/{n}/attrs/rect/fill` fake field
              fill: {
                type: 'text',
                defaultValue: 'yellow',
                attrs: { '.field': { style: 'display:none' }}
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 这种替代方案有一个小挫折,如果链接自动添加带有几个不同属性的标签而不通过检查器,则所有属性都必须通过@987654326硬编码到该链接中@(它们不会从检查器的默认值中检索到)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 2020-12-27
    • 2020-09-25
    • 2021-02-04
    • 1970-01-01
    • 2014-10-17
    相关资源
    最近更新 更多