mirror of
https://github.com/maputnik/editor.git
synced 2026-08-26 06:57:26 +00:00
Migrate all the non react components code to typescript (#847)
This completes the migration to typescript of all the non react components code. The only changes introduced besides types are the type checks using `"something" in object` which narrows down types in typescript.
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import npmurl from 'url'
|
||||
|
||||
function loadJSON(url: string, defaultValue: any, cb: (...args: any[]) => void) {
|
||||
fetch(url, {
|
||||
mode: 'cors',
|
||||
credentials: "same-origin"
|
||||
})
|
||||
.then(function(response) {
|
||||
return response.json();
|
||||
})
|
||||
.then(function(body) {
|
||||
cb(body)
|
||||
})
|
||||
.catch(function() {
|
||||
console.warn('Can not metadata for ' + url)
|
||||
cb(defaultValue)
|
||||
})
|
||||
}
|
||||
|
||||
export function downloadGlyphsMetadata(urlTemplate: string, cb: (...args: any[]) => void) {
|
||||
if(!urlTemplate) return cb([])
|
||||
|
||||
// Special handling because Tileserver GL serves the fontstacks metadata differently
|
||||
// https://github.com/klokantech/tileserver-gl/pull/104#issuecomment-274444087
|
||||
let urlObj = npmurl.parse(urlTemplate);
|
||||
const normPathPart = '/%7Bfontstack%7D/%7Brange%7D.pbf';
|
||||
if(urlObj.pathname === normPathPart) {
|
||||
urlObj.pathname = '/fontstacks.json';
|
||||
} else {
|
||||
urlObj.pathname = urlObj.pathname!.replace(normPathPart, '.json');
|
||||
}
|
||||
let url = npmurl.format(urlObj);
|
||||
|
||||
loadJSON(url, [], cb)
|
||||
}
|
||||
|
||||
export function downloadSpriteMetadata(baseUrl: string, cb: (...args: any[]) => void) {
|
||||
if(!baseUrl) return cb([])
|
||||
const url = baseUrl + '.json'
|
||||
loadJSON(url, {}, glyphs => cb(Object.keys(glyphs)))
|
||||
}
|
||||
Reference in New Issue
Block a user