Drafty

Drafty

Drafty

Constructor

new Drafty()

The main object which performs all the formatting actions.
Source:

Methods

(static) append(first, second) → {Drafty}

Append one Drafty document to another.
Source:
Parameters:
Name Type Description
first Drafty Drafty document to append to.
second Drafty | string Drafty document or string being appended.
Returns:
Type:
Drafty
first document with the second appended to it.

(static) appendAudio(content, audioDesc) → {Drafty}

Append audio recodring to Drafty document.
Source:
Parameters:
Name Type Description
content Drafty document to add recording to.
audioDesc AudioDesc object with audio data.
Returns:
Type:
Drafty
updated document.

(static) appendButton(content, title, name, actionType, actionValue, refUrl) → {Drafty}

Append clickable button to Drafty document.
Source:
Parameters:
Name Type Description
content Drafty | string Drafty document to insert button to or a string to be used as button text.
title string the text to be used as button title.
name string the button. Client should return it to the server when the button is clicked.
actionType string the type of the button, one of 'url' or 'pub'.
actionValue string the value to return on click:
refUrl string the URL to go to when the 'url' button is clicked.
Returns:
Type:
Drafty
updated document.

(static) appendImage(content, imageDesc) → {Drafty}

Append image to Drafty document.
Source:
Parameters:
Name Type Description
content Drafty document to add image to.
imageDesc ImageDesc object with image paramenets.
Returns:
Type:
Drafty
updated document.

(static) appendLineBreak(content) → {Drafty}

Append line break to a Drafty document.
Source:
Parameters:
Name Type Description
content Drafty Drafty document to append linebreak to.
Returns:
Type:
Drafty
the same document as content.
Append a link to a Drafty document.
Source:
Parameters:
Name Type Description
content Drafty Drafty document to append link to.
linkData Object Link info in format {txt: 'ankor text', url: 'http://...'}.
Returns:
Type:
Drafty
the same document as content.

(static) attachFile(content, object) → {Drafty}

Attach file to Drafty content. Either as a blob or as a reference.
Source:
Parameters:
Name Type Description
content Drafty document to attach file to.
object AttachmentDesc containing attachment description and data.
Returns:
Type:
Drafty
updated document.

(static) attachJSON(content, data) → {Drafty}

Attach a generic JS object. The object is attached as a json string. Intended for representing a form response.
Source:
Parameters:
Name Type Description
content Drafty Drafty document to attach file to.
data Object data to convert to json string and attach.
Returns:
Type:
Drafty
the same document as content.

(static) attachments(content, callback, context)

Enumerate attachments: style EX and outside of normal rendering flow, i.e. at = -1.
Source:
Parameters:
Name Type Description
content Drafty document to process for attachments.
callback EntityCallback callback to call for each attachment.
context Object value of "this" for callback.

(static) attrValue(style, data) → {Object}

For a given data bundle generate an object with HTML attributes, for instance, given {url: "http://www.example.com/"} return {href: "http://www.example.com/"}
Source:
Parameters:
Name Type Description
style string two-letter style to generate attributes for.
data Object data bundle to convert to attributes
Returns:
Type:
Object
object with HTML attributes.

(static) entities(content, callback, context)

Enumerate entities. Enumeration stops if callback returns 'true'.
Source:
Parameters:
Name Type Description
content Drafty document with entities to enumerate.
callback EntityCallback callback to call for each entity.
context Object value of "this" for callback.

(static) format(content, formatter, context) → {Object}

Convert Drafty document to a representation suitable for display. The context may expose a function getFormatter(style). If it's available it will call it to obtain a formatter for a subtree of styles under the style.
Source:
Parameters:
Name Type Description
content Drafty | Object Drafty document to transform.
formatter Formatter callback which formats individual elements.
context Object context provided to formatter as this.
Returns:
Type:
Object
transformed object

(static) forwardedContent(original)

