-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentItem.cs
136 lines (106 loc) · 3.87 KB
/
ContentItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using VkCore.Dtos;
using VkCore.Models.Word;
using VkCore.SharedKernel;
namespace VkCore.Models.ReadingModel
{
public class ContentItem : BaseEntity
{
private int _firstIndex;
private string _value;
public string ReadingId { get; set; }
public ContentItem(
int bodyIndex,
string value,
string wordId = null,
string annotationId = null,
string annotationContextId = null
)
{
//if (string.IsNullOrWhiteSpace(value))
// throw new ArgumentException("A content item cannot contain an empty value.");
Id = Guid.NewGuid().ToString();
BodyIndex = bodyIndex;
Value = value;
WordId = wordId;
AnnotationId = annotationId;
AnnotationContextId = annotationContextId;
}
public int BodyIndex { get; set; }
// Example:
// hello
// 01234
// StartIndex = 0
// EndIndex = 5
public int FirstIndex => _firstIndex;
public int LastIndex => _firstIndex + Value.Length - 1;
public string WordId { get; set; }
public WordEntry Word { get; set; }
public string AnnotationId { get; set; }
public Annotation Annotation { get; set; }
public string AnnotationContextId { get; set; }
public string Value
{
get { return _value.Replace("\r\n", "\n"); }
set { _value = value; }
}
public bool IsDefined()
{
return this.WordId != null;
}
public void SetFirstIndex(int firstIndex)
{
_firstIndex = firstIndex;
}
public int GetLocalIndex(int globalIndex)
{
return globalIndex == 0 ? 0 : globalIndex - FirstIndex;
}
public int GetGlobalIndex(int localIndex)
{
return localIndex + _firstIndex;
}
public void DeleteFromValue(int startIndex, int endIndex)
{
int localStartIndex = GetLocalIndex(startIndex);
int localEndIndex = GetLocalIndex(endIndex);
string left = "";
string right = "";
if (localStartIndex != 0)
left = Value.Substring(0, localStartIndex);
if (localEndIndex != Value.Length - 1)
right = Value.Substring(localEndIndex + 1, Value.Length - localEndIndex - 1);
RemoveDefinition();
Value = left + right;
}
public List<ContentItem> SplitOutNewValue(int insertIndex, string newValue)
{
var list = new List<ContentItem>();
var localInsertIndex = GetLocalIndex(insertIndex);
// split this value at insert index and update this value to first half
var newSecondHalf = this.Value.Substring(localInsertIndex);
this.Value = this.Value.Remove(localInsertIndex);
// create a new content item at the end of the first half and the length of the new value
list.Add(new ContentItem(this.BodyIndex + 1, newValue));
// create a new content item with the value of the second half
list.Add(new ContentItem(this.BodyIndex + 2, newSecondHalf));
RemoveDefinition();
return list;
}
public List<ContentItem> InsertValue(int insertIndex, string newValue)
{
var list = new List<ContentItem>();
var localInsertIndex = GetLocalIndex(insertIndex);
this.Value = this.Value.Insert(localInsertIndex, newValue);
RemoveDefinition();
return list;
}
public void RemoveDefinition()
{
this.AnnotationId = null;
this.WordId = null;
this.AnnotationContextId = null;
}
}
}