Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@blocksuite/virgo

Package Overview
Dependencies
Maintainers
5
Versions
509
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@blocksuite/virgo - npm Package Compare versions

Comparing version 0.0.0-20230924223737-8993bc0f-nightly to 0.0.0-20230925163612-6d2dbfcc-nightly

12

dist/components/virgo-line.d.ts

@@ -6,8 +6,8 @@ import { LitElement, type TemplateResult } from 'lit';

get vElements(): import("./virgo-element.js").VirgoElement<{
bold?: true | undefined;
italic?: true | undefined;
underline?: true | undefined;
strike?: true | undefined;
code?: true | undefined;
link?: string | undefined;
bold?: true | null | undefined;
italic?: true | null | undefined;
underline?: true | null | undefined;
strike?: true | null | undefined;
code?: true | null | undefined;
link?: string | null | undefined;
}>[];

@@ -14,0 +14,0 @@ get vTexts(): import("./virgo-text.js").VText[];

@@ -62,3 +62,3 @@ import { baseTextAttributes, getDefaultAttributeRenderer, } from '../utils/index.js';

// filter out undefined values
Object.entries(attributeResult.data).filter(([_, v]) => v));
Object.entries(attributeResult.data).filter(([_, v]) => v || v === null));
};

@@ -65,0 +65,0 @@ }

@@ -1,3 +0,3 @@

import type { VRange } from '../types.js';
import type { VRangeUpdatedProp } from '../types.js';
import type { VirgoLine } from '../components/virgo-line.js';
import type { TextPoint, VRange, VRangeUpdatedProp } from '../types.js';
import type { BaseTextAttributes } from '../utils/base-attributes.js';

@@ -10,4 +10,9 @@ import type { VEditor } from '../virgo.js';

get vRangeProvider(): import("../virgo.js").VRangeProvider | null;
get rootElement(): import("../virgo.js").VirgoRootElement<TextAttributes>;
onVRangeUpdated: ([newVRange, sync]: VRangeUpdatedProp) => Promise<void>;
getNativeSelection(): Selection | null;
getVRange: () => VRange | null;
getVRangeFromElement: (element: Element) => VRange | null;
getTextPoint(rangeIndex: VRange['index']): TextPoint;
getLine(rangeIndex: VRange['index']): readonly [VirgoLine, number];
isVRangeValid: (vRange: VRange | null) => boolean;