Transform Drafty doc for forwarding: strip leading @mention and any leading line breaks or whitespace.
Source:
Parameters:
Name Type Description
original Drafty | string Drafty object to shorten.
Returns:
converted Drafty object leaving the original intact.

(static) getContentType() → {string}

Drafty MIME type.
Source:
Returns:
Type:
string
content-Type "text/x-drafty".

(static) getDownloadUrl(entData) → {string}

Given the entity, get URL which can be used for downloading entity data.
Source:
Parameters:
Name Type Description
entData Object entity.data to get the URl from.
Returns:
Type:
string
URL to download entity data or null.

(static) getEntityMimeType(entData) → {string}

Get entity mime type.
Source:
Parameters:
Name Type Description
entData Object entity.data to get the type for.
Returns:
Type:
string
mime type of entity.

(static) getEntitySize(entData) → {number}

Get approximate size of the entity.
Source:
Parameters:
Name Type Description
entData Object entity.data to get the size for.
Returns:
Type:
number
size of entity data in bytes.

(static) getPreviewUrl() → {string}

Given the entity, get URL which can be used for previewing the entity.
Source:
Parameters:
Name Type Description
entity.data Object to get the URl from.
Returns:
Type:
string
url for previewing or null if no such url is available.

(static) hasAttachments(content)

Check if the drafty document has attachments: style EX and outside of normal rendering flow, i.e. at = -1.
Source:
Parameters:
Name Type Description
content Drafty document to check for attachments.
Returns:
true if there are attachments.

(static) hasEntities(content)

Check if the drafty document has entities.
Source:
Parameters:
Name Type Description
content Drafty document to check for entities.
Returns:
true if there are entities.

(static) init(plainText)

Initialize Drafty document to a plain text string.
Source:
Parameters:
Name Type Description
plainText string string to use as Drafty content.
Returns:
new Drafty document or null is plainText is not a string or undefined.

(static) insertAudio(content, at, audioDesc) → {Drafty}

Insert audio recording into Drafty document.
Source:
Parameters:
Name Type Description
content Drafty document to add audio record to.
at integer index where the object is inserted. The length of the record is always 1.
audioDesc AudioDesc object with the audio paramenets and data.
Returns:
Type:
Drafty
updated document.

(static) insertButton(content, at, len, name, actionType, actionValue, refUrl) → {Drafty}

Insert clickable button into Drafty document.
Source:
Parameters:
Name Type Description
content Drafty | string Drafty document to insert button to or a string to be used as button text.
at number location where the button is inserted.
len number the length of the text to be used as button title.
name string the button. Client should return it to the server when the button is clicked.
actionType string the type of the button, one of 'url' or 'pub'.
actionValue string the value to return on click:
refUrl string the URL to go to when the 'url' button is clicked.
Returns:
Type:
Drafty
updated document.

(static) insertImage(content, at, imageDesc) → {Drafty}

Insert inline image into Drafty document.
Source:
Parameters:
Name Type Description
content Drafty document to add image to.
at integer index where the object is inserted. The length of the image is always 1.
imageDesc ImageDesc object with image paramenets and data.
Returns:
Type:
Drafty
updated document.

(static) insertVideo(content, at, videoDesc) → {Drafty}

Insert inline image into Drafty document.
Source:
Parameters:
Name Type Description
content Drafty document to add video to.
at integer index where the object is inserted. The length of the video is always 1.
videoDesc VideoDesc object with video paramenets and data.
Returns:
Type:
Drafty
updated document.

(static) isPlainText(content)

Check if the document has no markup and no entities.
Source:
Parameters:
Name Type Description
content Drafty content to check for presence of markup.
Returns:
true is content is plain text, false otherwise.

(static) isProcessing() → {boolean}

Check if the entity data is not ready for sending, such as being uploaded to the server.
Source:
Parameters:
Name Type Description
entity.data Object to get the URl from.
Returns:
Type:
boolean
true if upload is in progress, false otherwise.

(static) isValid(content)

Checks if the object represets is a valid Drafty document.
Source:
Parameters:
Name Type Description
content Drafty content to check for validity.
Returns:
true is content is valid, false otherwise.

(static) mention(name, uid) → {Drafty}

Create a Drafty document with a mention.
Source:
Parameters:
Name Type Description
name string mentioned name.
uid string mentioned user ID.
Returns:
Type:
Drafty
document with the mention.

(static) parse(content) → {Drafty}

Parse plain text into Drafty document.
Source:
Parameters:
Name Type Description
content string plain-text content to parse.
Returns:
Type:
Drafty
parsed document or null if the source is not plain text.

(static) preview(original, limit, forwarding)

Generate drafty preview: - Shorten the document. - Strip all heavy entity data leaving just inline styles and entity references. - Replace line breaks with spaces. - Replace content of QQ with a space. - Replace forwarding mention with symbol '➦'. move all attachments to the end of the document and make them visible. The context may expose a function getFormatter(style). If it's available it will call it to obtain a formatter for a subtree of styles under the style.
Source:
Parameters:
Name Type Description
original Drafty | string Drafty object to shorten.
limit number length in characters to shorten to.
forwarding boolean this a forwarding message preview.
Returns:
new shortened Drafty object leaving the original intact.

(static) quote(header, uid, body)

Create a quote to Drafty document.
Source:
Parameters:
Name Type Description
header string Quote header (title, etc.).
uid string UID of the author to mention.
body Drafty Body of the quoted message.
Returns:
Reply quote Drafty doc with the quote formatting.

(static) replyContent(original, limit)

Prepare Drafty doc for wrapping into QQ as a reply: - Replace forwarding mention with symbol '➦' and remove data (UID). - Remove quoted text completely. - Replace line breaks with spaces. - Strip entities of heavy content. - Move attachments to the end of the document.
Source:
Parameters:
Name Type Description
original Drafty | string Drafty object to shorten.
limit number length in characters to shorten to.
Returns:
converted Drafty object leaving the original intact.

(static) sanitizeEntities(content)

Remove unrecognized fields from entity data
Source:
Parameters:
Name Type Description
content Drafty document with entities to enumerate.
Returns:
content.

(static) shorten(original, limit, light)

Shorten Drafty document making the drafty text no longer than the limit.
Source:
Parameters:
Name Type Description
original Drafty | string Drafty object to shorten.
limit number length in characrets to shorten to.
light boolean remove heavy data from entities.
Returns:
new shortened Drafty object leaving the original intact.

(static) styles(content, callback, context)

Enumerate styles (inline formats). Enumeration stops if callback returns 'true'.
Source:
Parameters:
Name Type Description
content Drafty document with styles (formats) to enumerate.
callback StyleCallback callback to call for each format.
context Object value of "this" for callback.

(static) tagName(style) → {string}

Get HTML tag for a given two-letter style name.
Source:
Parameters:
Name Type Description
style string two-letter style, like ST or LN.
Returns:
Type:
string
HTML tag name if style is found, {code: undefined} if style is falsish or not found.

(static) toMarkdown(content)

Convert document to plain text with markdown. All elements which cannot be represented in markdown are stripped.
Source:
Parameters:
Name Type Description
content Drafty document to convert to plain text with markdown.

(static) toPlainText(content) → {string}

Given Drafty document, convert it to plain text.
Source:
Parameters:
Name Type Description
content Drafty document to convert to plain text.
Returns:
Type:
string
plain-text representation of the drafty document.

(static) updateVideoCall(content, params)

Update video call (VC) entity with the new status and duration.
Source:
Parameters:
Name Type Description
content Drafty VC document to update.
params object new video call parameters.
Name Type Description
state string state of video call.
duration number duration of the video call in milliseconds.
Returns:
the same document with update applied.

(static) videoCall(audioOnly)

Create a (self-contained) video call Drafty document.
Source:
Parameters:
Name Type Description
audioOnly boolean true if the call is initially audio-only.
Returns:
Video Call drafty document.

(static) wrapAsForm(content, at, len) → {Drafty}

