substance
Version:
Substance is a JavaScript library for web-based content editing. It provides building blocks for realizing custom text editors and web-based publishing systems.
45 lines (43 loc) • 1.13 kB
JavaScript
import { Document, EditorSession } from 'substance'
import getTestConfig from './getTestConfig'
export default function createEditorSession(...seeds) {
let config = getTestConfig()
let doc = new Document(config.getSchema())
let body = doc.create({
type: 'container',
id: 'body'
})
seeds.forEach((seed)=>{
seed(doc, body)
})
let editorSession = new EditorSession(doc, { configurator: config })
let first = body.getNodeAt(0)
if (first) {
if (first.isText()) {
editorSession.setSelection({
type: 'property',
path: first.getTextPath(),
startOffset: 0,
containerId: 'body',
surfaceId: 'body'
})
} else if (first.isList()) {
editorSession.setSelection({
type: 'property',
path: first.getItemAt(0).getTextPath(),
startOffset: 0,
containerId: 'body',
surfaceId: 'body'
})
} else {
editorSession.setSelection({
type: 'node',
nodeId: first.id,
mode: 'before',
containerId: 'body',
surfaceId: 'body'
})
}
}
return editorSession
}