import _ from 'lodash' import $ from 'jquery' import he from 'he' import dragula from 'dragula' import textareaAutosizeInit from "./textarea.autosize" import createCursor from './cursor' import createSelection from './selection' import Store from './store' textareaAutosizeInit($) function editor(root, inputData, options) { let cursor = createCursor() let selection = createSelection() let store = createStore(inputData); let drake = null; function createStore(inputData) { let data = [ {indented: 0, text: '', fold: 'open'}, ]; if (inputData.length) { data = inputData } return Store(data); } function save() { return new Promise(function (resolve, reject) { if (store.hasChanged()) { resolve(store.debug().result) store.clearChanged() } else { reject() } }); } function saveTree(from) { return new Promise(function (resolve, reject) { if (store.hasChanged()) { resolve(store.tree(from)) store.clearChanged() } }); } function treeForId(id) { return new Promise(function (resolve, reject) { resolve(store.tree(id)) }) } function copy(element, opt) { let item = $(element).parents('.list-item') let id = item.attr('data-id') if (opt.recursive) { return saveTree(id) } return new Promise(function (resolve, reject) { resolve(store.value(id)); }); } function zoomin(element, opt) { let item = $(element).parents('.list-item') let id = item.attr('data-id') return new Promise(function (resolve, reject) { resolve(id); }); } function on(evt, handler) { events[evt].push(handler) } function trigger(event) { let args = [...arguments] args.splice(0, 1) _.each(events[event], function (handler) { handler(...args) }) } function start() { disableDragging(drake) render(root, store); drake = enableDragging(root) cursor.set(0) startEditing(root, store, cursor) } function replaceChildren(id, children) { store.replaceChildren(id, children) } function renderUpdate() { disableDragging(drake) render(root, store); drake = enableDragging(root) } let EDITOR = { on, save, saveTree, copy, update, start, zoomin, treeForId, replaceChildren, render: renderUpdate }; root.classList.add('root') root.setAttribute('tabindex', '-1') let defaults = { transform(text, element) { element.html(he.encode(text)) } } options = _.merge(defaults, options) let events = { change: [], 'start-editing': [], 'stop-editing': [], 'rendering': [], 'rendered': [] } let editing = false let currentEditor = null; function newListItem(indented) { return {indented: indented, text: '', fold: 'open', hidden: false} } function newItem(value) { let el = $('