@limitless-angular/sanity
Version:
A powerful Angular library for Sanity.io integration, featuring Portable Text rendering and optimized image loading.
78 lines (74 loc) • 3.44 kB
JavaScript
import * as i0 from '@angular/core';
import { inject, PLATFORM_ID, DestroyRef, Injectable } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { isPlatformBrowser } from '@angular/common';
import { BehaviorSubject, combineLatest } from 'rxjs';
import { filter, distinctUntilChanged } from 'rxjs/operators';
import { createNode, createNodeMachine } from '@sanity/comlink';
import { createCompatibilityActors } from '@limitless-angular/sanity/visual-editing-helpers';
class UseDocumentsInUseService {
constructor() {
this.isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
this.destroyRef = inject(DestroyRef);
this.comlink$ = new BehaviorSubject(null);
this.connected$ = new BehaviorSubject(false);
this.documentsInUse$ = new BehaviorSubject('[]');
this.documentsInUse = new Map();
}
initialize(config) {
if (this.isBrowser) {
this.setupChannelIfNeeded();
this.setupDocumentSync(config);
}
return this;
}
setupChannelIfNeeded() {
if (window.self !== window.top || window.opener) {
const comlink = createNode({
name: 'preview-kit',
connectTo: 'presentation',
}, createNodeMachine().provide({
actors: createCompatibilityActors(),
}));
comlink.onStatus((status) => this.connected$.next(status === 'connected'));
const timeout = setTimeout(() => this.comlink$.next(comlink), 0);
const stop = comlink.start();
this.destroyRef.onDestroy(() => {
stop();
this.comlink$.next(null);
clearTimeout(timeout);
});
}
}
setupDocumentSync({ projectId, dataset }) {
combineLatest([this.comlink$, this.connected$, this.documentsInUse$])
.pipe(filter(([comlink, connected, changedKeys]) => !!comlink && connected && changedKeys !== '[]'), distinctUntilChanged(), takeUntilDestroyed(this.destroyRef))
.subscribe(([comlink]) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
comlink.post({
type: 'preview-kit/documents',
data: {
projectId,
dataset,
perspective: 'previewDrafts',
documents: Array.from(this.documentsInUse.values()),
},
});
});
}
updateDocumentsInUse(documentsInUse) {
this.documentsInUse = documentsInUse;
const changedKeys = JSON.stringify(Array.from(documentsInUse.keys()));
this.documentsInUse$.next(changedKeys);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.3", ngImport: i0, type: UseDocumentsInUseService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.0.3", ngImport: i0, type: UseDocumentsInUseService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.3", ngImport: i0, type: UseDocumentsInUseService, decorators: [{
type: Injectable
}] });
/**
* Generated bundle index. Do not edit.
*/
export { UseDocumentsInUseService };
//# sourceMappingURL=limitless-angular-sanity-preview-kit-compat.mjs.map