@blocksuite/virgo
Advanced tools
Comparing version 0.5.0-20230308232548-463df59 to 0.5.0-20230309082016-5a8e126
import type { AttributesRenderer } from '../types.js'; | ||
export declare const getDefaultAttributeRenderer: <T extends { | ||
bold?: boolean | undefined; | ||
italic?: boolean | undefined; | ||
underline?: boolean | undefined; | ||
strike?: boolean | undefined; | ||
code?: boolean | undefined; | ||
bold?: true | undefined; | ||
italic?: true | undefined; | ||
underline?: true | undefined; | ||
strike?: true | undefined; | ||
code?: true | undefined; | ||
}>() => AttributesRenderer<T>; | ||
//# sourceMappingURL=attributes-renderer.d.ts.map |
import { z } from 'zod'; | ||
export declare const baseTextAttributes: z.ZodObject<{ | ||
bold: z.ZodOptional<z.ZodBoolean>; | ||
italic: z.ZodOptional<z.ZodBoolean>; | ||
underline: z.ZodOptional<z.ZodBoolean>; | ||
strike: z.ZodOptional<z.ZodBoolean>; | ||
code: z.ZodOptional<z.ZodBoolean>; | ||
bold: z.ZodOptional<z.ZodLiteral<true>>; | ||
italic: z.ZodOptional<z.ZodLiteral<true>>; | ||
underline: z.ZodOptional<z.ZodLiteral<true>>; | ||
strike: z.ZodOptional<z.ZodLiteral<true>>; | ||
code: z.ZodOptional<z.ZodLiteral<true>>; | ||
}, "strip", z.ZodTypeAny, { | ||
bold?: boolean | undefined; | ||
italic?: boolean | undefined; | ||
underline?: boolean | undefined; | ||
strike?: boolean | undefined; | ||
code?: boolean | undefined; | ||
bold?: true | undefined; | ||
italic?: true | undefined; | ||
underline?: true | undefined; | ||
strike?: true | undefined; | ||
code?: true | undefined; | ||
}, { | ||
bold?: boolean | undefined; | ||
italic?: boolean | undefined; | ||
underline?: boolean | undefined; | ||
strike?: boolean | undefined; | ||
code?: boolean | undefined; | ||
bold?: true | undefined; | ||
italic?: true | undefined; | ||
underline?: true | undefined; | ||
strike?: true | undefined; | ||
code?: true | undefined; | ||
}>; | ||
export type BaseTextAttributes = z.infer<typeof baseTextAttributes>; | ||
//# sourceMappingURL=base-attributes.d.ts.map |
import { z } from 'zod'; | ||
export const baseTextAttributes = z.object({ | ||
bold: z.boolean().optional(), | ||
italic: z.boolean().optional(), | ||
underline: z.boolean().optional(), | ||
strike: z.boolean().optional(), | ||
code: z.boolean().optional(), | ||
bold: z.literal(true).optional(), | ||
italic: z.literal(true).optional(), | ||
underline: z.literal(true).optional(), | ||
strike: z.literal(true).optional(), | ||
code: z.literal(true).optional(), | ||
}); | ||
//# sourceMappingURL=base-attributes.js.map |
@@ -32,3 +32,3 @@ import { assertExists, Slot } from '@blocksuite/global/utils'; | ||
text = texts[0]; | ||
textOffset = 0; | ||
textOffset = offset === 0 ? offset : text.length; | ||
} | ||
@@ -49,3 +49,3 @@ } | ||
text = texts[0]; | ||
textOffset = 0; | ||
textOffset = offset === 0 ? offset : text.length; | ||
break; | ||
@@ -60,3 +60,3 @@ } | ||
text = texts[0]; | ||
textOffset = 0; | ||
textOffset = offset === 0 ? offset : text.length; | ||
break; | ||
@@ -324,2 +324,6 @@ } | ||
} | ||
if (range.startContainer.nodeType !== Node.TEXT_NODE || | ||
range.endContainer.nodeType !== Node.TEXT_NODE) { | ||
this.syncVRange(); | ||
} | ||
}; | ||
@@ -472,3 +476,3 @@ this._onUpdateVRange = ([newVRange, origin]) => { | ||
for (const lineElement of lineElements) { | ||
if (rangeIndex >= index && rangeIndex < index + lineElement.textLength) { | ||
if (rangeIndex >= index && rangeIndex <= index + lineElement.textLength) { | ||
return [lineElement, rangeIndex - index]; | ||
@@ -503,17 +507,26 @@ } | ||
getFormat(vRange) { | ||
const deltas = this.getDeltasByVRange(vRange); | ||
const result = {}; | ||
for (const [delta] of deltas) { | ||
if (delta.attributes) { | ||
for (const [key, value] of Object.entries(delta.attributes)) { | ||
if (typeof value === 'boolean' && !value) { | ||
delete result[key]; | ||
} | ||
else { | ||
result[key] = value; | ||
} | ||
const deltas = this.getDeltasByVRange(vRange).filter(([delta, position]) => position.index + position.length > vRange.index && | ||
position.index <= vRange.index + vRange.length); | ||
const maybeAttributesArray = deltas.map(([delta]) => delta.attributes); | ||
if (!maybeAttributesArray.length || | ||
// some text does not have any attributes | ||
maybeAttributesArray.some(attributes => !attributes)) { | ||
return {}; | ||
} | ||
const attributesArray = maybeAttributesArray; | ||
return attributesArray.reduce((acc, cur) => { | ||
const newFormat = {}; | ||
for (const key in acc) { | ||
const typedKey = key; | ||
// If the given range contains multiple different formats | ||
// such as links with different values, | ||
// we will treat it as having no format | ||
if (acc[typedKey] === cur[typedKey]) { | ||
// This cast is secure because we have checked that the value of the key is the same. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
newFormat[typedKey] = acc[typedKey]; | ||
} | ||
} | ||
} | ||
return result; | ||
return newFormat; | ||
}); | ||
} | ||
@@ -520,0 +533,0 @@ setReadonly(isReadonly) { |
{ | ||
"name": "@blocksuite/virgo", | ||
"version": "0.5.0-20230308232548-463df59", | ||
"version": "0.5.0-20230309082016-5a8e126", | ||
"description": "A micro editor.", | ||
@@ -26,3 +26,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@blocksuite/global": "0.5.0-20230308232548-463df59", | ||
"@blocksuite/global": "0.5.0-20230309082016-5a8e126", | ||
"zod": "^3.20.6" | ||
@@ -29,0 +29,0 @@ }, |
import { z } from 'zod'; | ||
export const baseTextAttributes = z.object({ | ||
bold: z.boolean().optional(), | ||
italic: z.boolean().optional(), | ||
underline: z.boolean().optional(), | ||
strike: z.boolean().optional(), | ||
code: z.boolean().optional(), | ||
bold: z.literal(true).optional(), | ||
italic: z.literal(true).optional(), | ||
underline: z.literal(true).optional(), | ||
strike: z.literal(true).optional(), | ||
code: z.literal(true).optional(), | ||
}); | ||
export type BaseTextAttributes = z.infer<typeof baseTextAttributes>; |
@@ -62,3 +62,3 @@ import { assertExists, Slot } from '@blocksuite/global/utils'; | ||
text = texts[0]; | ||
textOffset = 0; | ||
textOffset = offset === 0 ? offset : text.length; | ||
} | ||
@@ -79,3 +79,3 @@ } else { | ||
text = texts[0]; | ||
textOffset = 0; | ||
textOffset = offset === 0 ? offset : text.length; | ||
break; | ||
@@ -92,3 +92,3 @@ } | ||
text = texts[0]; | ||
textOffset = 0; | ||
textOffset = offset === 0 ? offset : text.length; | ||
break; | ||
@@ -444,3 +444,3 @@ } else if ( | ||
for (const lineElement of lineElements) { | ||
if (rangeIndex >= index && rangeIndex < index + lineElement.textLength) { | ||
if (rangeIndex >= index && rangeIndex <= index + lineElement.textLength) { | ||
return [lineElement, rangeIndex - index] as const; | ||
@@ -485,20 +485,31 @@ } | ||
getFormat(vRange: VRange): TextAttributes { | ||
const deltas = this.getDeltasByVRange(vRange); | ||
const result: { | ||
[key: string]: unknown; | ||
} = {}; | ||
for (const [delta] of deltas) { | ||
if (delta.attributes) { | ||
for (const [key, value] of Object.entries(delta.attributes)) { | ||
if (typeof value === 'boolean' && !value) { | ||
delete result[key]; | ||
} else { | ||
result[key] = value; | ||
} | ||
const deltas = this.getDeltasByVRange(vRange).filter( | ||
([delta, position]) => | ||
position.index + position.length > vRange.index && | ||
position.index <= vRange.index + vRange.length | ||
); | ||
const maybeAttributesArray = deltas.map(([delta]) => delta.attributes); | ||
if ( | ||
!maybeAttributesArray.length || | ||
// some text does not have any attributes | ||
maybeAttributesArray.some(attributes => !attributes) | ||
) { | ||
return {} as TextAttributes; | ||
} | ||
const attributesArray = maybeAttributesArray as TextAttributes[]; | ||
return attributesArray.reduce((acc, cur) => { | ||
const newFormat = {} as TextAttributes; | ||
for (const key in acc) { | ||
const typedKey = key as keyof TextAttributes; | ||
// If the given range contains multiple different formats | ||
// such as links with different values, | ||
// we will treat it as having no format | ||
if (acc[typedKey] === cur[typedKey]) { | ||
// This cast is secure because we have checked that the value of the key is the same. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
newFormat[typedKey] = acc[typedKey] as any; | ||
} | ||
} | ||
} | ||
return result as TextAttributes; | ||
return newFormat; | ||
}); | ||
} | ||
@@ -983,2 +994,9 @@ | ||
} | ||
if ( | ||
range.startContainer.nodeType !== Node.TEXT_NODE || | ||
range.endContainer.nodeType !== Node.TEXT_NODE | ||
) { | ||
this.syncVRange(); | ||
} | ||
}; | ||
@@ -985,0 +1003,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
358064
4805
+ Added@blocksuite/global@0.5.0-20230309082016-5a8e126(transitive)
- Removed@blocksuite/global@0.5.0-20230308232548-463df59(transitive)