Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 阿里小程序等<svg> 需要encodeUri操作, 提取部分公共代码 #23

Merged
merged 6 commits into from
Sep 14, 2021
Merged

Conversation

jaluik
Copy link
Contributor

@jaluik jaluik commented Sep 7, 2021

阿里、头条等小程序端增加 encodeUri操作, 提取部分公共代码

作者你好:

我使用remax-iconfont-cli进行阿里小程序icon组件生成时,无法正确显示图标, 经查看得知原因是在于生成的 base64的svg图片无法正常显示。

生成的格式为这个样式:(因为<>符号未进行encodeURI操作, 所以无法正确显示)

data:image/svg+xml, <svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg' width='16px' height='16px'><path d='M571.134948 546.559968a59.134953 59.134953 0 1 1-118.269906 0V270.852186a59.134953 59.134953 0 1 1 118.269906 0zM511.999995 802.552765a58.878953 58.878953 0 1 1 41.726967-17.150986A57.086955 57.086955 0 0 1 511.999995 802.553765zM511.999995 0.0104a501.749603 501.749603 0 0 0-199.419842 39.935968A509.685597 509.685597 0 0 0 39.946368 312.580153 501.749603 501.749603 0 0 0 0.0104 511.999995a501.749603 501.749603 0 0 0 39.935968 199.419842A509.685597 509.685597 0 0 0 312.580153 984.054622 501.749603 501.749603 0 0 0 511.999995 1023.98959a501.749603 501.749603 0 0 0 199.419842-39.934968A509.685597 509.685597 0 0 0 984.054622 711.419837 501.749603 501.749603 0 0 0 1023.98959 511.999995a501.749603 501.749603 0 0 0-39.934968-199.419842A509.685597 509.685597 0 0 0 711.419837 39.946368 501.749603 501.749603 0 0 0 511.999995 0.0104z' fill='#FABD52' /></svg>

经查看源码得知 remax-iconfont-cli 依赖于仓库,其中生成阿里小程序的template时,没有进行encodeURI的操作。

这个pr用于修复阿里小程序的问题, 同时抽离了部分公共代码(对于 replaceHexToRgb 这个方法, wechat、头条等平台使用了,其他平台没有使用,因此我这里根据源码做了重构,保留了平台区分)。

希望尽快修复此bug, 目前公司项目依赖于本框架~ 谢谢!

@fwh1990
Copy link
Member

fwh1990 commented Sep 13, 2021

我这边用原生支付宝小程序测试encodeURI之后仍然无效?

@jaluik
Copy link
Contributor Author

jaluik commented Sep 13, 2021

我看下 应该是必须要进行全部encodeURIComponent才行

比如下面在阿里小程序无法显示

data:image/svg+xml, <svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg' width='16px' height='16px'><path d='M571.134948 546.559968a59.134953 59.134953 0 1 1-118.269906 0V270.852186a59.134953 59.134953 0 1 1 118.269906 0zM511.999995 802.552765a58.878953 58.878953 0 1 1 41.726967-17.150986A57.086955 57.086955 0 0 1 511.999995 802.553765zM511.999995 0.0104a501.749603 501.749603 0 0 0-199.419842 39.935968A509.685597 509.685597 0 0 0 39.946368 312.580153 501.749603 501.749603 0 0 0 0.0104 511.999995a501.749603 501.749603 0 0 0 39.935968 199.419842A509.685597 509.685597 0 0 0 312.580153 984.054622 501.749603 501.749603 0 0 0 511.999995 1023.98959a501.749603 501.749603 0 0 0 199.419842-39.934968A509.685597 509.685597 0 0 0 984.054622 711.419837 501.749603 501.749603 0 0 0 1023.98959 511.999995a501.749603 501.749603 0 0 0-39.934968-199.419842A509.685597 509.685597 0 0 0 711.419837 39.946368 501.749603 501.749603 0 0 0 511.999995 0.0104z' fill='#FABD52' /></svg>

全部encode后就可以了

data:image/svg+xml, %3Csvg%20viewBox%3D'0%200%201024%201024'%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20width%3D'16px'%20height%3D'16px'%3E%3Cpath%20d%3D'M571.134948%20546.559968a59.134953%2059.134953%200%201%201-118.269906%200V270.852186a59.134953%2059.134953%200%201%201%20118.269906%200zM511.999995%20802.552765a58.878953%2058.878953%200%201%201%2041.726967-17.150986A57.086955%2057.086955%200%200%201%20511.999995%20802.553765zM511.999995%200.0104a501.749603%20501.749603%200%200%200-199.419842%2039.935968A509.685597%20509.685597%200%200%200%2039.946368%20312.580153%20501.749603%20501.749603%200%200%200%200.0104%20511.999995a501.749603%20501.749603%200%200%200%2039.935968%20199.419842A509.685597%20509.685597%200%200%200%20312.580153%20984.054622%20501.749603%20501.749603%200%200%200%20511.999995%201023.98959a501.749603%20501.749603%200%200%200%20199.419842-39.934968A509.685597%20509.685597%200%200%200%20984.054622%20711.419837%20501.749603%20501.749603%200%200%200%201023.98959%20511.999995a501.749603%20501.749603%200%200%200-39.934968-199.419842A509.685597%20509.685597%200%200%200%20711.419837%2039.946368%20501.749603%20501.749603%200%200%200%20511.999995%200.0104z'%20fill%3D'%23FABD52'%20%2F%3E%3C%2Fsvg%3E

