简体   繁体   English

Elm:如何在Enter按下Chrome时在内容可编辑的div中添加br标签?

[英]Elm: How to add a br tag in a contenteditable div in Chrome on enter press?

Chrome seems to add a div when enter key is pressed in a contenteditable . 当在contenteditable按下Enter键时,Chrome似乎添加了一个div I'd like a br to always be used. 我想一直使用br There are plenty of js examples on SO (and the interwebs) but I'm new to Elm and there isn't that much information on how to do this. 在SO(和互连网)上有很多js示例,但是我对Elm并不陌生,关于如何执行此操作的信息也不多。

I have come across this code on github: 我在github上遇到了这段代码:

onKeyPress : (Int -> msg) -> Attribute msg
onKeyPress tagger =
    on "keypress" (Decode.map tagger keyCode)

but am not grokking how to use the function. 但并不在意如何使用该功能。 My view would look something like this: 我的观点看起来像这样:

view : Model -> Html Message
view model = div [ contentEditable True, onKeyPress SomeMessage] []

Message type would look like this: 消息类型如下所示:

type Message
= None
| SomeMessage Int

And the update would be something like: 更新将是这样的:

update : Message -> Model -> ( Model, Cmd Message )
update message model =
    case message of
        SomeMessage (Ok isThisTheKeyCode) ->
            ( model, Cmd.none )

        SomeMessage (Err err) ->
            ( model, Cmd.none )

There are examples with input fields but I think those are different. 有一些带有输入字段的示例,但我认为它们是不同的。

Any advice or pointing me in the right direction or filling in the gaps would be greatly appreciated. 任何建议,正确的方向或填补空白。 Thank you 谢谢

Update: To be clear, I can see how this is done with JavaScript. 更新:明确地说,我可以看到这是如何使用JavaScript完成的。 I'm asking for assistance or direction for the Elm implementation. 我正在寻求Elm实施的帮助或方向。 Thank you 谢谢

Update 2: I think I'm in the right direction. 更新2:我认为我的方向正确。 My implementation actually had a div with another contentEditable inside it. 我的实现实际上有一个div,其中包含另一个contentEditable。 The inner div was the one I was trying to get to update. 内部div是我要更新的那个。

div [ contentEditable True][
  div [contentEditable True]
  []
]

I removed the inner contentEditable so now looks like 我删除了内部contentEditable,所以现在看起来像

div [ contentEditable True][
  div [] []
]

Still trying to figure out how to substitute br for div in Chrome 仍在尝试找出如何在Chrome中用br代替div

You can change default paragraph separator by by referring this . 您可以通过参考this更改默认的段落分隔符。 But what you are trying to achieve is possible with Shift + Enter combination. 但是,您想要实现的目标可以通过Shift + Enter组合来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM