|
1 |
| -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Linq;
|
| 4 | +using System.Text.RegularExpressions; |
| 5 | + |
3 | 6 | namespace Openize.Words.IElements
|
4 | 7 | {
|
5 | 8 | /// <summary>
|
@@ -226,6 +229,111 @@ internal void UpdateText()
|
226 | 229 | {
|
227 | 230 | Text = string.Join("", Runs.Select(run => run.Text));
|
228 | 231 | }
|
| 232 | + |
| 233 | + internal void ReplaceText(string search, string replacement, bool useRegex = false) |
| 234 | + { |
| 235 | + var matches = useRegex |
| 236 | + ? Regex.Matches(this.Text, search) |
| 237 | + : Regex.Matches(this.Text, Regex.Escape(search)); |
| 238 | + |
| 239 | + if (matches.Count == 0) return; |
| 240 | + |
| 241 | + var newRuns = new List<Run>(); |
| 242 | + int globalIndex = 0; |
| 243 | + |
| 244 | + foreach (Match match in matches) |
| 245 | + { |
| 246 | + int matchStart = match.Index; |
| 247 | + int matchEnd = match.Index + match.Length; |
| 248 | + |
| 249 | + // Track through original runs |
| 250 | + int currentIndex = 0; |
| 251 | + foreach (var run in this.Runs) |
| 252 | + { |
| 253 | + var text = run.Text; |
| 254 | + int runStart = currentIndex; |
| 255 | + int runEnd = currentIndex + text.Length; |
| 256 | + |
| 257 | + if (runEnd <= matchStart || runStart >= matchEnd) |
| 258 | + { |
| 259 | + newRuns.Add(CloneRun(run)); |
| 260 | + } |
| 261 | + else |
| 262 | + { |
| 263 | + // Overlaps with the match |
| 264 | + int relativeStart = Math.Max(0, matchStart - currentIndex); |
| 265 | + int relativeEnd = Math.Min(text.Length, matchEnd - currentIndex); |
| 266 | + |
| 267 | + if (relativeStart > 0) |
| 268 | + { |
| 269 | + newRuns.Add(new Run |
| 270 | + { |
| 271 | + Text = text.Substring(0, relativeStart), |
| 272 | + FontFamily = run.FontFamily, |
| 273 | + FontSize = run.FontSize, |
| 274 | + Color = run.Color, |
| 275 | + Bold = run.Bold, |
| 276 | + Italic = run.Italic, |
| 277 | + Underline = run.Underline |
| 278 | + }); |
| 279 | + } |
| 280 | + |
| 281 | + if (runStart <= matchStart && runEnd >= matchEnd) |
| 282 | + { |
| 283 | + newRuns.Add(new Run |
| 284 | + { |
| 285 | + Text = replacement, |
| 286 | + FontFamily = run.FontFamily, |
| 287 | + FontSize = run.FontSize, |
| 288 | + Color = run.Color, |
| 289 | + Bold = run.Bold, |
| 290 | + Italic = run.Italic, |
| 291 | + Underline = run.Underline |
| 292 | + }); |
| 293 | + } |
| 294 | + |
| 295 | + if (relativeEnd < text.Length) |
| 296 | + { |
| 297 | + newRuns.Add(new Run |
| 298 | + { |
| 299 | + Text = text.Substring(relativeEnd), |
| 300 | + FontFamily = run.FontFamily, |
| 301 | + FontSize = run.FontSize, |
| 302 | + Color = run.Color, |
| 303 | + Bold = run.Bold, |
| 304 | + Italic = run.Italic, |
| 305 | + Underline = run.Underline |
| 306 | + }); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + currentIndex += run.Text.Length; |
| 311 | + |
| 312 | + if (currentIndex >= matchEnd) |
| 313 | + break; |
| 314 | + } |
| 315 | + |
| 316 | + // Replace only the first match at a time to avoid index shifting |
| 317 | + break; |
| 318 | + } |
| 319 | + |
| 320 | + this.Runs.Clear(); |
| 321 | + foreach (var r in newRuns) |
| 322 | + { |
| 323 | + this.AddRun(r); |
| 324 | + } |
| 325 | + } |
| 326 | + private static Run CloneRun(Run r) => |
| 327 | + new Run |
| 328 | + { |
| 329 | + Text = r.Text, |
| 330 | + FontFamily = r.FontFamily, |
| 331 | + FontSize = r.FontSize, |
| 332 | + Color = r.Color, |
| 333 | + Bold = r.Bold, |
| 334 | + Italic = r.Italic, |
| 335 | + Underline = r.Underline |
| 336 | + }; |
229 | 337 | }
|
230 | 338 |
|
231 | 339 | /// <summary>
|
|
0 commit comments