|
| 1 | +import random |
| 2 | + |
| 3 | +import idom |
| 4 | + |
| 5 | + |
| 6 | +@idom.component |
| 7 | +def RangeWords(text): |
| 8 | + highlight_range, set_highlight_range = idom.hooks.use_state([]) |
| 9 | + |
| 10 | + def update_range(index): |
| 11 | + if highlight_range == []: |
| 12 | + set_highlight_range([index]) |
| 13 | + elif highlight_range == [index]: |
| 14 | + set_highlight_range([]) |
| 15 | + elif index in [highlight_range[-1], highlight_range[0]]: |
| 16 | + set_highlight_range([index]) |
| 17 | + elif index < highlight_range[0]: |
| 18 | + set_highlight_range(list(range(index, highlight_range[0] + 1))) |
| 19 | + else: |
| 20 | + set_highlight_range(list(range(highlight_range[0], index + 1))) |
| 21 | + |
| 22 | + divs = [ |
| 23 | + idom.html.div( |
| 24 | + { |
| 25 | + "onClick": lambda e, i=i: update_range(i), |
| 26 | + "style": { |
| 27 | + "backgroundColor": "aquamarine" |
| 28 | + if i in highlight_range |
| 29 | + else "white", |
| 30 | + "display": "inline", |
| 31 | + "padding": "5px", |
| 32 | + }, |
| 33 | + }, |
| 34 | + word, |
| 35 | + key=str(random.random()), |
| 36 | + ) |
| 37 | + for i, word in enumerate(text.split()) |
| 38 | + ] |
| 39 | + |
| 40 | + return idom.html.div( |
| 41 | + { |
| 42 | + "style": { |
| 43 | + "width": "95%", |
| 44 | + "display": "flex", |
| 45 | + "flex-wrap": "wrap", |
| 46 | + "margin": "5px", |
| 47 | + } |
| 48 | + }, |
| 49 | + divs, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +@idom.component |
| 54 | +def WorkSentDiv(tfname): |
| 55 | + |
| 56 | + sid, set_sid = idom.hooks.use_state(0) |
| 57 | + |
| 58 | + works = { |
| 59 | + "bubs": [ |
| 60 | + "it was the best of times, it was the blurst of times.", |
| 61 | + "they will break upon this fortress like water on rock.", |
| 62 | + "ah those heady days of yore.", |
| 63 | + "forge ahead, no matter the cost.", |
| 64 | + "bring me the head of alfredo garcia.", |
| 65 | + "there were monsters aboard that ship, and truly we were them.", |
| 66 | + ], |
| 67 | + "zugzug": [ |
| 68 | + "나도너도나도 웰웰웰 아하아하.", |
| 69 | + "섬웨어 오버 더 레인보.", |
| 70 | + "유 캔트 듀 댓 온 테레비즌.", |
| 71 | + "컴퓨터 프로그램잉.", |
| 72 | + "어메이징 테일즈.", |
| 73 | + "대츠 오케이!", |
| 74 | + "데르즈 노 비지니스 라이크 쇼 비지니스", |
| 75 | + ], |
| 76 | + } |
| 77 | + |
| 78 | + def get_stext(tfname, sid): |
| 79 | + if sid < len(works[tfname]): |
| 80 | + return works[tfname][sid] |
| 81 | + else: |
| 82 | + return "no more sentences" |
| 83 | + |
| 84 | + def btn_click(count): |
| 85 | + if sid + count < 0: |
| 86 | + return |
| 87 | + |
| 88 | + set_sid(sid + count) |
| 89 | + |
| 90 | + return idom.html.div( |
| 91 | + idom.html.div(f"sentence id: {sid}"), |
| 92 | + idom.html.button({"onClick": lambda e: btn_click(-1)}, "Prev"), |
| 93 | + idom.html.button({"onClick": lambda e: btn_click(1)}, "Next"), |
| 94 | + RangeWords(get_stext(tfname, sid)), |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +@idom.component |
| 99 | +def DualWorkView(): |
| 100 | + return idom.html.div(WorkSentDiv("bubs"), WorkSentDiv("zugzug")) |
| 101 | + |
| 102 | + |
| 103 | +idom.run(DualWorkView) |
0 commit comments