@@ -14,0 +19,0 @@ /**

@@ -0,3 +1,5 @@

import { assertExists } from '@blocksuite/global/utils';
import { findDocumentOrShadowRoot } from '../utils/query.js';
import { domRangeToVirgoRange, virgoRangeToDomRange, } from '../utils/range-conversion.js';
import { calculateTextLength, getTextNodesFromElement } from '../utils/text.js';
import { isMaybeVRangeEqual } from '../utils/v-range.js';

@@ -51,2 +53,15 @@ export class VirgoRangeService {

};
this.getVRangeFromElement = (element) => {
const range = document.createRange();
const text = element.querySelector('[data-virgo-text');
if (!text) {
return null;
}
const textNode = text.childNodes[1];
assertExists(textNode instanceof Text);
range.setStart(textNode, 0);
range.setEnd(textNode, textNode.textContent?.length ?? 0);
const vRange = this.toVRange(range);
return vRange;
};
this.isVRangeValid = (vRange) => {

@@ -135,3 +150,49 @@ return !(vRange &&

}
get rootElement() {
return this.editor.rootElement;
}
getNativeSelection() {
const selectionRoot = findDocumentOrShadowRoot(this.editor);
const selection = selectionRoot.getSelection();
if (!selection)
return null;
if (selection.rangeCount === 0)
return null;
return selection;
}
getTextPoint(rangeIndex) {
const vLines = Array.from(this.rootElement.querySelectorAll('v-line'));
let index = 0;
for (const vLine of vLines) {
const texts = getTextNodesFromElement(vLine);
for (const text of texts) {
if (!text.textContent) {
throw new Error('text element should have textContent');
}
if (index + text.textContent.length >= rangeIndex) {
return [text, rangeIndex - index];
}
index += calculateTextLength(text);
}
index += 1;
}
throw new Error('failed to find leaf');
}
// the number is related to the VirgoLine's textLength
getLine(rangeIndex) {
const lineElements = Array.from(this.rootElement.querySelectorAll('v-line'));
let index = 0;
for (const lineElement of lineElements) {
if (rangeIndex >= index && rangeIndex <= index + lineElement.textLength) {
return [lineElement, rangeIndex - index];
}
if (rangeIndex === index + lineElement.textLength &&
rangeIndex === this.editor.yTextLength) {
return [lineElement, rangeIndex - index];
}
index += lineElement.textLength + 1;
}
throw new Error('failed to find line');
}
}
//# sourceMappingURL=range.js.map
import type { AttributeRenderer } from '../types.js';
export declare const getDefaultAttributeRenderer: <T extends {
bold?: true | undefined;
italic?: true | undefined;
underline?: true | undefined;
strike?: true | undefined;
code?: true | undefined;
link?: string | undefined;
bold?: true | null | undefined;
italic?: true | null | undefined;
underline?: true | null | undefined;
strike?: true | null | undefined;
code?: true | null | undefined;
link?: string | null | undefined;
}>() => AttributeRenderer<T>;
//# sourceMappingURL=attribute-renderer.d.ts.map
import { z } from 'zod';
export declare const baseTextAttributes: z.ZodObject<{
bold: z.ZodCatch<z.ZodOptional<z.ZodLiteral<true>>>;
italic: z.ZodCatch<z.ZodOptional<z.ZodLiteral<true>>>;
underline: z.ZodCatch<z.ZodOptional<z.ZodLiteral<true>>>;
strike: z.ZodCatch<z.ZodOptional<z.ZodLiteral<true>>>;
code: z.ZodCatch<z.ZodOptional<z.ZodLiteral<true>>>;
link: z.ZodCatch<z.ZodOptional<z.ZodString>>;
bold: z.ZodCatch<z.ZodNullable<z.ZodOptional<z.ZodLiteral<true>>>>;
italic: z.ZodCatch<z.ZodNullable<z.ZodOptional<z.ZodLiteral<true>>>>;
underline: z.ZodCatch<z.ZodNullable<z.ZodOptional<z.ZodLiteral<true>>>>;
strike: z.ZodCatch<z.ZodNullable<z.ZodOptional<z.ZodLiteral<true>>>>;
code: z.ZodCatch<z.ZodNullable<z.ZodOptional<z.ZodLiteral<true>>>>;
link: z.ZodCatch<z.ZodNullable<z.ZodOptional<z.ZodString>>>;
}, "strip", z.ZodTypeAny, {
bold?: true | undefined;
italic?: true | undefined;
underline?: true | undefined;
strike?: true | undefined;
code?: true | undefined;
link?: string | undefined;
bold?: true | null | undefined;
italic?: true | null | undefined;
underline?: true | null | undefined;
strike?: true | null | undefined;
code?: true | null | undefined;
link?: string | null | undefined;
}, {

@@ -17,0 +17,0 @@ bold?: unknown;

import { z } from 'zod';
export const baseTextAttributes = z.object({
bold: z.literal(true).optional().catch(undefined),
italic: z.literal(true).optional().catch(undefined),
underline: z.literal(true).optional().catch(undefined),
strike: z.literal(true).optional().catch(undefined),
code: z.literal(true).optional().catch(undefined),
link: z.string().optional().catch(undefined),
bold: z.literal(true).optional().nullable().catch(undefined),
italic: z.literal(true).optional().nullable().catch(undefined),
underline: z.literal(true).optional().nullable().catch(undefined),
strike: z.literal(true).optional().nullable().catch(undefined),
code: z.literal(true).optional().nullable().catch(undefined),
link: z.string().optional().nullable().catch(undefined),
});
//# sourceMappingURL=base-attributes.js.map

@@ -1,8 +0,6 @@

import type { NullablePartial } from '@blocksuite/global/utils';
import { DisposableGroup, Slot } from '@blocksuite/global/utils';
import type * as Y from 'yjs';
import type { VirgoLine } from './components/index.js';
import { VirgoHookService } from './services/hook.js';
import { VirgoAttributeService, VirgoDeltaService, VirgoEventService, VirgoRangeService } from './services/index.js';
import type { DeltaInsert, TextPoint, VRange, VRangeUpdatedProp } from './types.js';
import type { DeltaInsert, VRange, VRangeUpdatedProp } from './types.js';
import { type BaseTextAttributes, nativePointToTextPoint, textPointToDomPoint } from './utils/index.js';

@@ -61,2 +59,6 @@ import { getTextNodesFromElement } from './utils/text.js';

getVRange: () => VRange | null;
getVRangeFromElement: (element: Element) => VRange | null;
getNativeSelection: () => Selection | null;
getTextPoint: (rangeIndex: number) => import("./types.js").TextPoint;
getLine: (rangeIndex: number) => readonly [import("./index.js").VirgoLine, number];
isVRangeValid: (vRange: VRange | null) => boolean;

@@ -82,5 +84,2 @@ setVRange: (vRange: VRange | null, sync?: boolean) => void;

waitForUpdate(): Promise<void>;
getNativeSelection(): Selection | null;
getTextPoint(rangeIndex: VRange['index']): TextPoint;
getLine(rangeIndex: VRange['index']): readonly [VirgoLine, number];
setReadonly(isReadonly: boolean): void;

@@ -97,3 +96,3 @@ get isReadonly(): boolean;

insertLineBreak(vRange: VRange): void;
formatText(vRange: VRange, attributes: NullablePartial<TextAttributes>, options?: {
formatText(vRange: VRange, attributes: TextAttributes, options?: {
match?: (delta: DeltaInsert, deltaVRange: VRange) => boolean;

@@ -100,0 +99,0 @@ mode?: 'replace' | 'merge';

@@ -6,4 +6,4 @@ import { assertExists, DisposableGroup, Slot } from '@blocksuite/global/utils';

import { VirgoAttributeService, VirgoDeltaService, VirgoEventService, VirgoRangeService, } from './services/index.js';
import { findDocumentOrShadowRoot, nativePointToTextPoint, textPointToDomPoint, } from './utils/index.js';
import { calculateTextLength, getTextNodesFromElement } from './utils/text.js';
import { nativePointToTextPoint, textPointToDomPoint, } from './utils/index.js';
import { getTextNodesFromElement } from './utils/text.js';
import { intersectVRange } from './utils/v-range.js';

@@ -71,2 +71,6 @@ export class VEditor {

this.getVRange = this.rangeService.getVRange;
this.getVRangeFromElement = this.rangeService.getVRangeFromElement;
this.getNativeSelection = this.rangeService.getNativeSelection;
this.getTextPoint = this.rangeService.getTextPoint;
this.getLine = this.rangeService.getLine;
this.isVRangeValid = this.rangeService.isVRangeValid;

@@ -142,47 +146,2 @@ this.setVRange = this.rangeService.setVRange;

}
getNativeSelection() {
const selectionRoot = findDocumentOrShadowRoot(this);
const selection = selectionRoot.getSelection();
if (!selection)
return null;
if (selection.rangeCount === 0)
return null;
return selection;
}
getTextPoint(rangeIndex) {
assertExists(this._rootElement);
const vLines = Array.from(this._rootElement.querySelectorAll('v-line'));
let index = 0;
for (const vLine of vLines) {
const texts = VEditor.getTextNodesFromElement(vLine);
for (const text of texts) {
if (!text.textContent) {
throw new Error('text element should have textContent');
}
if (index + text.textContent.length >= rangeIndex) {
return [text, rangeIndex - index];
}
index += calculateTextLength(text);
}
index += 1;
}
throw new Error('failed to find leaf');
}
// the number is related to the VirgoLine's textLength
getLine(rangeIndex) {
assertExists(this._rootElement);
const lineElements = Array.from(this._rootElement.querySelectorAll('v-line'));
let index = 0;
for (const lineElement of lineElements) {
if (rangeIndex >= index && rangeIndex <= index + lineElement.textLength) {
return [lineElement, rangeIndex - index];
}
if (rangeIndex === index + lineElement.textLength &&
rangeIndex === this.yText.length) {
return [lineElement, rangeIndex - index];
}
index += lineElement.textLength + 1;
}
throw new Error('failed to find line');
}
setReadonly(isReadonly) {

@@ -246,2 +205,5 @@ this.rootElement.contentEditable = isReadonly ? 'false' : 'true';

.forEach(([_delta, deltaVRange]) => {
const normalizedAttributes = this._attributeService.normalizeAttributes(attributes);
if (!normalizedAttributes)
return;
const targetVRange = intersectVRange(vRange, deltaVRange);

@@ -254,3 +216,3 @@ if (!targetVRange)

this._transact(() => {
this.yText.format(targetVRange.index, targetVRange.length, attributes);
this.yText.format(targetVRange.index, targetVRange.length, normalizedAttributes);
});

@@ -257,0 +219,0 @@ });

{
"name": "@blocksuite/virgo",
"version": "0.0.0-20230924223737-8993bc0f-nightly",
"version": "0.0.0-20230925163612-6d2dbfcc-nightly",
"description": "A micro editor.",

@@ -27,3 +27,3 @@ "type": "module",

"zod": "^3.22.2",
"@blocksuite/global": "0.0.0-20230924223737-8993bc0f-nightly"
"@blocksuite/global": "0.0.0-20230925163612-6d2dbfcc-nightly"
},

@@ -30,0 +30,0 @@ "scripts": {

@@ -100,5 +100,5 @@ import type { z, ZodTypeDef } from 'zod';

// filter out undefined values
Object.entries(attributeResult.data).filter(([_, v]) => v)
Object.entries(attributeResult.data).filter(([_, v]) => v || v === null)
) as TextAttributes;
};
}

@@ -1,3 +0,5 @@

import type { VRange } from '../types.js';
import type { VRangeUpdatedProp } from '../types.js';
import { assertExists } from '@blocksuite/global/utils';
import type { VirgoLine } from '../components/virgo-line.js';
import type { TextPoint, VRange, VRangeUpdatedProp } from '../types.js';
import type { BaseTextAttributes } from '../utils/base-attributes.js';

@@ -9,2 +11,3 @@ import { findDocumentOrShadowRoot } from '../utils/query.js';

} from '../utils/range-conversion.js';
import { calculateTextLength, getTextNodesFromElement } from '../utils/text.js';
import { isMaybeVRangeEqual } from '../utils/v-range.js';

@@ -22,2 +25,6 @@ import type { VEditor } from '../virgo.js';

get rootElement() {
return this.editor.rootElement;
}
onVRangeUpdated = async ([newVRange, sync]: VRangeUpdatedProp) => {

@@ -67,2 +74,11 @@ const eq = isMaybeVRangeEqual(this._vRange, newVRange);

getNativeSelection(): Selection | null {
const selectionRoot = findDocumentOrShadowRoot(this.editor);
const selection = selectionRoot.getSelection();
if (!selection) return null;
if (selection.rangeCount === 0) return null;
return selection;
}
getVRange = (): VRange | null => {

@@ -76,2 +92,62 @@ if (this.vRangeProvider) {

getVRangeFromElement = (element: Element): VRange | null => {
const range = document.createRange();
const text = element.querySelector('[data-virgo-text');
if (!text) {
return null;
}
const textNode = text.childNodes[1];
assertExists(textNode instanceof Text);
range.setStart(textNode, 0);
range.setEnd(textNode, textNode.textContent?.length ?? 0);
const vRange = this.toVRange(range);
return vRange;
};
getTextPoint(rangeIndex: VRange['index']): TextPoint {
const vLines = Array.from(this.rootElement.querySelectorAll('v-line'));
let index = 0;
for (const vLine of vLines) {
const texts = getTextNodesFromElement(vLine);
for (const text of texts) {
if (!text.textContent) {
throw new Error('text element should have textContent');
}
if (index + text.textContent.length >= rangeIndex) {
return [text, rangeIndex - index];
}
index += calculateTextLength(text);
}
index += 1;
}
throw new Error('failed to find leaf');
}
// the number is related to the VirgoLine's textLength
getLine(rangeIndex: VRange['index']): readonly [VirgoLine, number] {
const lineElements = Array.from(
this.rootElement.querySelectorAll('v-line')
);
let index = 0;
for (const lineElement of lineElements) {
if (rangeIndex >= index && rangeIndex <= index + lineElement.textLength) {
return [lineElement, rangeIndex - index] as const;
}
if (
rangeIndex === index + lineElement.textLength &&
rangeIndex === this.editor.yTextLength
) {
return [lineElement, rangeIndex - index] as const;
}
index += lineElement.textLength + 1;
}
throw new Error('failed to find line');
}
isVRangeValid = (vRange: VRange | null): boolean => {

@@ -78,0 +154,0 @@ return !(

import { z } from 'zod';
export const baseTextAttributes = z.object({
bold: z.literal(true).optional().catch(undefined),
italic: z.literal(true).optional().catch(undefined),
underline: z.literal(true).optional().catch(undefined),
strike: z.literal(true).optional().catch(undefined),
code: z.literal(true).optional().catch(undefined),
link: z.string().optional().catch(undefined),
bold: z.literal(true).optional().nullable().catch(undefined),
italic: z.literal(true).optional().nullable().catch(undefined),
underline: z.literal(true).optional().nullable().catch(undefined),
strike: z.literal(true).optional().nullable().catch(undefined),
code: z.literal(true).optional().nullable().catch(undefined),
link: z.string().optional().nullable().catch(undefined),
});
export type BaseTextAttributes = z.infer<typeof baseTextAttributes>;

@@ -1,2 +0,1 @@

import type { NullablePartial } from '@blocksuite/global/utils';
import { assertExists, DisposableGroup, Slot } from '@blocksuite/global/utils';

@@ -6,3 +5,2 @@ import { nothing, render } from 'lit';

import type { VirgoLine } from './components/index.js';
import { VIRGO_ROOT_ATTR } from './consts.js';

@@ -16,15 +14,9 @@ import { VirgoHookService } from './services/hook.js';

} from './services/index.js';
import type {
DeltaInsert,
TextPoint,
VRange,
VRangeUpdatedProp,
} from './types.js';
import type { DeltaInsert, VRange, VRangeUpdatedProp } from './types.js';
import {
type BaseTextAttributes,
findDocumentOrShadowRoot,
nativePointToTextPoint,
textPointToDomPoint,
} from './utils/index.js';
import { calculateTextLength, getTextNodesFromElement } from './utils/text.js';
import { getTextNodesFromElement } from './utils/text.js';
import { intersectVRange } from './utils/v-range.js';

@@ -143,2 +135,6 @@

getVRange = this.rangeService.getVRange;
getVRangeFromElement = this.rangeService.getVRangeFromElement;
getNativeSelection = this.rangeService.getNativeSelection;
getTextPoint = this.rangeService.getTextPoint;
getLine = this.rangeService.getLine;
isVRangeValid = this.rangeService.isVRangeValid;

@@ -234,59 +230,2 @@ setVRange = this.rangeService.setVRange;

getNativeSelection(): Selection | null {
const selectionRoot = findDocumentOrShadowRoot(this);
const selection = selectionRoot.getSelection();
if (!selection) return null;
if (selection.rangeCount === 0) return null;
return selection;
}
getTextPoint(rangeIndex: VRange['index']): TextPoint {
assertExists(this._rootElement);
const vLines = Array.from(this._rootElement.querySelectorAll('v-line'));
let index = 0;
for (const vLine of vLines) {
const texts = VEditor.getTextNodesFromElement(vLine);
for (const text of texts) {
if (!text.textContent) {
throw new Error('text element should have textContent');
}
if (index + text.textContent.length >= rangeIndex) {
return [text, rangeIndex - index];
}
index += calculateTextLength(text);
}
index += 1;
}
throw new Error('failed to find leaf');
}
// the number is related to the VirgoLine's textLength
getLine(rangeIndex: VRange['index']): readonly [VirgoLine, number] {
assertExists(this._rootElement);
const lineElements = Array.from(
this._rootElement.querySelectorAll('v-line')
);
let index = 0;
for (const lineElement of lineElements) {
if (rangeIndex >= index && rangeIndex <= index + lineElement.textLength) {
return [lineElement, rangeIndex - index] as const;
}
if (
rangeIndex === index + lineElement.textLength &&
rangeIndex === this.yText.length
) {
return [lineElement, rangeIndex - index] as const;
}
index += lineElement.textLength + 1;
}
throw new Error('failed to find line');
}
setReadonly(isReadonly: boolean): void {

@@ -361,3 +300,3 @@ this.rootElement.contentEditable = isReadonly ? 'false' : 'true';

vRange: VRange,
attributes: NullablePartial<TextAttributes>,
attributes: TextAttributes,
options: {

@@ -374,4 +313,7 @@ match?: (delta: DeltaInsert, deltaVRange: VRange) => boolean;

.forEach(([_delta, deltaVRange]) => {
const normalizedAttributes =
this._attributeService.normalizeAttributes(attributes);
if (!normalizedAttributes) return;
const targetVRange = intersectVRange(vRange, deltaVRange);
if (!targetVRange) return;

@@ -387,3 +329,3 @@

targetVRange.length,
attributes
normalizedAttributes
);

@@ -390,0 +332,0 @@ });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc