LoginSignup
0
0

More than 5 years have passed since last update.

riot.js contenteditable

Last updated at Posted at 2018-04-11

contenteditable は valueのようにはいかない。ハウリングしてしまう。

dummyをriotにwatchして貰って、それを更新する。更新を回避する事もできるが、each要素をストアデータとして利用したい場合には、やはりdummyに保存しておいたほうが良い。

<li each="{item in items}" oninput={input} contenteditable="plaintext-only" dummy={item.text} ref='ed'></li>

ただ、this.one mountでelement.textContentに初期値を入れ込む処理が入る。

 this.one('mount',function(){
  this.refs.ed.forEach((el,i)=>{ el.textContent=this.items[i].text; })
 })

全部

左はテキストデータ、右はテキストデータのアナライズ

<script src="//gnjo.github.io/riot.js"></script>
<loop></loop>
<style>
 body{margin:0;width:100vw;height:100vh;}
.f{
 display:flex;flex-direction:row;
 width:80%;height:100%;justify-content:center;
 margin:auto;
}
.f>ul{margin:0;padding:0;width:100%;height:100%;}
.f>ul li{
 list-style:none;
 min-height:1rem;min-width:100%;
 margin:0.1rem;
}
.left li{background-color:skyblue}
.right li{background-color:pink}
</style> 
let fn={};
fn.jpTime=(timestamp=Date.now())=>{
 return new Date(timestamp+1000*60*60*9)
  .toISOString().replace(/-/g,'/').replace('T',' ').slice(0,'YYYY/MM/DD hh:mm:ss'.length)
} 
fn.lex=(d)=>{ return {head:d.match(/#{1,6} .+/),count:d.length,time:fn.jpTime(),text:d} }
;
riot.tag(
`loop`
,`<div class="f">
<ul class="left"><li each="{item in items}" oninput={input} contenteditable="plaintext-only" dummy={item.text} ref='ed'></li> </ul>
<ul class="right"> <li each="{item in items}">{item.count}:{item.head} :{item.time}</li> </ul>
</div>
`
,function(opts){
 this.items =['# xyz','# xxx','# yyy'].map(d=>fn.lex(d))
 this.one('mount',function(){ this.refs.ed.forEach((el,i)=>{ el.textContent=this.items[i].text; }) })
 this.input=function(ev){ Object.assign(this.item,fn.lex(ev.target.textContent)) }
})
riot.mount('loop',{})
;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0