Wraps content into an interactive form.
Source:
Parameters:
Name Type Description
content Drafty | string to wrap into a form.
at number index where the forms starts.
len number length of the form content.
Returns:
Type:
Drafty
updated document.

(static) wrapInto(content, style, at, len) → {Drafty}

Wraps drafty document into a simple formatting style.
Source:
Parameters:
Name Type Description
content Drafty | string document or string to wrap into a style.
style string two-letter style to wrap into.
at number index where the style starts, default 0.
len number length of the form content, default all of it.
Returns:
Type:
Drafty
updated document.

Type Definitions

AttachmentDesc

Description of a file to attach.
Properties:
Name Type Description
mime string mime-type of the attachment, e.g. "application/octet-stream"
data string base64-encoded in-band content of small attachments. Could be null/undefined.
filename string file name suggestion for downloading the attachment.
size integer size of the file in bytes. Treat is as an untrusted hint.
refurl string reference to the out-of-band content. Could be null/undefined.
urlPromise Promise Promise which returns content URL when resolved.
Source:
Type:
  • Object

AudioDesc

Description of an audio recording to attach.
Properties:
Name Type Description
mime string mime-type of the audio, e.g. "audio/ogg".
refurl string reference to the content. Could be null/undefined.
bits string base64-encoded audio content. Could be null/undefined.
duration integer duration of the record in milliseconds.
preview string base64 encoded short array of amplitude values 0..100.
filename string file name suggestion for downloading the audio.
size integer size of the recording in bytes. Treat is as an untrusted hint.
urlPromise Promise Promise which returns content URL when resolved.
Source:
Type:
  • Object

EntityCallback(data, entity, index)

Callback for enumerating entities in a Drafty document. Called once for each entity.
Source:
Parameters:
Name Type Description
data Object entity data.
entity string type.
index number entity's index in `content.ent`.
Returns:
'true-ish' to stop processing, 'false-ish' otherwise.

Formatter(style, data, values, index)

Callback for applying custom formatting to a Drafty document. Called once for each style span.
Source:
Parameters:
Name Type Description
style string style code such as "ST" or "IM".
data Object entity's data.
values Object possibly styled subspans contained in this style span.
index number index of the element guaranteed to be unique.

ImageDesc

Description of an image to attach.
Properties:
Name Type Description
mime string mime-type of the image, e.g. "image/png".
refurl string reference to the content. Could be null/undefined.
bits string base64-encoded image content. Could be null/undefined.
preview string base64-encoded thumbnail of the image.
width integer width of the image.
height integer height of the image.
filename string file name suggestion for downloading the image.
size integer size of the image in bytes. Treat is as an untrusted hint.
_tempPreview string base64-encoded image preview used during upload process; not serializable.
urlPromise Promise Promise which returns content URL when resolved.
Source:
Type:
  • Object

StyleCallback(tp, at, len, key, index)

Callback for enumerating styles (inline formats) in a Drafty document. Called once for each style.
Source:
Parameters:
Name Type Description
tp string format type.
at number starting position of the format in text.
len number extent of the format in characters.
key number index of the entity if format is a reference.
index number style's index in `content.fmt`.
Returns:
'true-ish' to stop processing, 'false-ish' otherwise.

VideoDesc

Description of a video to attach.
Properties:
Name Type Description
mime string mime-type of the video, e.g. "video/mpeg".
refurl string reference to the content. Could be null/undefined.
bits string in-band base64-encoded image data. Could be null/undefined.
preview string base64-encoded screencapture from the video. Could be null/undefined.
preref string reference to screencapture from the video. Could be null/undefined.
width integer width of the video.
height integer height of the video.
duration integer duration of the video.
filename string file name suggestion for downloading the video.
size integer size of the video in bytes. Treat is as an untrusted hint.
_tempPreview string base64-encoded screencapture used during upload process; not serializable.
urlPromise Promise array of two promises, which return URLs of video and preview uploads correspondingly (either could be null).
Source:
Type:
  • Object