Update to new textslice

zio/stable
Paul Frazee 2022-11-04 17:56:48 -05:00
parent f333a90fab
commit 3f730f1173
6 changed files with 40 additions and 34 deletions

View File

@ -12936,17 +12936,18 @@ var recordSchemaDict = {
} }
}, },
textSlice: { textSlice: {
type: "array", type: "object",
items: [ required: ["start", "end"],
{ properties: {
type: "number" start: {
type: "number",
minimum: 0
}, },
{ end: {
type: "number" type: "number",
minimum: 0
} }
], }
minItems: 2,
maxItems: 2
}, },
postRef: { postRef: {
type: "object", type: "object",
@ -12992,17 +12993,18 @@ var recordSchemaDict = {
} }
}, },
textSlice: { textSlice: {
type: "array", type: "object",
items: [ required: ["start", "end"],
{ properties: {
type: "number" start: {
type: "number",
minimum: 0
}, },
{ end: {
type: "number" type: "number",
minimum: 0
} }
], }
minItems: 2,
maxItems: 2
} }
} }
}, },

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,3 @@
export declare type TextSlice = [number, number];
export interface Record { export interface Record {
text: string; text: string;
entities?: Entity[]; entities?: Entity[];
@ -16,6 +15,11 @@ export interface Entity {
value: string; value: string;
[k: string]: unknown; [k: string]: unknown;
} }
export interface TextSlice {
start: number;
end: number;
[k: string]: unknown;
}
export interface PostRef { export interface PostRef {
uri: string; uri: string;
cid: string; cid: string;

File diff suppressed because one or more lines are too long

View File

@ -3,7 +3,7 @@ import {Text, TextStyle, StyleProp} from 'react-native'
import {Link} from './Link' import {Link} from './Link'
import {s} from '../../lib/styles' import {s} from '../../lib/styles'
type TextSlice = [number, number] type TextSlice = {start: number; end: number}
type Entity = { type Entity = {
index: TextSlice index: TextSlice
type: string type: string
@ -53,7 +53,7 @@ export function RichText({
} }
function sortByIndex(a: Entity, b: Entity) { function sortByIndex(a: Entity, b: Entity) {
return a.index[0] - b.index[0] return a.index.start - b.index.start
} }
function* toSegments(text: string, entities: Entity[]) { function* toSegments(text: string, entities: Entity[]) {
@ -61,14 +61,14 @@ function* toSegments(text: string, entities: Entity[]) {
let i = 0 let i = 0
do { do {
let currEnt = entities[i] let currEnt = entities[i]
if (cursor < currEnt.index[0]) { if (cursor < currEnt.index.start) {
yield text.slice(cursor, currEnt.index[0]) yield text.slice(cursor, currEnt.index.start)
} else if (cursor > currEnt.index[0]) { } else if (cursor > currEnt.index.start) {
i++ i++
continue continue
} }
if (currEnt.index[0] < currEnt.index[1]) { if (currEnt.index.start < currEnt.index.end) {
let subtext = text.slice(currEnt.index[0], currEnt.index[1]) let subtext = text.slice(currEnt.index.start, currEnt.index.end)
if ( if (
!subtext.trim() || !subtext.trim() ||
stripUsername(subtext) !== stripUsername(currEnt.value) stripUsername(subtext) !== stripUsername(currEnt.value)
@ -82,7 +82,7 @@ function* toSegments(text: string, entities: Entity[]) {
} }
} }
} }
cursor = currEnt.index[1] cursor = currEnt.index.end
i++ i++
} while (i < entities.length) } while (i < entities.length)
if (cursor < text.length) { if (cursor < text.length) {

View File

@ -63,10 +63,10 @@ export function extractEntities(text: string): Entity[] | undefined {
ents.push({ ents.push({
type: 'mention', type: 'mention',
value: match[3], value: match[3],
index: [ index: {
match.indices[2][0], // skip the (^|\s) but include the '@' start: match.indices[2][0], // skip the (^|\s) but include the '@'
match.indices[3][1], end: match.indices[3][1],
], },
}) })
} }
return ents.length > 0 ? ents : undefined return ents.length > 0 ? ents : undefined