shaack / cm-chessboard

A JavaScript chessboard without dependencies. Rendered in SVG, coded in ES6. Views FEN, handles move input, animated, responsive, expandable
MIT License
209 stars 63 forks source link

Absolute URLs to SVGs (pieces/markers/arrows) #120

Closed MrEnroke closed 1 year ago

MrEnroke commented 1 year ago

In previous versions you could reference a full url from the corresponding "svgs". sprite: { url: RESOURCES_URL + 'images/chess/setsnew/' + this.pieces + '/pieces.svg', size: 66, // the sprite size, defaults to 40x40px cache: true // cache the sprite inline, in the HTML }, Now the code is more restrictive and "forces" an intermediate path to exist:

pieces: { type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now file: "standard.svg", // the filename of the sprite inassets/pieces/ tileSize: 40 // the tile size in the sprite },

Could you somehow afford a full url? In our case, the proposed structure is not interesting because we have more images related to the theme.

View commit: 167b4d7b7d233679df626987b642df4a7aab632f

shaack commented 1 year ago

OK, I understand your point. I would like to keep the props.assetsUrl for convenience, but we could change the code in a way, that, if an asset file starts with / or http, the assetsUrl is ignored. So that only relative Urls use the assertsUrl.

    getSpriteUrl() {
        if(Utils.isAbsoluteUrl(this.props.sprite)) {
            return this.props.sprite
        } else {
            return this.chessboard.props.assetsUrl + "extensions/arrows/" + this.props.sprite
        }
    }

Another way would be to define the path of the assets always relative from props.assetsUrl. "standard.svg" would become "pieces/standard.svg". With that way, we could set the assetsUrl to "" and put full paths in the file props.

    getSpriteUrl() {
        return this.chessboard.props.assetsUrl + this.props.sprite
    }
MrEnroke commented 1 year ago

Looking at the proposal, the first option includes a more complete solution, although I still doubt the need to include a fixed path such as /extensions/markers.

I would leave it up to everyone to define their path in props.sprite: 'extensions/markers/markers.svg'

shaack commented 1 year ago

You are right, the best solution is both

    getSpriteUrl() {
        if(Utils.isAbsoluteUrl(this.props.sprite)) {
            return this.props.sprite
        } else {
            return this.chessboard.props.assetsUrl + this.props.sprite
        }
    }

unfortunately not backwards compatible. But I will implement it and increase a minor version number.

shaack commented 1 year ago

Published to https://registry.npmjs.org/ as cm-chessboard@7.7.1