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: {
type: "array",
items: [
{
type: "number"
type: "object",
required: ["start", "end"],
properties: {
start: {
type: "number",
minimum: 0
},
{
type: "number"
end: {
type: "number",
minimum: 0
}
}
],
minItems: 2,
maxItems: 2
},
postRef: {
type: "object",
@ -12992,17 +12993,18 @@ var recordSchemaDict = {
}
},
textSlice: {
type: "array",
items: [
{
type: "number"
type: "object",
required: ["start", "end"],
properties: {
start: {
type: "number",
minimum: 0
},
{
type: "number"
end: {
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 {
text: string;
entities?: Entity[];
@ -16,6 +15,11 @@ export interface Entity {
value: string;
[k: string]: unknown;
}
export interface TextSlice {
start: number;
end: number;
[k: string]: unknown;
}
export interface PostRef {
uri: 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 {s} from '../../lib/styles'
type TextSlice = [number, number]
type TextSlice = {start: number; end: number}
type Entity = {
index: TextSlice
type: string
@ -53,7 +53,7 @@ export function RichText({
}
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[]) {
@ -61,14 +61,14 @@ function* toSegments(text: string, entities: Entity[]) {
let i = 0
do {
let currEnt = entities[i]
if (cursor < currEnt.index[0]) {
yield text.slice(cursor, currEnt.index[0])
} else if (cursor > currEnt.index[0]) {
if (cursor < currEnt.index.start) {
yield text.slice(cursor, currEnt.index.start)
} else if (cursor > currEnt.index.start) {
i++
continue
}
if (currEnt.index[0] < currEnt.index[1]) {
let subtext = text.slice(currEnt.index[0], currEnt.index[1])
if (currEnt.index.start < currEnt.index.end) {
let subtext = text.slice(currEnt.index.start, currEnt.index.end)
if (
!subtext.trim() ||
stripUsername(subtext) !== stripUsername(currEnt.value)
@ -82,7 +82,7 @@ function* toSegments(text: string, entities: Entity[]) {
}
}
}
cursor = currEnt.index[1]
cursor = currEnt.index.end
i++
} while (i < entities.length)
if (cursor < text.length) {

View File

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