2021-08-03 16:55:52 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import type { Doc, PropertyType } from './classes'
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export type _OperatorFunc = (doc: Doc, op: object) => void
|
2021-08-03 16:55:52 +00:00
|
|
|
|
|
|
|
function $push (document: Doc, keyval: Record<string, PropertyType>): void {
|
|
|
|
const doc = document as any
|
|
|
|
for (const key in keyval) {
|
|
|
|
const arr = doc[key]
|
|
|
|
if (arr === undefined) {
|
|
|
|
doc[key] = [keyval[key]]
|
|
|
|
} else {
|
|
|
|
arr.push(keyval[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 09:15:09 +00:00
|
|
|
function $pushMixin (document: Doc, options: any): void {
|
|
|
|
const doc = document as any
|
|
|
|
const mixinId = options.$mixin
|
|
|
|
if (mixinId === undefined) { throw new Error('$mixin must be specified for $push_mixin operation') }
|
|
|
|
const mixin = doc[mixinId]
|
|
|
|
const keyval = options.values
|
|
|
|
for (const key in keyval) {
|
|
|
|
const arr = mixin[key]
|
|
|
|
if (arr === undefined) {
|
|
|
|
mixin[key] = [keyval[key]]
|
|
|
|
} else {
|
|
|
|
arr.push(keyval[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
const operators: Record<string, _OperatorFunc> = {
|
2021-08-05 09:15:09 +00:00
|
|
|
$push,
|
|
|
|
$pushMixin
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @param name -
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function _getOperator (name: string): _OperatorFunc {
|
2021-08-03 16:55:52 +00:00
|
|
|
const operator = operators[name]
|
|
|
|
if (operator === undefined) throw new Error('unknown operator: ' + name)
|
|
|
|
return operator
|
|
|
|
}
|