test
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

works! (hours ago)

12Me21 a53f1fd3 f0942f9e

+108 -145
+100 -130
facet.js
··· 89 89 return spans 90 90 } 91 91 92 - export function make_windows(spans) { 93 - // at begin, push its type to stack 94 - // at end, pop 95 - // make list of begins and ends 96 - // sort 97 - // iterate 98 - let edges = [] 99 - for (let x of spans) { 100 - edges.push([x.start, x]) 101 - edges.push([x.end, x]) 102 - } 103 - edges.sort(([a],[b])=>a-b) 104 - let open = new Set() 105 - let windows = [] 106 - let last = 0 107 - for (let [index,span] of edges) { 108 - if (index > last && open.size) { 109 - windows.push({ 110 - start: last, 111 - end: index, 112 - spans: [...open], 113 - }) 114 - } 115 - open.delete(span) || open.add(span) // toggle 116 - last = index 117 - } 118 - return windows 119 - } 120 - // note "group" is perpendicular to "span" (need better name) 121 - 122 92 function index_to_byteindex(string, index) { 123 93 return unescape(encodeURIComponent(string.slice(0,index))).length 124 94 } 125 95 126 - export function propagate_strip(windows) { 127 - for (let win of windows) { 128 - win.spans = win.spans.map(span=>{ 129 - let length = win.end-win.start 130 - function clamp(x) { 131 - if (x<0) 132 - return 0 133 - if (x>length) 134 - return length 135 - return x 136 - } 137 - let inset_left = win.start - span.start 138 - let inset_right = span.end - win.end 139 - let strip_left = clamp(span.strip[0] - inset_left) 140 - let strip_right = clamp(span.strip[1] - inset_right) 141 - let span2 = { 142 - feature: span.feature, 143 - strip: [strip_left, strip_right], 144 - } 145 - return span2 146 - }) 147 - } 148 - } 149 - 150 96 function has_client_strip(feature) { 151 97 return feature.$type=="com.example.richtext.facet#markup" 152 98 } 153 99 100 + // todo: exclusion like "dont allow anything overlapping this one" -> delete all reflections of a span if anything overlaps one of its reflections, or vice versa. 154 101 155 - export function apply_strips(obj) { 156 - let text = obj.text 157 - let shift = 0 158 - function cut(index, amount) { 159 - console.log(cut, index, amount) 160 - text = text.slice(0, index) + text.slice(index+amount) 161 - shift += amount 102 + export class MarkedText { 103 + constructor(text) { 104 + this.text = text 105 + this.spans = [] 106 + this.windows = null 162 107 } 163 - 164 - for (let i=0; i<obj.windows.length; i++) { 165 - let win = obj.windows[i] 166 - let lstrip = 0 167 - let rstrip = 0 168 - // find the max strip we have to remove 169 - for (let span of win.spans) { 170 - if (!has_client_strip(span.feature)) { 171 - lstrip = Math.max(lstrip, span.strip[0]) 172 - rstrip = Math.max(rstrip, span.strip[1]) 173 - } 108 + make_windows() { 109 + let edges = [] 110 + for (let span of this.spans) { 111 + edges.push([span.start, span]) 112 + edges.push([span.end, span]) 113 + } 114 + edges.sort(([a],[b])=>a-b) 115 + let open = new Set() 116 + this.windows = [] 117 + let last = 0 118 + for (let [index,span] of edges) { 119 + if (index > last && open.size) 120 + this.windows.push({start: last, end: index, spans: [...open]}) 121 + open.delete(span) || open.add(span) // toggle 122 + last = index 174 123 } 175 - let len = win.end-win.start 176 - win.start -= shift 177 - win.end -= shift 178 - if (lstrip+rstrip > len) { 179 - // invalid 180 - throw new Error("heck darn") 181 - } else if (lstrip+rstrip == len) { 182 - // cut out the entire thing 183 - cut(win.start, len) 184 - obj.windows.splice(i, 1) 185 - i-- 186 - } else { 187 - if (lstrip) { 188 - cut(win.start, lstrip) 189 - win.end -= lstrip 190 - } 191 - if (rstrip) { 192 - cut(win.end-rstrip, rstrip) 193 - win.end -= rstrip 194 - } 195 - 196 - // decrease strip amounts 124 + // propagate strips 125 + for (let win of this.windows) { 126 + win.spans = win.spans.map(span=>{ 127 + let length = win.end-win.start 128 + function clamp(x) { 129 + if (x<0) 130 + return 0 131 + if (x>length) 132 + return length 133 + return x 134 + } 135 + let inset_left = win.start - span.start 136 + let inset_right = span.end - win.end 137 + let strip_left = clamp(span.strip[0] - inset_left) 138 + let strip_right = clamp(span.strip[1] - inset_right) 139 + let span2 = { 140 + feature: span.feature, 141 + strip: [strip_left, strip_right], 142 + } 143 + return span2 144 + }) 145 + } 146 + } 147 + create_facets() { 148 + let facets = [] 149 + 150 + let text = this.text 151 + let shift = 0 152 + function cut(index, amount) { 153 + text = text.slice(0, index) + text.slice(index+amount) 154 + shift += amount 155 + } 156 + 157 + for (let win of this.windows) { 158 + // PRE-APPLY STRIP to facets which don't support that: 159 + let lstrip = 0 160 + let rstrip = 0 161 + // find the max strip we have to remove 197 162 for (let span of win.spans) { 198 - span.strip[0] = Math.max(0, span.strip[0]-lstrip) 199 - span.strip[1] = Math.max(0, span.strip[1]-rstrip) 163 + if (!has_client_strip(span.feature)) { 164 + lstrip = Math.max(lstrip, span.strip[0]) 165 + rstrip = Math.max(rstrip, span.strip[1]) 166 + } 167 + } 168 + let start = win.start-shift 169 + let end = win.end-shift 170 + let len = end-start 171 + if (lstrip+rstrip > len) { 172 + // invalid 173 + throw new Error("heck darn") 174 + } else if (lstrip+rstrip == len) { 175 + // cut out the entire thing 176 + cut(start, len) 177 + } else { 178 + if (lstrip) { 179 + cut(start, lstrip) 180 + end -= lstrip 181 + } 182 + if (rstrip) { 183 + cut(end-rstrip, rstrip) 184 + end -= rstrip 185 + } 186 + // now push the facet 187 + facets.push({ 188 + index: { 189 + byteStart: index_to_byteindex(text, start), 190 + byteEnd: index_to_byteindex(text, end), 191 + }, 192 + features: win.spans.map(span=>{ 193 + let feature = {...span.feature} 194 + if (has_client_strip(feature)) { 195 + feature.strip = [ 196 + Math.max(0, span.strip[0]-lstrip), 197 + Math.max(0, span.strip[1]-rstrip), 198 + ] 199 + } 200 + return feature 201 + }), 202 + }) 200 203 } 201 204 } 205 + return {text, facets} 202 206 } 203 - obj.text = text 204 207 } 205 - 206 - export function finalize_facets(text, windows) { 207 - let facets = windows.map(x=>({ 208 - index: { 209 - byteStart:index_to_byteindex(text, x.start), 210 - byteEnd:index_to_byteindex(text, x.end), 211 - }, 212 - features: x.spans.map(x=>{ 213 - let x2 = {...x.feature} 214 - if (has_client_strip(x2)) { 215 - x2.strip = x.strip 216 - } 217 - return x2 218 - }), 219 - })) 220 - return facets 221 - } 222 - 223 - /*spans = [ 224 - {type:"abc", start: 1, end: 5}, 225 - {type:"def", start: 3, end: 5}, 226 - {type:"zzz", start: 6, end: 10}, 227 - ] 228 - 229 - .aaaa..... 230 - ...dd.zzzz 231 - 232 - output = [ 233 - {start: 1, end: 3, types: ["abc"]}, 234 - {start: 3, end: 5, types: ["abc","def"]}, 235 - {start: 6, end: 10, types: ["zzz"]}, 236 - ] 237 - */
+8 -15
test3.html
··· 1 1 <!doctype html> 2 2 3 3 <script type=module> 4 - import {apply_strips,make_windows,markup_italic,markup1,finalize_facets,propagate_strip} from './facet.js' 5 - 6 - let text = "abc /123 #test/ http://example.com[abc]" 4 + import {MarkedText,markup_italic,markup1} from './facet.js' 7 5 8 - let spans = [ 9 - ...markup_italic(text), 10 - ...markup1.text_to_spans(text), 6 + let text = new MarkedText("abc /123 #test/ http://example.com") 7 + text.spans = [ 8 + ...markup_italic(text.text), 9 + ...markup1.text_to_spans(text.text), 11 10 ] 12 - // todo: exclusion like "dont allow anything overlapping this one" -> delete all reflections of a span if anything overlaps one of its reflections, or vice versa. 13 - let windows = make_windows(spans) 14 - propagate_strip(windows) 15 - console.log(windows) 16 11 17 - let x = {text, windows} 18 - apply_strips(x) 19 - 20 - let facets = finalize_facets(x.text, x.windows) 21 - console.log(x.text, facets) 12 + text.make_windows() 13 + let f = text.create_facets() 14 + console.log(f) 22 15 </script>