Merge branch 'raineorshine-getXmlElement'

This commit is contained in:
Kevin Jahns 2024-02-09 23:31:29 +01:00
commit f54ea625e2
5 changed files with 778 additions and 394 deletions

View File

@ -739,6 +739,8 @@ type. Doesn't log types that have not been defined (using
<dd>Define a shared Y.Map type. Is equivalent to <code>y.get(string, Y.Map)</code>.</dd> <dd>Define a shared Y.Map type. Is equivalent to <code>y.get(string, Y.Map)</code>.</dd>
<b><code>getText(string):Y.Text</code></b> <b><code>getText(string):Y.Text</code></b>
<dd>Define a shared Y.Text type. Is equivalent to <code>y.get(string, Y.Text)</code>.</dd> <dd>Define a shared Y.Text type. Is equivalent to <code>y.get(string, Y.Text)</code>.</dd>
<b><code>getXmlElement(string, string):Y.XmlElement</code></b>
<dd>Define a shared Y.XmlElement type. Is equivalent to <code>y.get(string, Y.XmlElement)</code>.</dd>
<b><code>getXmlFragment(string):Y.XmlFragment</code></b> <b><code>getXmlFragment(string):Y.XmlFragment</code></b>
<dd>Define a shared Y.XmlFragment type. Is equivalent to <code>y.get(string, Y.XmlFragment)</code>.</dd> <dd>Define a shared Y.XmlFragment type. Is equivalent to <code>y.get(string, Y.XmlFragment)</code>.</dd>
<b><code>on(string, function)</code></b> <b><code>on(string, function)</code></b>

1125
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@ import {
/** /**
* An YXmlElement imitates the behavior of a * An YXmlElement imitates the behavior of a
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Element|Dom Element}. * https://developer.mozilla.org/en-US/docs/Web/API/Element|Dom Element
* *
* * An YXmlElement has attributes (key value pairs) * * An YXmlElement has attributes (key value pairs)
* * An YXmlElement has childElements that must inherit from YXmlElement * * An YXmlElement has childElements that must inherit from YXmlElement

View File

@ -8,6 +8,7 @@ import {
YArray, YArray,
YText, YText,
YMap, YMap,
YXmlElement,
YXmlFragment, YXmlFragment,
transact, transact,
ContentDoc, Item, Transaction, YEvent // eslint-disable-line ContentDoc, Item, Transaction, YEvent // eslint-disable-line
@ -180,6 +181,7 @@ export class Doc extends Observable {
* Define all types right after the Yjs instance is created and store them in a separate object. * Define all types right after the Yjs instance is created and store them in a separate object.
* Also use the typed methods `getText(name)`, `getArray(name)`, .. * Also use the typed methods `getText(name)`, `getArray(name)`, ..
* *
* @template {typeof AbstractType<any>} Type
* @example * @example
* const y = new Y(..) * const y = new Y(..)
* const appState = { * const appState = {
@ -188,12 +190,12 @@ export class Doc extends Observable {
* } * }
* *
* @param {string} name * @param {string} name
* @param {Function} TypeConstructor The constructor of the type definition. E.g. Y.Text, Y.Array, Y.Map, ... * @param {Type} TypeConstructor The constructor of the type definition. E.g. Y.Text, Y.Array, Y.Map, ...
* @return {AbstractType<any>} The created type. Constructed with TypeConstructor * @return {InstanceType<Type>} The created type. Constructed with TypeConstructor
* *
* @public * @public
*/ */
get (name, TypeConstructor = AbstractType) { get (name, TypeConstructor = /** @type {any} */ (AbstractType)) {
const type = map.setIfUndefined(this.share, name, () => { const type = map.setIfUndefined(this.share, name, () => {
// @ts-ignore // @ts-ignore
const t = new TypeConstructor() const t = new TypeConstructor()
@ -219,12 +221,12 @@ export class Doc extends Observable {
t._length = type._length t._length = type._length
this.share.set(name, t) this.share.set(name, t)
t._integrate(this, null) t._integrate(this, null)
return t return /** @type {InstanceType<Type>} */ (t)
} else { } else {
throw new Error(`Type with the name ${name} has already been defined with a different constructor`) throw new Error(`Type with the name ${name} has already been defined with a different constructor`)
} }
} }
return type return /** @type {InstanceType<Type>} */ (type)
} }
/** /**
@ -235,8 +237,7 @@ export class Doc extends Observable {
* @public * @public
*/ */
getArray (name = '') { getArray (name = '') {
// @ts-ignore return /** @type {YArray<T>} */ (this.get(name, YArray))
return this.get(name, YArray)
} }
/** /**
@ -246,7 +247,6 @@ export class Doc extends Observable {
* @public * @public
*/ */
getText (name = '') { getText (name = '') {
// @ts-ignore
return this.get(name, YText) return this.get(name, YText)
} }
@ -258,8 +258,17 @@ export class Doc extends Observable {
* @public * @public
*/ */
getMap (name = '') { getMap (name = '') {
// @ts-ignore return /** @type {YMap<T>} */ (this.get(name, YMap))
return this.get(name, YMap) }
/**
* @param {string} [name]
* @return {YXmlElement}
*
* @public
*/
getXmlElement (name = '') {
return /** @type {YXmlElement<{[key:string]:string}>} */ (this.get(name, YXmlElement))
} }
/** /**
@ -269,7 +278,6 @@ export class Doc extends Observable {
* @public * @public
*/ */
getXmlFragment (name = '') { getXmlFragment (name = '') {
// @ts-ignore
return this.get(name, YXmlFragment) return this.get(name, YXmlFragment)
} }

View File

@ -189,7 +189,6 @@ export const testClone = _tc => {
const third = new Y.XmlElement('p') const third = new Y.XmlElement('p')
yxml.push([first, second, third]) yxml.push([first, second, third])
t.compareArrays(yxml.toArray(), [first, second, third]) t.compareArrays(yxml.toArray(), [first, second, third])
const cloneYxml = yxml.clone() const cloneYxml = yxml.clone()
ydoc.getArray('copyarr').insert(0, [cloneYxml]) ydoc.getArray('copyarr').insert(0, [cloneYxml])
t.assert(cloneYxml.length === 3) t.assert(cloneYxml.length === 3)
@ -210,3 +209,15 @@ export const testFormattingBug = _tc => {
yxml.applyDelta(delta) yxml.applyDelta(delta)
t.compare(yxml.toDelta(), delta) t.compare(yxml.toDelta(), delta)
} }
/**
* @param {t.TestCase} _tc
*/
export const testElement = _tc => {
const ydoc = new Y.Doc()
const yxmlel = ydoc.getXmlElement()
const text1 = new Y.XmlText('text1')
const text2 = new Y.XmlText('text2')
yxmlel.insert(0, [text1, text2])
t.compareArrays(yxmlel.toArray(), [text1, text2])
}