Created
October 11, 2017 17:49
-
-
Save ericeslinger/a7c036bc36aa6408008f375872b03d85 to your computer and use it in GitHub Desktop.
rich text / quill snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function snippetize(delta: DeltaStatic, maxLength: number): { ops: any[] } { | |
const retVal = { | |
ops: [], | |
}; | |
let done = false; | |
let idx = 0; | |
let soFar = 0; | |
while (!done && delta.ops.length > idx) { | |
const op = Object.assign({}, delta.ops[idx]); | |
idx = idx + 1; | |
if (typeof op.insert === 'string') { | |
op.insert = op.insert.replace(/\n/g, ' '); | |
if (op.attributes) { | |
delete op.attributes.list; | |
delete op.attributes.bullet; | |
delete op.attributes.ordered; | |
delete op.attributes.header; | |
} | |
if (soFar + op.insert.length > maxLength) { | |
op.insert = op.insert.slice(0, maxLength - soFar); | |
op.insert = op.insert.slice(0, op.insert.lastIndexOf(' ')); | |
done = true; | |
} | |
soFar = soFar + op.insert.length; | |
retVal.ops.push(op); | |
} else if (op.insert.atmention || op.insert.hashtag) { | |
retVal.ops.push(op); | |
} else if (op.insert['trellis-image']) { | |
retVal.ops.push(op); | |
done = true; | |
} | |
} | |
if (done) { | |
retVal.ops.push({ insert: '... \n' }); | |
} | |
return retVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment