Skip to content

Commit

Permalink
Add optional support for showing typographic characters
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Jul 5, 2024
1 parent 755a230 commit 6316b41
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Next version

- Updated all dependencies.
- Stopped putting anything into the global scope in ``init.js``.
- Added optional support for showing typographic characters.


0.4 (2024-05-26)
Expand Down
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ It's possible to slightly customize the field or widget by passing an optional
.. code-block:: python
config = {
"types": None, # Allow all nodes and marks
"history": True, # Enable undo and redo
"html": True, # Add a button which allows editing the raw HTML
"types": None, # Allow all nodes and marks
"history": True, # Enable undo and redo
"html": True, # Add a button which allows editing the raw HTML
"typographic": False, # Do not highlight typographic characters
}
If you only want to support paragraphs, strong, emphasis, sub- and superset and
Expand Down
2 changes: 1 addition & 1 deletion django_prose_editor/static/django_prose_editor/editor.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions django_prose_editor/static/django_prose_editor/editor.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions django_prose_editor/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def get_context(self, name, value, attrs):
"types": None,
"history": True,
"html": True,
"typograpic": False,
},
separators=(",", ":"),
)
Expand Down
4 changes: 4 additions & 0 deletions src/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@
label:empty:has(+ .prose-editor) {
display: none;
}

.prose-editor-nbsp {
background: lightblue;
}
4 changes: 3 additions & 1 deletion src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
htmlMenuItem,
} from "./menu.js"
import { noSpellCheck } from "./nospellcheck.js"
import { typographicPlugin } from "./typographic.js"
import { crel, createDebouncedBackWriter, parseHTML } from "./utils.js"

const underlineDOM = ["u", 0]
Expand Down Expand Up @@ -118,7 +119,8 @@ export function createEditor(textarea, config) {
].filter(Boolean),
),
noSpellCheck(),
]
config.typograpic ? typographicPlugin : null,
].filter(Boolean)

const editor = crel("div", { className: "prose-editor" })
textarea.before(editor)
Expand Down
39 changes: 39 additions & 0 deletions src/typographic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Plugin which shows typographic characters (currently only non-breaking spaces)

import { Plugin } from "prosemirror-state"
import { Decoration, DecorationSet } from "prosemirror-view"

const typographicDecorations = (doc) => {
const decorations = []
doc.descendants((node, position) => {
if (node.text) {
for (const match of node.text.matchAll(/\u00A0/g)) {
const from = position + (match.index || 0)
decorations.push(
Decoration.inline(from, from + 1, {
class: "prose-editor-nbsp",
}),
)
}
}
})
return DecorationSet.create(doc, decorations)
}

export const typographicPlugin = new Plugin({
state: {
init(_, { doc }) {
return typographicDecorations(doc)
},
apply(tr, set) {
// return set.map(tr.mapping, tr.doc)
// I fear that's not very performant. Maybe improve this "later".
return tr.docChanged ? typographicDecorations(tr.doc) : set
},
},
props: {
decorations(state) {
return typographicPlugin.getState(state)
},
},
})

2 comments on commit 6316b41

@matthiask
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nada sorry, ich konnte es nicht sein lassen.

@nada
Copy link

@nada nada commented on 6316b41 Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂😂 @matthiask Hahahahaha! Sehr schön.

Bei mir klemmt grad das Hauptprojekt....

Please sign in to comment.