This commit is contained in:
Kevin Jahns
2018-04-27 18:33:28 +02:00
parent 99f92cb9a0
commit a54d826d6d
20 changed files with 82 additions and 77 deletions

View File

@@ -51,9 +51,9 @@ export default class DomBinding extends Binding {
this.filter = opts.filter || defaultFilter
// set initial value
target.innerHTML = ''
for (let child of type) {
type.forEach(child => {
target.insertBefore(child.toDom(opts.document, opts.hooks, this), null)
}
})
this._typeObserver = typeObserver.bind(this)
this._domObserver = (mutations) => {
domObserver.call(this, mutations, opts.document)

View File

@@ -1,35 +1,37 @@
import { YXmlText, YXmlElement, YXmlHook } from '../../Types/YXml/YXml.js'
import { createAssociation } from './util.js'
import { filterDomAttributes } from './filter.js'
import { createAssociation, domsToTypes } from './util.js'
import { filterDomAttributes, defaultFilter } from './filter.js'
/**
* Creates a Yjs type (YXml) based on the contents of a DOM Element.
*
* @param {Element|TextNode} element The DOM Element
* @param {?Document} _document Optional. Provide the global document object.
* @param {?DomBinding} binding This property should only be set if the type
* is going to be bound with the dom-binding.
* @param {?Document} _document Optional. Provide the global document object
* @param {Hooks} [hooks = {}] Optional. Set of Yjs Hooks
* @param {Filter} [filter=defaultFilter] Optional. Dom element filter
* @param {?DomBinding} binding Warning: This property is for internal use only!
* @return {YXmlElement | YXmlText}
*/
export default function domToType (element, _document = document, binding) {
export default function domToType (element, _document = document, hooks = {}, filter = defaultFilter, binding) {
let type
switch (element.nodeType) {
case _document.ELEMENT_NODE:
let hookName = element.dataset.yjsHook
let hookName = null
let hook
// configure `hookName !== undefined` if element is a hook.
if (hookName !== undefined) {
hook = binding.opts.hooks[hookName]
if (element.hasAttribute('data-yjs-hook')) {
hookName = element.getAttribute('data-yjs-hook')
hook = hooks[hookName]
if (hook === undefined) {
console.error(`Unknown hook "${hookName}". Deleting yjsHook dataset property.`)
delete element.dataset.yjsHook
hookName = undefined
delete element.removeAttribute('data-yjs-hook')
hookName = null
}
}
if (hookName === undefined) {
if (hookName === null) {
// Not a hook
const attrs = filterDomAttributes(element, binding.filter)
const attrs = filterDomAttributes(element, filter)
if (attrs === null) {
type = false
} else {
@@ -37,14 +39,7 @@ export default function domToType (element, _document = document, binding) {
attrs.forEach((val, key) => {
type.setAttribute(key, val)
})
const children = []
for (let elem of element.childNodes) {
const type = domToType(elem, _document, binding)
if (type !== false) {
children.push(type)
}
}
type.insert(0, children)
type.insert(0, domsToTypes(element.childNodes, document, hooks, filter, binding))
}
} else {
// Is a hook

View File

@@ -14,7 +14,7 @@ export function defaultFilter (nodeName, attrs) {
}
/**
*
*
*/
export function filterDomAttributes (dom, filter) {
const attrs = new Map()

View File

@@ -17,7 +17,10 @@ export function iterateUntilUndeleted (item) {
* Removes an association (the information that a DOM element belongs to a
* type).
*
* @private
* @param {DomBinding} domBinding The binding object
* @param {Element} dom The dom that is to be associated with type
* @param {YXmlElement|YXmlHook} type The type that is to be associated with dom
*
*/
export function removeAssociation (domBinding, dom, type) {
domBinding.domToType.delete(dom)
@@ -28,7 +31,10 @@ export function removeAssociation (domBinding, dom, type) {
* Creates an association (the information that a DOM element belongs to a
* type).
*
* @private
* @param {DomBinding} domBinding The binding object
* @param {Element} dom The dom that is to be associated with type
* @param {YXmlElement|YXmlHook} type The type that is to be associated with dom
*
*/
export function createAssociation (domBinding, dom, type) {
if (domBinding !== undefined) {
@@ -37,6 +43,24 @@ export function createAssociation (domBinding, dom, type) {
}
}
/**
* If oldDom is associated with a type, associate newDom with the type and
* forget about oldDom. If oldDom is not associated with any type, nothing happens.
*
* @param {DomBinding} domBinding The binding object
* @param {Element} oldDom The existing dom
* @param {Element} newDom The new dom object
*/
export function switchAssociation (domBinding, oldDom, newDom) {
if (domBinding !== undefined) {
const type = domBinding.domToType.get(oldDom)
if (type !== undefined) {
removeAssociation(domBinding, oldDom, type)
createAssociation(domBinding, newDom, type)
}
}
}
/**
* Insert Dom Elements after one of the children of this YXmlFragment.
* The Dom elements will be bound to a new YXmlElement and inserted at the
@@ -54,14 +78,19 @@ export function createAssociation (domBinding, dom, type) {
* @private
*/
export function insertDomElementsAfter (type, prev, doms, _document, binding) {
const types = domsToTypes(doms, _document, binding.opts.hooks, binding.filter, binding)
return type.insertAfter(prev, types)
}
export function domsToTypes (doms, _document, hooks, filter, binding) {
const types = []
for (let dom of doms) {
const t = domToType(dom, _document, binding)
const t = domToType(dom, _document, hooks, filter, binding)
if (t !== false) {
types.push(t)
}
}
return type.insertAfter(prev, types)
return types
}
/**

View File

@@ -1,7 +1,7 @@
import { getStruct } from '../Util/structReferences.js'
import BinaryDecoder from '../Util/Binary/Decoder.js'
import { logID } from './messageToString.js'
import GC from '../Struct/GC.js';
import GC from '../Struct/GC.js'
class MissingEntry {
constructor (decoder, missing, struct) {

View File

@@ -91,4 +91,4 @@ export default class GC {
gc._length = this._length - diff
return gc
}
}
}

View File

@@ -238,7 +238,7 @@ export default class Item {
}
_gcChildren (y) {}
_gc (y) {
const gc = new GC()
gc._id = this._id

View File

@@ -213,6 +213,8 @@ export default class Type extends Item {
* @param {Y} y The Yjs instance
* @param {boolean} createDelete Whether to propagate a message that this
* Type was deleted.
* @param {boolean} [gcChildren=y._hasUndoManager===false] Whether to garbage
* collect the children of this type.
*/
_delete (y, createDelete, gcChildren) {
if (gcChildren === undefined) {

View File

@@ -198,7 +198,7 @@ export default class YArray extends Type {
content = this._item._content[this._itemElement++]
}
return {
value: [this._count, content],
value: content,
done: false
}
},

View File

@@ -123,6 +123,8 @@ export default class YXmlFragment extends YArray {
* @param {Y} y The Yjs instance
* @param {boolean} createDelete Whether to propagate a message that this
* Type was deleted.
* @param {boolean} [gcChildren=y._hasUndoManager===false] Whether to garbage
* collect the children of this type.
*
* @private
*/

View File

@@ -52,7 +52,7 @@ export default class YXmlHook extends YMap {
} else {
dom = document.createElement(this.hookName)
}
dom.dataset.yjsHook = this.hookName
dom.setAttribute('data-yjs-hook', this.hookName)
createAssociation(binding, dom, this)
return dom
}

View File

@@ -35,6 +35,8 @@ export default class YXmlText extends YText {
* @param {Y} y The Yjs instance
* @param {boolean} createDelete Whether to propagate a message that this
* Type was deleted.
* @param {boolean} [gcChildren=y._hasUndoManager===false] Whether to garbage
* collect the children of this type.
*
* @private
*/

View File

@@ -1,5 +1,6 @@
import ID from './ID/ID.js'
import RootID from './ID/RootID.js'
import GC from '../Struct/GC.js'
// TODO: Implement function to describe ranges
@@ -76,6 +77,9 @@ export function fromRelativePosition (y, rpos) {
id = new RootID(rpos[3], rpos[4])
}
const type = y.os.get(id)
if (type === null || type.constructor === GC) {
return null
}
return {
type,
offset: type.length
@@ -84,7 +88,7 @@ export function fromRelativePosition (y, rpos) {
let offset = 0
let struct = y.os.findNodeWithUpperBound(new ID(rpos[0], rpos[1])).val
const parent = struct._parent
if (parent._deleted) {
if (struct.constructor === GC || parent._deleted) {
return null
}
if (!struct._deleted) {

View File

@@ -56,4 +56,4 @@ registerStruct(7, YXmlElement)
registerStruct(8, YXmlText)
registerStruct(9, YXmlHook)
registerStruct(12, GC)
registerStruct(12, GC)

View File

@@ -20,6 +20,8 @@ import DomBinding from './Bindings/DomBinding/DomBinding.js'
import { toBinary, fromBinary } from './MessageHandler/binaryEncode.js'
import debug from 'debug'
import domToType from './Bindings/DomBinding/domToType.js'
import { domsToTypes, switchAssociation } from './Bindings/DomBinding/util.js'
// TODO: The following assignments should be moved to yjs-dist
Y.AbstractConnector = Connector
@@ -36,6 +38,10 @@ Y.TextareaBinding = TextareaBinding
Y.QuillBinding = QuillBinding
Y.DomBinding = DomBinding
DomBinding.domToType = domToType
DomBinding.domsToTypes = domsToTypes
DomBinding.switchAssociation = switchAssociation
Y.utils = {
BinaryDecoder,
UndoManager,