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

Revert "feat: support SVG symbols (DHIS2-14440)" #519

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions src/layers/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ class Layer extends Evented {
const beforeId = map.getBeforeLayerId()

if (images) {
try {
await addImages(mapgl, images)
} catch (error) {
this.onError(error)
}
await addImages(mapgl, images)
}

Object.keys(source).forEach(id => {
Expand Down Expand Up @@ -351,17 +347,6 @@ class Layer extends Evented {
this._map.hideLabel()
}
}

// Pass layer error to calling app if handler exists
onError(error) {
const { onError } = this.options

if (onError) {
onError(error)
} else {
console.error(error)
}
}
}

export default Layer
83 changes: 20 additions & 63 deletions src/utils/images.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,24 @@
// Only one SVG icon size is currently suppored
const svgWidth = 16
const svgHeight = 16

const credentials = 'include'

// Add image to map sprite if not exist
const addImage = (map, name, img) => {
if (!map.hasImage(name)) {
map.addImage(name, img)
}
}

// Creates image from SVG data URI
const dataUri2image = dataUri =>
new Promise((resolve, reject) => {
const img = new Image(svgWidth, svgHeight)
img.onload = () => resolve(img)
img.onerror = reject
img.src = dataUri
})

// Fetch SVG and convert to Base64 data URI
const fetchSvg = url =>
fetch(url, { credentials })
.then(response => response.text())
.then(
svg => `data:image/svg+xml;charset=utf-8;base64,${window.btoa(svg)}`
)
.then(dataUri2image)

// Load and add image to map
const loadImage = map => url =>
new Promise(resolve => {
if (url.endsWith('.svg')) {
fetchSvg(url)
.then(img => {
addImage(map, url, img)
resolve(img)
})
.catch(() => {
resolve()
})
} else {
map.loadImage(url, (error, img) => {
if (img) {
addImage(map, url, img)
}
resolve(img)
})
}
})

// Load and add images to map sprite
export const addImages = async (map, images) => {
const result = await Promise.all(images.map(loadImage(map)))
const errorIndex = result.indexOf(undefined)

// Throws error for the first image not found
if (errorIndex !== -1) {
throw `Symbol not found: ${images[errorIndex]}`
}
}
export const addImages = async (map, images) =>
Promise.all(
images.map(
url =>
new Promise((resolve, reject) => {
map.loadImage(url, (error, img) => {
if (error) {
reject(error)
}

if (!map.hasImage(url)) {
map.addImage(url, img)
}

resolve(img)
})
})
)
)

// Include cookies for cross-origin image requests
export const transformRequest = (url, resourceType) =>
resourceType === 'Image' ? { url, credentials } : null
resourceType === 'Image' ? { url, credentials: 'include' } : null