@fabwcie/free-drawing / utils/drawingFile
utils/drawingFile
Variables
drawingFileExtension
constdrawingFileExtension:".freedrawing"='.freedrawing'
Defined in: packages/free-drawing/src/utils/drawingFile.ts:58
Extension of drawing files (JSON)
linkDataLimits
constlinkDataLimits:object
Defined in: packages/free-drawing/src/utils/drawingFile.ts:436
Size of the text of link data at most (bytes, decompressed): a short link or an image may hold a huge text (crafted), which would crash the page. Above the drawings of servers (20 MB), tests lower it
Type Declaration
size
size:
number=50_000_000
maxShapeIdLength
constmaxShapeIdLength:200=200
Defined in: packages/free-drawing/src/utils/drawingFile.ts:14
Longest id of a shape: collaboration servers refuse a change holding a longer one
Functions
decodeLinkData()
decodeLinkData(
data):Promise<string>
Defined in: packages/free-drawing/src/utils/drawingFile.ts:497
Text of link data (see encodeLinkData)
Parameters
data
string
data of a link hash, with its z or j prefix
Returns
Promise<string>
decoded text (JSON of a drawing file)
Throws
error.damagedLink for damaged data, error.drawingTooBig beyond linkDataLimits
encodeLinkData()
encodeLinkData(
text):Promise<string>
Defined in: packages/free-drawing/src/utils/drawingFile.ts:483
Text as link data (base64url), compressed when browser can: z prefix, else j
Parameters
text
string
JSON of a drawing file
Returns
Promise<string>
data to put in a link hash
fixIds()
fixIds(
shapes):void
Defined in: packages/free-drawing/src/utils/drawingFile.ts:24
Ids of the shapes of a drawing file made unique and short enough (maxShapeIdLength): a repeated id is replaced, a long one too, with the links of arrows and texts to it; shapes without id get one when added
Parameters
shapes
States of the shapes of the file (changed)
Returns
void
fromBase64Url()
fromBase64Url(
data):Uint8Array<ArrayBuffer>
Defined in: packages/free-drawing/src/utils/drawingFile.ts:426
Bytes of base64url text (see toBase64Url)
Parameters
data
string
encoded text, without padding
Returns
Uint8Array<ArrayBuffer>
decoded bytes
Throws
InvalidCharacterError when not base64
parseDrawingFile()
parseDrawingFile(
json):DrawingFile
Defined in: packages/free-drawing/src/utils/drawingFile.ts:370
Drawing file from its JSON, shapes other than drawing ones are ignored
Parameters
json
string
content of a .freedrawing file or of a link
Returns
drawing file of version 1, white background when missing or invalid, ids of shapes unique (fixIds)
Throws
not a drawing file
parseShape()
parseShape(
shape,framePart?):ShapeState|null
Defined in: packages/free-drawing/src/utils/drawingFile.ts:320
Drawing shape or group of a file, null for other nodes
Parameters
shape
node state read from the file (not trusted)
framePart?
boolean = false
Child of a frame: may be one of its parts
Returns
ShapeState | null
state with only attrs, className and valid children (groups and frames), null for other classes, without attributes or frames without their parts; names reserved by the drawer dropped
parseShapeState()
parseShapeState(
json):ShapeState|null
Defined in: packages/free-drawing/src/utils/drawingFile.ts:354
Drawing shape received from another user of a collaboration, checked as shapes of files
Parameters
json
unknown
JSON of a shape state (konva toObject), not trusted
Returns
ShapeState | null
state with only drawing shapes (see parseDrawingFile), null for other nodes, without id or invalid JSON
pickBlob()
pickBlob(
accept):Promise<File|null>
Defined in: packages/free-drawing/src/utils/drawingFile.ts:551
File chosen by user, null if canceled
Parameters
accept
string
File types (input accept attribute)
Returns
Promise<File | null>
promise of the file, of null when canceled or no file chosen
pickFile()
pickFile(
accept):Promise<string|null>
Defined in: packages/free-drawing/src/utils/drawingFile.ts:568
Text of a file chosen by user, null if canceled
Parameters
accept
string
File types (input accept attribute)
Returns
Promise<string | null>
promise of the file text, of null when canceled or no file chosen
saveFile()
saveFile(
name,content,type?):Promise<void>
Defined in: packages/free-drawing/src/utils/drawingFile.ts:516
Save content in a file: browser save dialog when available (Chromium), else download
Parameters
name
string
File name with extension
content
string | Blob
file content: text or binary data (exported image)
type?
string = 'application/json'
Mime type of content
Returns
Promise<void>
Throws
AbortError when dialog is canceled
toBase64Url()
toBase64Url(
bytes):string
Defined in: packages/free-drawing/src/utils/drawingFile.ts:409
Base64url text of bytes (URL safe: - and _, no padding)
Parameters
bytes
Uint8Array
data to encode
Returns
string
encoded text