Добавлен метод для конвертации SVG в DataURL

This commit is contained in:
goodmice 2022-06-25 15:47:13 +05:00
parent 9eb6ab4fa6
commit 8db3476c0e
2 changed files with 22 additions and 0 deletions

View File

@ -3,6 +3,8 @@ export * from './arrayOrDefault'
export * from './datetime'
export type { RawDate, TimezoneId, timeInS } from './datetime'
export * from './chart'
export * from './min_max_filter'
export type { KeyType } from './min_max_filter'
@ -21,5 +23,7 @@ export type { StorageNames, DataDashboardNNB } from './storage'
export * from './string'
export * from './svg'
export * from './table_settings'
export type { TableColumnSettings, TableSettings, TableSettingsStore } from './table_settings'

View File

@ -0,0 +1,18 @@
export const svgToDataURL = (svg: SVGSVGElement) => {
const serializer = new XMLSerializer()
let source = serializer.serializeToString(svg)
if(!source.match(/^<svg[^>]+xmlns="http\:\/\/www\.w3\.org\/2000\/svg"/))
source = source.replace(/^<svg/, '<svg xmlns="http://www.w3.org/2000/svg"')
if(!source.match(/^<svg[^>]+"http\:\/\/www\.w3\.org\/1999\/xlink"/))
source = source.replace(/^<svg/, '<svg xmlns:xlink="http://www.w3.org/1999/xlink"')
source =
'data:image/svg+xml;charset=utf-8,' +
'<?xml version="1.0" standalone="no"?>\r\n' +
encodeURIComponent(source)
return source
}