Diffdown is a real-time collaborative Markdown editor/previewer built on the AT Protocol diffdown.com

refactor: extract applyInternal helper, add Insert nil check

+15 -23
+15 -23
internal/collaboration/ot.go
··· 28 28 ot.mu.Lock() 29 29 defer ot.mu.Unlock() 30 30 31 - if op.From < 0 { 32 - op.From = 0 33 - } 34 - if op.To < 0 { 35 - op.To = 0 36 - } 37 - if op.From > len(ot.documentText) { 38 - op.From = len(ot.documentText) 39 - } 40 - if op.To > len(ot.documentText) { 41 - op.To = len(ot.documentText) 42 - } 43 - if op.From > op.To { 44 - return ot.documentText 45 - } 46 - 47 - newText := ot.documentText[:op.From] + op.Insert + ot.documentText[op.To:] 48 - ot.documentText = newText 49 - ot.version++ 50 - 51 - return ot.documentText 31 + newText, _ := ot.applyInternal(op) 32 + return newText 52 33 } 53 34 54 35 func (ot *OTEngine) ApplyWithVersion(op Operation) (string, int) { 55 36 ot.mu.Lock() 56 37 defer ot.mu.Unlock() 57 38 39 + newText, changed := ot.applyInternal(op) 40 + if !changed { 41 + return newText, ot.version 42 + } 43 + return newText, ot.version 44 + } 45 + 46 + func (ot *OTEngine) applyInternal(op Operation) (string, bool) { 47 + if op.Insert == "" { 48 + return ot.documentText, false 49 + } 58 50 if op.From < 0 { 59 51 op.From = 0 60 52 } ··· 68 60 op.To = len(ot.documentText) 69 61 } 70 62 if op.From > op.To { 71 - return ot.documentText, ot.version 63 + return ot.documentText, false 72 64 } 73 65 74 66 newText := ot.documentText[:op.From] + op.Insert + ot.documentText[op.To:] 75 67 ot.documentText = newText 76 68 ot.version++ 77 69 78 - return ot.documentText, ot.version 70 + return newText, true 79 71 } 80 72 81 73 func (ot *OTEngine) GetText() string {