Skip to content

Commit

Permalink
Set contenteditable=plaintext-only directly via an attribute
Browse files Browse the repository at this point in the history
So that it isn't enabled when editable is false.

FIX: Fix a regression where `EditorView.editable.of(false)` didn't disable
editing on Webkit-based browsers.

Issue codemirror/dev#526
  • Loading branch information
marijnh committed Jul 12, 2021
1 parent 3a45e00 commit e3cdb43
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,16 @@ export function dispatchKey(elt: HTMLElement, name: string, code: number): boole
elt.dispatchEvent(up)
return down.defaultPrevented || up.defaultPrevented
}

let _plainTextSupported: boolean | null = null
export function contentEditablePlainTextSupported() {
if (_plainTextSupported == null) {
_plainTextSupported = false
let dummy = document.createElement("div")
try {
dummy.contentEditable = "plaintext-only"
_plainTextSupported = dummy.contentEditable == "plaintext-only"
} catch(_) {}
}
return _plainTextSupported
}
4 changes: 2 additions & 2 deletions src/editorview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {StyleModule, StyleSpec} from "style-mod"
import {DocView} from "./docview"
import {ContentView} from "./contentview"
import {InputState} from "./input"
import {Rect, focusPreventScroll, flattenRect} from "./dom"
import {Rect, focusPreventScroll, flattenRect, contentEditablePlainTextSupported} from "./dom"
import {posAtCoords, moveByChar, moveToLineBoundary, byGroup, moveVertically, skipAtoms} from "./cursor"
import {BlockInfo} from "./heightmap"
import {ViewState} from "./viewstate"
Expand Down Expand Up @@ -355,7 +355,7 @@ export class EditorView {
spellcheck: "false",
autocorrect: "off",
autocapitalize: "off",
contenteditable: String(this.state.facet(editable)),
contenteditable: !this.state.facet(editable) ? "false" : contentEditablePlainTextSupported() ? "plaintext-only" : "true",
class: "cm-content",
style: `${browser.tabSize}: ${this.state.tabSize}`,
role: "textbox",
Expand Down
1 change: 0 additions & 1 deletion src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export const baseTheme = buildTheme("." + baseThemeID, {
display: "block",
whiteSpace: "pre",
wordWrap: "normal", // https://github.com/codemirror/codemirror.next/issues/456
WebkitUserModify: "read-write-plaintext-only",
boxSizing: "border-box",

padding: "4px 0",
Expand Down

0 comments on commit e3cdb43

Please sign in to comment.