需要对模板语法做一些调整, 我晚些时候改一下再更新这个pr

@jaluik
Copy link
Contributor Author

jaluik commented Sep 13, 2021

新提交了一些改动

下面是本地的效果

image

@jaluik
Copy link
Contributor Author

jaluik commented Sep 13, 2021

已经解决了~ @fwh1990

color = sub.$[attributeName]
}

template += ` ${attributeName}='{{(isStr ? colors : colors[${counter.colorIndex}]) || '${color}'}}'`;
Copy link
Member

@fwh1990 fwh1990 Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了微信小程序是colors,其他地方都是用color,能否再加一个参数区分呢?

Copy link
Member

@fwh1990 fwh1990 Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有一个规律就是开启了hexToRgb的就是colors

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以的,我再提一个

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fwh1990 可以了 pr更新了

下面是测试效果

image

counter.colorIndex += 1;
}else {
color = sub.$[attributeName]
template += ` ${attributeName}='{{color}}'`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个应该是错误的逻辑。

let keyword: string;

if (hextorgb) {
  ...
  ...
  keyword = 'colors';
} else {
 ...
 ...
  keyword = 'color';
}

template += ` ${attributeName}='{{(isStr ? ${keyword} : ${keyword}[${counter.colorIndex}]) || '${color}'}}'`;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我是看到这些模板根本没用到colors呀, 你是要做个通用的倒是可以这么改

  props: {
    // #names#
    name: null,
    // string | string[]
    color: '',
    size: #size#,
  },
  data: {
    quot: '"',
    svgSize: #size#,
    isStr: true,
  },
  didMount() {
    const size = this.props.size;
    const color = this.props.color;

    this.setData({
      isStr: typeof color === 'string',
    });

    if (size !== this.data.svgSize) {
      this.setData({
        svgSize: #rpx-1:size / 750 * my.getSystemInfoSync().windowWidth:##rpx-0:size:#,
      });
    }
  },
  disUpdate(prevProps) {
    const size = this.props.size;
    const color = this.props.color;

    if (color !== prevProps.color) {
      this.setData({
        isStr: typeof color === 'string',
      });
    }

    if (size !== prevProps.size) {
      this.setData({
        svgSize: #rpx-1:size / 750 * my.getSystemInfoSync().windowWidth:##rpx-0:size:#,
      });
    }
  },

@jaluik
Copy link
Contributor Author

jaluik commented Sep 14, 2021

统一了一下逻辑

@fwh1990
Copy link
Member

fwh1990 commented Sep 14, 2021

好的,午休过后合并,然后我再全部平台测试一下。感谢

@fwh1990 fwh1990 merged commit 0869797 into iconfont-cli:master Sep 14, 2021
@jaluik jaluik deleted the fix-svgTagEncode branch September 14, 2021 08:47
@fwh1990
Copy link
Member

fwh1990 commented Sep 14, 2021

sjs 方案是多余的,只需转换hex2rgb即可。已经发布0.6.1,你可以先测试一下这个库是否可行

@fwh1990
Copy link
Member

fwh1990 commented Sep 14, 2021

我自己各个平台测试过了,你再测试一下,没问题之后我就升级到remax-icon和taro-iconfont

@jaluik
Copy link
Contributor Author

jaluik commented Sep 14, 2021

ok 晚上我测一下

@jaluik
Copy link
Contributor Author

jaluik commented Sep 14, 2021

验证阿里平台改成rgb是可行的。

@fwh1990

请教一下,bug出现的原因是在于 不支持 fill='#FABD52' 这种写法么

@fwh1990
Copy link
Member

fwh1990 commented Sep 15, 2021

验证阿里平台改成rgb是可行的。

@fwh1990

请教一下,bug出现的原因是在于 不支持 fill='#FABD52' 这种写法么

对的。除了QQ小程序,其他平台应该都不支持#号颜色。也许是解析的时候被当成url hash了(纯猜想)

@fwh1990
Copy link
Member

fwh1990 commented Sep 15, 2021

remax-iconfont-cli 已经更新2.0.1

@jaluik
Copy link
Contributor Author

jaluik commented Sep 15, 2021

ok 感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants