wagtail-command-palette 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wagtail_command_palette-0.1.0/LICENSE +21 -0
- wagtail_command_palette-0.1.0/PKG-INFO +97 -0
- wagtail_command_palette-0.1.0/README.md +72 -0
- wagtail_command_palette-0.1.0/pyproject.toml +61 -0
- wagtail_command_palette-0.1.0/pyproject.toml.orig +56 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/__init__.py +5 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/apps.py +13 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/blocks.py +123 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/locale/fr/LC_MESSAGES/django.mo +0 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/locale/fr/LC_MESSAGES/django.po +55 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/management/__init__.py +0 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/management/commands/__init__.py +0 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/management/commands/update_block_index.py +21 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/migrations/0001_initial.py +52 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/migrations/__init__.py +0 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/models.py +40 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/search.py +153 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/static/wagtail_command_palette/palette.css +153 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/static/wagtail_command_palette/palette.js +97 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/templates/wagtail_command_palette/palette.html +23 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/templates/wagtail_command_palette/results.html +20 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/views.py +41 -0
- wagtail_command_palette-0.1.0/src/wagtail_command_palette/wagtail_hooks.py +29 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wagtail-command-palette contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wagtail-command-palette
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Cmd+K command palette for the Wagtail admin: search pages, StreamField blocks, snippets, images, documents and admin menus, powered by django-modelsearch.
|
|
5
|
+
Keywords: wagtail,django,search,command palette,streamfield,modelsearch
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Framework :: Django
|
|
13
|
+
Classifier: Framework :: Wagtail
|
|
14
|
+
Classifier: Framework :: Wagtail :: 7
|
|
15
|
+
Requires-Dist: django>=5.2
|
|
16
|
+
Requires-Dist: wagtail>=7.4
|
|
17
|
+
Requires-Dist: modelsearch>=1.3
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Project-URL: Homepage, https://github.com/incubateur-ademe/wagtail-search-palette
|
|
20
|
+
Project-URL: Repository, https://github.com/incubateur-ademe/wagtail-search-palette
|
|
21
|
+
Project-URL: Changelog, https://github.com/incubateur-ademe/wagtail-search-palette/blob/main/CHANGELOG.md
|
|
22
|
+
Project-URL: Documentation, https://github.com/incubateur-ademe/wagtail-search-palette/blob/main/README.md
|
|
23
|
+
Project-URL: Issues, https://github.com/incubateur-ademe/wagtail-search-palette/issues
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# wagtail-command-palette
|
|
27
|
+
|
|
28
|
+
A <kbd>Cmd</kbd>+<kbd>K</kbd> command palette for the Wagtail admin. One search box finds:
|
|
29
|
+
|
|
30
|
+
| Category | What it holds | How it is searched |
|
|
31
|
+
| --- | --- | --- |
|
|
32
|
+
| **Go to** | Every admin menu item the user can see, submenus included | Label contains the words typed |
|
|
33
|
+
| **Pages** | All page types | [django-modelsearch](https://github.com/wagtail/django-modelsearch) |
|
|
34
|
+
| **Blocks** | Every StreamField block, nested ones too, of pages and of any other model | django-modelsearch |
|
|
35
|
+
| One per indexed model | Images, documents, snippets with `search_fields`… | django-modelsearch |
|
|
36
|
+
| **Search in** | Every search the admin registers (`register_admin_search_area`), with the query filled in | A link to that search |
|
|
37
|
+
|
|
38
|
+
A block result shows the rendered block next to the list and opens the editor scrolled to that block.
|
|
39
|
+
|
|
40
|
+
Results follow permissions: an object the user may not edit is left out, and so are its blocks.
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
uv add wagtail-command-palette
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
INSTALLED_APPS = [
|
|
50
|
+
"wagtail_command_palette",
|
|
51
|
+
# …
|
|
52
|
+
"wagtail.search",
|
|
53
|
+
]
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
./manage.py migrate
|
|
58
|
+
./manage.py update_block_index # once, to index the blocks that already exist
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Blocks are then kept in sync when an object or one of its revisions is saved.
|
|
62
|
+
|
|
63
|
+
Requires Wagtail 7.4 or later. Works with any modelsearch backend (database, Elasticsearch, OpenSearch).
|
|
64
|
+
|
|
65
|
+
## Keyboard
|
|
66
|
+
|
|
67
|
+
| Key | Action |
|
|
68
|
+
| --- | --- |
|
|
69
|
+
| <kbd>Cmd</kbd>+<kbd>K</kbd> / <kbd>Ctrl</kbd>+<kbd>K</kbd> | Open or close the palette |
|
|
70
|
+
| <kbd>↑</kbd> <kbd>↓</kbd> | Move through the results |
|
|
71
|
+
| <kbd>Enter</kbd> | Open the selected result |
|
|
72
|
+
| <kbd>Esc</kbd> | Close |
|
|
73
|
+
|
|
74
|
+
In a rich text field the shortcut stays Draftail's "add a link": click outside the field first.
|
|
75
|
+
|
|
76
|
+
## How blocks are searched
|
|
77
|
+
|
|
78
|
+
modelsearch indexes a StreamField as one text per object, so it can tell which page matched but not which block. This package stores one `BlockEntry` row per block, itself indexed with modelsearch, so blocks are ranked, filtered and linked one by one.
|
|
79
|
+
|
|
80
|
+
- **Nested blocks**: a block inside a nested `StreamBlock` gets its own entry, and its text is taken out of its parent's. A section is found by its title, its paragraphs by their words.
|
|
81
|
+
- **Drafts**: for models with revisions (pages included), the latest revision is indexed, since that is what the editor opens.
|
|
82
|
+
- **Preview**: the block is rendered with [Wagtail's block preview template](https://docs.wagtail.org/en/stable/advanced_topics/streamfield_block_previews.html). Override `wagtailcore/shared/block_preview.html` to load your site's CSS, as for the block picker. The preview runs in a sandboxed iframe without scripts.
|
|
83
|
+
- Only blocks with searchable text are indexed: an image block without a caption is not found.
|
|
84
|
+
|
|
85
|
+
## Limits
|
|
86
|
+
|
|
87
|
+
- Models that are not indexed with modelsearch (users, snippets without `search_fields`) are reached through their **Search in** link, not listed directly.
|
|
88
|
+
- Each category shows 5 results.
|
|
89
|
+
- The excerpt of a block is centred on the words as typed. A match found by stemming shows the start of the block instead.
|
|
90
|
+
|
|
91
|
+
## Development
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
just install
|
|
95
|
+
just test
|
|
96
|
+
just demo # http://localhost:8000/admin/, admin / admin
|
|
97
|
+
```
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# wagtail-command-palette
|
|
2
|
+
|
|
3
|
+
A <kbd>Cmd</kbd>+<kbd>K</kbd> command palette for the Wagtail admin. One search box finds:
|
|
4
|
+
|
|
5
|
+
| Category | What it holds | How it is searched |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| **Go to** | Every admin menu item the user can see, submenus included | Label contains the words typed |
|
|
8
|
+
| **Pages** | All page types | [django-modelsearch](https://github.com/wagtail/django-modelsearch) |
|
|
9
|
+
| **Blocks** | Every StreamField block, nested ones too, of pages and of any other model | django-modelsearch |
|
|
10
|
+
| One per indexed model | Images, documents, snippets with `search_fields`… | django-modelsearch |
|
|
11
|
+
| **Search in** | Every search the admin registers (`register_admin_search_area`), with the query filled in | A link to that search |
|
|
12
|
+
|
|
13
|
+
A block result shows the rendered block next to the list and opens the editor scrolled to that block.
|
|
14
|
+
|
|
15
|
+
Results follow permissions: an object the user may not edit is left out, and so are its blocks.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
uv add wagtail-command-palette
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
INSTALLED_APPS = [
|
|
25
|
+
"wagtail_command_palette",
|
|
26
|
+
# …
|
|
27
|
+
"wagtail.search",
|
|
28
|
+
]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
./manage.py migrate
|
|
33
|
+
./manage.py update_block_index # once, to index the blocks that already exist
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Blocks are then kept in sync when an object or one of its revisions is saved.
|
|
37
|
+
|
|
38
|
+
Requires Wagtail 7.4 or later. Works with any modelsearch backend (database, Elasticsearch, OpenSearch).
|
|
39
|
+
|
|
40
|
+
## Keyboard
|
|
41
|
+
|
|
42
|
+
| Key | Action |
|
|
43
|
+
| --- | --- |
|
|
44
|
+
| <kbd>Cmd</kbd>+<kbd>K</kbd> / <kbd>Ctrl</kbd>+<kbd>K</kbd> | Open or close the palette |
|
|
45
|
+
| <kbd>↑</kbd> <kbd>↓</kbd> | Move through the results |
|
|
46
|
+
| <kbd>Enter</kbd> | Open the selected result |
|
|
47
|
+
| <kbd>Esc</kbd> | Close |
|
|
48
|
+
|
|
49
|
+
In a rich text field the shortcut stays Draftail's "add a link": click outside the field first.
|
|
50
|
+
|
|
51
|
+
## How blocks are searched
|
|
52
|
+
|
|
53
|
+
modelsearch indexes a StreamField as one text per object, so it can tell which page matched but not which block. This package stores one `BlockEntry` row per block, itself indexed with modelsearch, so blocks are ranked, filtered and linked one by one.
|
|
54
|
+
|
|
55
|
+
- **Nested blocks**: a block inside a nested `StreamBlock` gets its own entry, and its text is taken out of its parent's. A section is found by its title, its paragraphs by their words.
|
|
56
|
+
- **Drafts**: for models with revisions (pages included), the latest revision is indexed, since that is what the editor opens.
|
|
57
|
+
- **Preview**: the block is rendered with [Wagtail's block preview template](https://docs.wagtail.org/en/stable/advanced_topics/streamfield_block_previews.html). Override `wagtailcore/shared/block_preview.html` to load your site's CSS, as for the block picker. The preview runs in a sandboxed iframe without scripts.
|
|
58
|
+
- Only blocks with searchable text are indexed: an image block without a caption is not found.
|
|
59
|
+
|
|
60
|
+
## Limits
|
|
61
|
+
|
|
62
|
+
- Models that are not indexed with modelsearch (users, snippets without `search_fields`) are reached through their **Search in** link, not listed directly.
|
|
63
|
+
- Each category shows 5 results.
|
|
64
|
+
- The excerpt of a block is centred on the words as typed. A match found by stemming shows the start of the block instead.
|
|
65
|
+
|
|
66
|
+
## Development
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
just install
|
|
70
|
+
just test
|
|
71
|
+
just demo # http://localhost:8000/admin/, admin / admin
|
|
72
|
+
```
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "wagtail-command-palette"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A Cmd+K command palette for the Wagtail admin: search pages, StreamField blocks, snippets, images, documents and admin menus, powered by django-modelsearch."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
classifiers = [
|
|
9
|
+
"Development Status :: 3 - Alpha",
|
|
10
|
+
"Intended Audience :: Developers",
|
|
11
|
+
"Operating System :: OS Independent",
|
|
12
|
+
"Programming Language :: Python :: 3",
|
|
13
|
+
"Framework :: Django",
|
|
14
|
+
"Framework :: Wagtail",
|
|
15
|
+
"Framework :: Wagtail :: 7",
|
|
16
|
+
]
|
|
17
|
+
keywords = [
|
|
18
|
+
"wagtail",
|
|
19
|
+
"django",
|
|
20
|
+
"search",
|
|
21
|
+
"command palette",
|
|
22
|
+
"streamfield",
|
|
23
|
+
"modelsearch",
|
|
24
|
+
]
|
|
25
|
+
requires-python = ">=3.12"
|
|
26
|
+
dependencies = [
|
|
27
|
+
"django>=5.2",
|
|
28
|
+
"wagtail>=7.4",
|
|
29
|
+
"modelsearch>=1.3",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = "https://github.com/incubateur-ademe/wagtail-search-palette"
|
|
34
|
+
Repository = "https://github.com/incubateur-ademe/wagtail-search-palette"
|
|
35
|
+
Changelog = "https://github.com/incubateur-ademe/wagtail-search-palette/blob/main/CHANGELOG.md"
|
|
36
|
+
Documentation = "https://github.com/incubateur-ademe/wagtail-search-palette/blob/main/README.md"
|
|
37
|
+
Issues = "https://github.com/incubateur-ademe/wagtail-search-palette/issues"
|
|
38
|
+
|
|
39
|
+
[dependency-groups]
|
|
40
|
+
dev = [
|
|
41
|
+
"pytest>=8.3",
|
|
42
|
+
"pytest-cov>=7.1.0",
|
|
43
|
+
"pytest-django>=4.9",
|
|
44
|
+
"ruff>=0.15.9",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[build-system]
|
|
48
|
+
requires = ["uv_build>=0.9,<0.14"]
|
|
49
|
+
build-backend = "uv_build"
|
|
50
|
+
|
|
51
|
+
[tool.uv.build-backend]
|
|
52
|
+
module-name = "wagtail_command_palette"
|
|
53
|
+
source-exclude = [
|
|
54
|
+
"wagtail_command_palette/test",
|
|
55
|
+
"*.db",
|
|
56
|
+
".*",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
[tool.pytest.ini_options]
|
|
60
|
+
DJANGO_SETTINGS_MODULE = "wagtail_command_palette.test.settings"
|
|
61
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "wagtail-command-palette"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A Cmd+K command palette for the Wagtail admin: search pages, StreamField blocks, snippets, images, documents and admin menus, powered by django-modelsearch."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
classifiers = [
|
|
9
|
+
"Development Status :: 3 - Alpha",
|
|
10
|
+
"Intended Audience :: Developers",
|
|
11
|
+
"Operating System :: OS Independent",
|
|
12
|
+
"Programming Language :: Python :: 3",
|
|
13
|
+
"Framework :: Django",
|
|
14
|
+
"Framework :: Wagtail",
|
|
15
|
+
"Framework :: Wagtail :: 7",
|
|
16
|
+
]
|
|
17
|
+
keywords = ["wagtail", "django", "search", "command palette", "streamfield", "modelsearch"]
|
|
18
|
+
requires-python = ">=3.12"
|
|
19
|
+
dependencies = [
|
|
20
|
+
"django>=5.2",
|
|
21
|
+
# 7.4 pins modelsearch 1.3, the version this package is written against.
|
|
22
|
+
"wagtail>=7.4",
|
|
23
|
+
# Wagtail's own search library, used directly.
|
|
24
|
+
"modelsearch>=1.3",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/incubateur-ademe/wagtail-search-palette"
|
|
29
|
+
Repository = "https://github.com/incubateur-ademe/wagtail-search-palette"
|
|
30
|
+
Changelog = "https://github.com/incubateur-ademe/wagtail-search-palette/blob/main/CHANGELOG.md"
|
|
31
|
+
Documentation = "https://github.com/incubateur-ademe/wagtail-search-palette/blob/main/README.md"
|
|
32
|
+
Issues = "https://github.com/incubateur-ademe/wagtail-search-palette/issues"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = [
|
|
36
|
+
"pytest>=8.3",
|
|
37
|
+
"pytest-cov>=7.1.0",
|
|
38
|
+
"pytest-django>=4.9",
|
|
39
|
+
"ruff>=0.15.9",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[build-system]
|
|
43
|
+
requires = ["uv_build>=0.9,<0.14"]
|
|
44
|
+
build-backend = "uv_build"
|
|
45
|
+
|
|
46
|
+
[tool.uv.build-backend]
|
|
47
|
+
module-name = "wagtail_command_palette"
|
|
48
|
+
source-exclude = [
|
|
49
|
+
"wagtail_command_palette/test",
|
|
50
|
+
"*.db",
|
|
51
|
+
".*",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
DJANGO_SETTINGS_MODULE = "wagtail_command_palette.test.settings"
|
|
56
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from django.apps import AppConfig
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class WagtailCommandPaletteAppConfig(AppConfig):
|
|
5
|
+
label = "wagtail_command_palette"
|
|
6
|
+
name = "wagtail_command_palette"
|
|
7
|
+
verbose_name = "Wagtail command palette"
|
|
8
|
+
default_auto_field = "django.db.models.BigAutoField"
|
|
9
|
+
|
|
10
|
+
def ready(self):
|
|
11
|
+
from wagtail_command_palette import blocks
|
|
12
|
+
|
|
13
|
+
blocks.connect_signals()
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""Extract the blocks of StreamFields and keep their BlockEntry rows in sync."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from functools import cache
|
|
5
|
+
|
|
6
|
+
from django.contrib.contenttypes.models import ContentType
|
|
7
|
+
from django.db import transaction
|
|
8
|
+
from django.db.models.signals import post_delete, post_save
|
|
9
|
+
from django.utils.html import strip_tags
|
|
10
|
+
from wagtail.blocks import StreamBlock, StructBlock
|
|
11
|
+
from wagtail.fields import StreamField
|
|
12
|
+
from wagtail.models import Revision
|
|
13
|
+
|
|
14
|
+
from wagtail_command_palette.models import BlockEntry
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@cache
|
|
18
|
+
def stream_fields(model):
|
|
19
|
+
return [f for f in model._meta.get_fields() if isinstance(f, StreamField)]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _own_text(block, value, found):
|
|
23
|
+
"""
|
|
24
|
+
Return the searchable texts of `value` that no nested stream block owns,
|
|
25
|
+
appending every stream block met on the way to `found` as (child, texts).
|
|
26
|
+
|
|
27
|
+
A section holding a nested stream is then found by its own title, and its
|
|
28
|
+
children by their own content, instead of the section matching for all.
|
|
29
|
+
"""
|
|
30
|
+
if value is None:
|
|
31
|
+
return []
|
|
32
|
+
if isinstance(block, StreamBlock):
|
|
33
|
+
for child in value:
|
|
34
|
+
found.append((child, _own_text(child.block, child.value, found)))
|
|
35
|
+
return []
|
|
36
|
+
# Not every StructBlock holds a dict: the value of ImageBlock is an image.
|
|
37
|
+
if isinstance(block, StructBlock) and isinstance(value, Mapping):
|
|
38
|
+
return [
|
|
39
|
+
text
|
|
40
|
+
for name, sub_block in block.child_blocks.items()
|
|
41
|
+
for text in _own_text(sub_block, value.get(name), found)
|
|
42
|
+
]
|
|
43
|
+
return block.get_searchable_content(value)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def get_blocks(obj):
|
|
47
|
+
"""Return (field name, stream child, text) for every stream block of `obj`."""
|
|
48
|
+
blocks = []
|
|
49
|
+
for field in stream_fields(type(obj)):
|
|
50
|
+
found = []
|
|
51
|
+
_own_text(field.stream_block, getattr(obj, field.name), found)
|
|
52
|
+
blocks += [
|
|
53
|
+
# Rich text is indexed as HTML source: keep the words only.
|
|
54
|
+
(field.name, child, strip_tags(" ".join(texts)).strip())
|
|
55
|
+
for child, texts in found
|
|
56
|
+
]
|
|
57
|
+
return blocks
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def find_block(obj, block_id):
|
|
61
|
+
"""Return the stream child of `obj` with this id, or None."""
|
|
62
|
+
for _, child, _ in get_blocks(obj):
|
|
63
|
+
if child.id == block_id:
|
|
64
|
+
return child
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def latest(obj):
|
|
68
|
+
"""Return `obj` as the editor shows it: its latest revision when it has one."""
|
|
69
|
+
revision = getattr(obj, "latest_revision", None)
|
|
70
|
+
return revision.as_object() if revision else obj
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def index_object(obj):
|
|
74
|
+
"""Rewrite the BlockEntry rows of `obj` from its current StreamField values."""
|
|
75
|
+
content_type = ContentType.objects.get_for_model(obj)
|
|
76
|
+
entries = BlockEntry.objects.filter(content_type=content_type, object_id=obj.pk)
|
|
77
|
+
rows = {
|
|
78
|
+
child.id: (field_name, child.block_type, text)
|
|
79
|
+
for field_name, child, text in get_blocks(obj)
|
|
80
|
+
if child.id and text
|
|
81
|
+
}
|
|
82
|
+
current = {e.block_id: (e.field_name, e.block_type, e.text) for e in entries}
|
|
83
|
+
if rows == current:
|
|
84
|
+
return
|
|
85
|
+
with transaction.atomic():
|
|
86
|
+
# One by one, not in bulk: modelsearch indexes on post_save/post_delete.
|
|
87
|
+
for entry in entries:
|
|
88
|
+
entry.delete()
|
|
89
|
+
for block_id, (field_name, block_type, text) in rows.items():
|
|
90
|
+
BlockEntry.objects.create(
|
|
91
|
+
content_type=content_type,
|
|
92
|
+
object_id=obj.pk,
|
|
93
|
+
field_name=field_name,
|
|
94
|
+
block_id=block_id,
|
|
95
|
+
block_type=block_type,
|
|
96
|
+
text=text,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _on_save(sender, instance, raw=False, **kwargs):
|
|
101
|
+
if raw:
|
|
102
|
+
return
|
|
103
|
+
if sender is Revision:
|
|
104
|
+
model = instance.content_type.model_class()
|
|
105
|
+
if model and stream_fields(model):
|
|
106
|
+
index_object(instance.as_object())
|
|
107
|
+
# With revisions, the latest one is indexed, not the live fields.
|
|
108
|
+
elif stream_fields(sender) and not getattr(instance, "latest_revision_id", None):
|
|
109
|
+
index_object(instance)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _on_delete(sender, instance, **kwargs):
|
|
113
|
+
if sender is not BlockEntry and stream_fields(sender):
|
|
114
|
+
for entry in BlockEntry.objects.filter(
|
|
115
|
+
content_type=ContentType.objects.get_for_model(sender),
|
|
116
|
+
object_id=instance.pk,
|
|
117
|
+
):
|
|
118
|
+
entry.delete()
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def connect_signals():
|
|
122
|
+
post_save.connect(_on_save, dispatch_uid="wagtail_command_palette_save")
|
|
123
|
+
post_delete.connect(_on_delete, dispatch_uid="wagtail_command_palette_delete")
|
|
Binary file
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# SOME DESCRIPTIVE TITLE.
|
|
2
|
+
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
|
|
3
|
+
# This file is distributed under the same license as the PACKAGE package.
|
|
4
|
+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
|
|
5
|
+
#
|
|
6
|
+
msgid ""
|
|
7
|
+
msgstr ""
|
|
8
|
+
"Project-Id-Version: PACKAGE VERSION\n"
|
|
9
|
+
"Report-Msgid-Bugs-To: \n"
|
|
10
|
+
"POT-Creation-Date: 2026-12-04 06:03-0600\n"
|
|
11
|
+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
|
12
|
+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
|
13
|
+
"Language-Team: LANGUAGE <LL@li.org>\n"
|
|
14
|
+
"Language: \n"
|
|
15
|
+
"MIME-Version: 1.0\n"
|
|
16
|
+
"Content-Type: text/plain; charset=UTF-8\n"
|
|
17
|
+
"Content-Transfer-Encoding: 8bit\n"
|
|
18
|
+
"Plural-Forms: nplurals=3; plural=(n == 0 || n == 1) ? 0 : n != 0 && n % "
|
|
19
|
+
"1000000 == 0 ? 1 : 2;\n"
|
|
20
|
+
#: search.py:118
|
|
21
|
+
msgid "Blocks"
|
|
22
|
+
msgstr "Blocs"
|
|
23
|
+
|
|
24
|
+
#: search.py:126
|
|
25
|
+
msgid "Go to"
|
|
26
|
+
msgstr "Aller à"
|
|
27
|
+
|
|
28
|
+
#: search.py:142
|
|
29
|
+
msgid "Search in"
|
|
30
|
+
msgstr "Rechercher dans"
|
|
31
|
+
|
|
32
|
+
#: templates/wagtail_command_palette/palette.html:2
|
|
33
|
+
msgid "Command palette"
|
|
34
|
+
msgstr "Palette de commandes"
|
|
35
|
+
|
|
36
|
+
#: templates/wagtail_command_palette/palette.html:11
|
|
37
|
+
msgid "Search the admin"
|
|
38
|
+
msgstr "Rechercher dans l’administration"
|
|
39
|
+
|
|
40
|
+
#: templates/wagtail_command_palette/palette.html:12
|
|
41
|
+
msgid "Search pages, blocks, snippets, settings…"
|
|
42
|
+
msgstr "Rechercher des pages, blocs, fragments, réglages…"
|
|
43
|
+
|
|
44
|
+
#: templates/wagtail_command_palette/palette.html:18
|
|
45
|
+
msgid "Results"
|
|
46
|
+
msgstr "Résultats"
|
|
47
|
+
|
|
48
|
+
#: templates/wagtail_command_palette/palette.html:20
|
|
49
|
+
msgid "Block preview"
|
|
50
|
+
msgstr "Aperçu du bloc"
|
|
51
|
+
|
|
52
|
+
#: templates/wagtail_command_palette/results.html:19
|
|
53
|
+
#, python-format
|
|
54
|
+
msgid "No results for “%(query)s”."
|
|
55
|
+
msgstr "Aucun résultat pour « %(query)s »."
|
|
File without changes
|
|
File without changes
|
wagtail_command_palette-0.1.0/src/wagtail_command_palette/management/commands/update_block_index.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from django.apps import apps
|
|
2
|
+
from django.core.management.base import BaseCommand
|
|
3
|
+
|
|
4
|
+
from wagtail_command_palette.blocks import index_object, latest, stream_fields
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Command(BaseCommand):
|
|
8
|
+
help = "Index the StreamField blocks of every object for the command palette."
|
|
9
|
+
|
|
10
|
+
def handle(self, **options):
|
|
11
|
+
for model in apps.get_models():
|
|
12
|
+
if not stream_fields(model):
|
|
13
|
+
continue
|
|
14
|
+
count = 0
|
|
15
|
+
for obj in model._default_manager.iterator():
|
|
16
|
+
# A page of a subclass is indexed with its own model, not here.
|
|
17
|
+
if getattr(obj, "specific_class", model) is not model:
|
|
18
|
+
continue
|
|
19
|
+
index_object(latest(obj))
|
|
20
|
+
count += 1
|
|
21
|
+
self.stdout.write(f"{model._meta.label}: {count}")
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Generated by Django 6.1.4 on 2026-12-04 12:00
|
|
2
|
+
|
|
3
|
+
import django.db.models.deletion
|
|
4
|
+
import modelsearch.index
|
|
5
|
+
|
|
6
|
+
from django.db import migrations, models
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Migration(migrations.Migration):
|
|
10
|
+
initial = True
|
|
11
|
+
|
|
12
|
+
dependencies = [
|
|
13
|
+
("contenttypes", "0002_remove_content_type_name"),
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
operations = [
|
|
17
|
+
migrations.CreateModel(
|
|
18
|
+
name="BlockEntry",
|
|
19
|
+
fields=[
|
|
20
|
+
(
|
|
21
|
+
"id",
|
|
22
|
+
models.BigAutoField(
|
|
23
|
+
auto_created=True,
|
|
24
|
+
primary_key=True,
|
|
25
|
+
serialize=False,
|
|
26
|
+
verbose_name="ID",
|
|
27
|
+
),
|
|
28
|
+
),
|
|
29
|
+
("object_id", models.CharField(max_length=255)),
|
|
30
|
+
("field_name", models.CharField(max_length=255)),
|
|
31
|
+
("block_id", models.CharField(max_length=255)),
|
|
32
|
+
("block_type", models.CharField(max_length=255)),
|
|
33
|
+
("text", models.TextField()),
|
|
34
|
+
(
|
|
35
|
+
"content_type",
|
|
36
|
+
models.ForeignKey(
|
|
37
|
+
on_delete=django.db.models.deletion.CASCADE,
|
|
38
|
+
to="contenttypes.contenttype",
|
|
39
|
+
),
|
|
40
|
+
),
|
|
41
|
+
],
|
|
42
|
+
options={
|
|
43
|
+
"indexes": [
|
|
44
|
+
models.Index(
|
|
45
|
+
fields=["content_type", "object_id"],
|
|
46
|
+
name="wagtail_com_content_b6d673_idx",
|
|
47
|
+
)
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
bases=(modelsearch.index.Indexed, models.Model),
|
|
51
|
+
),
|
|
52
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
2
|
+
from django.contrib.contenttypes.models import ContentType
|
|
3
|
+
from django.db import models
|
|
4
|
+
from modelsearch import index
|
|
5
|
+
from modelsearch.queryset import SearchableQuerySetMixin
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class BlockEntryQuerySet(SearchableQuerySetMixin, models.QuerySet):
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class BlockEntry(index.Indexed, models.Model):
|
|
13
|
+
"""
|
|
14
|
+
One StreamField block of one object, as a row modelsearch can index.
|
|
15
|
+
|
|
16
|
+
modelsearch indexes a StreamField as a single text per object, so a match
|
|
17
|
+
cannot be traced back to a block. One indexed row per block can.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
|
21
|
+
object_id = models.CharField(max_length=255)
|
|
22
|
+
content_object = GenericForeignKey("content_type", "object_id")
|
|
23
|
+
field_name = models.CharField(max_length=255)
|
|
24
|
+
block_id = models.CharField(max_length=255)
|
|
25
|
+
block_type = models.CharField(max_length=255)
|
|
26
|
+
text = models.TextField()
|
|
27
|
+
|
|
28
|
+
objects = BlockEntryQuerySet.as_manager()
|
|
29
|
+
|
|
30
|
+
search_fields = [
|
|
31
|
+
index.SearchField("text"),
|
|
32
|
+
index.AutocompleteField("text"),
|
|
33
|
+
index.FilterField("block_type"),
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
class Meta:
|
|
37
|
+
indexes = [models.Index(fields=["content_type", "object_id"])]
|
|
38
|
+
|
|
39
|
+
def __str__(self):
|
|
40
|
+
return f"{self.block_type} {self.block_id}"
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""What the palette finds for a query, by category."""
|
|
2
|
+
|
|
3
|
+
from django.urls import reverse
|
|
4
|
+
from django.utils.http import urlencode
|
|
5
|
+
from django.utils.text import capfirst
|
|
6
|
+
from django.utils.translation import gettext as _
|
|
7
|
+
from modelsearch.backends import get_search_backend
|
|
8
|
+
from modelsearch.index import get_indexed_models
|
|
9
|
+
from wagtail.admin.admin_url_finder import AdminURLFinder
|
|
10
|
+
from wagtail.admin.menu import admin_menu
|
|
11
|
+
from wagtail.admin.search import admin_search_areas
|
|
12
|
+
from wagtail.documents.models import AbstractDocument
|
|
13
|
+
from wagtail.images.models import AbstractImage
|
|
14
|
+
from wagtail.models import Page
|
|
15
|
+
|
|
16
|
+
from wagtail_command_palette.blocks import find_block, latest
|
|
17
|
+
from wagtail_command_palette.models import BlockEntry
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
LIMIT = 5
|
|
21
|
+
|
|
22
|
+
ICONS = {
|
|
23
|
+
Page: "doc-empty-inverse",
|
|
24
|
+
AbstractImage: "image",
|
|
25
|
+
AbstractDocument: "doc-full",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _hits(query, model):
|
|
30
|
+
"""Partial-word matches on autocomplete fields, then whole-word matches anywhere."""
|
|
31
|
+
backend = get_search_backend()
|
|
32
|
+
# ponytail: fetches twice the limit to make up for results the user may not
|
|
33
|
+
# edit; filter on permissions in the query if whole categories come up short.
|
|
34
|
+
fetch = LIMIT * 2
|
|
35
|
+
hits = []
|
|
36
|
+
if model.get_autocomplete_search_fields():
|
|
37
|
+
hits += backend.autocomplete(query, model, operator="and")[:fetch]
|
|
38
|
+
hits += [
|
|
39
|
+
hit
|
|
40
|
+
for hit in backend.search(query, model, operator="and")[:fetch]
|
|
41
|
+
if hit not in hits
|
|
42
|
+
]
|
|
43
|
+
return hits
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _title(obj):
|
|
47
|
+
return getattr(obj, "get_admin_display_title", obj.__str__)()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _icon(model):
|
|
51
|
+
viewset = getattr(model, "snippet_viewset", None)
|
|
52
|
+
if viewset:
|
|
53
|
+
return viewset.icon
|
|
54
|
+
return next((i for base, i in ICONS.items() if issubclass(model, base)), "snippet")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _excerpt(text, query, width=100):
|
|
58
|
+
# ponytail: modelsearch returns no highlights, so this looks for the query
|
|
59
|
+
# words as typed; a stemmed match falls back to the start of the text.
|
|
60
|
+
lower = text.lower()
|
|
61
|
+
found = [p for word in query.lower().split() if (p := lower.find(word)) >= 0]
|
|
62
|
+
start = max(min(found, default=0) - 30, 0)
|
|
63
|
+
end = start + width
|
|
64
|
+
return ("…" if start else "") + text[start:end] + ("…" if end < len(text) else "")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _menu_items(menu, request, prefix=""):
|
|
68
|
+
for item in menu.menu_items_for_request(request):
|
|
69
|
+
label = f"{prefix}{item.label}"
|
|
70
|
+
if hasattr(item, "menu"):
|
|
71
|
+
yield from _menu_items(item.menu, request, f"{label} › ")
|
|
72
|
+
else:
|
|
73
|
+
yield {"title": label, "url": item.url, "icon": item.icon_name}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _models(request, query, finder):
|
|
77
|
+
models = [m for m in get_indexed_models() if m is not BlockEntry]
|
|
78
|
+
# A model indexed under a parent (every page type) is found through it.
|
|
79
|
+
roots = [
|
|
80
|
+
m for m in models if not any(m is not o and issubclass(m, o) for o in models)
|
|
81
|
+
]
|
|
82
|
+
for model in sorted(roots, key=lambda m: m is not Page):
|
|
83
|
+
results = [
|
|
84
|
+
{
|
|
85
|
+
"title": _title(obj),
|
|
86
|
+
"url": url,
|
|
87
|
+
"icon": _icon(model),
|
|
88
|
+
"meta": getattr(obj, "page_type_display_name", ""),
|
|
89
|
+
}
|
|
90
|
+
for obj in _hits(query, model)
|
|
91
|
+
if (url := finder.get_edit_url(obj))
|
|
92
|
+
]
|
|
93
|
+
yield {
|
|
94
|
+
"label": capfirst(model._meta.verbose_name_plural),
|
|
95
|
+
"results": results[:LIMIT],
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _blocks(request, query, finder):
|
|
100
|
+
results = []
|
|
101
|
+
for entry in _hits(query, BlockEntry):
|
|
102
|
+
owner = entry.content_object
|
|
103
|
+
url = owner and finder.get_edit_url(owner)
|
|
104
|
+
child = url and find_block(latest(owner), entry.block_id)
|
|
105
|
+
if not child:
|
|
106
|
+
continue
|
|
107
|
+
results.append(
|
|
108
|
+
{
|
|
109
|
+
"title": _excerpt(entry.text, query),
|
|
110
|
+
"url": f"{url}#block-{entry.block_id}-section",
|
|
111
|
+
"icon": child.block.meta.icon,
|
|
112
|
+
"meta": f"{child.block.label} · {_title(owner)}",
|
|
113
|
+
"preview_url": reverse(
|
|
114
|
+
"wagtail_command_palette:block_preview", args=[entry.pk]
|
|
115
|
+
),
|
|
116
|
+
}
|
|
117
|
+
)
|
|
118
|
+
return {"label": _("Blocks"), "results": results[:LIMIT]}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def search(request, query):
|
|
122
|
+
"""Return the non-empty categories of results as dicts of label and results."""
|
|
123
|
+
words = query.lower().split()
|
|
124
|
+
categories = [
|
|
125
|
+
{
|
|
126
|
+
"label": _("Go to"),
|
|
127
|
+
"results": [
|
|
128
|
+
item
|
|
129
|
+
for item in _menu_items(admin_menu, request)
|
|
130
|
+
if all(word in item["title"].lower() for word in words)
|
|
131
|
+
],
|
|
132
|
+
}
|
|
133
|
+
]
|
|
134
|
+
if query:
|
|
135
|
+
finder = AdminURLFinder(request.user)
|
|
136
|
+
models = list(_models(request, query, finder))
|
|
137
|
+
categories += [*models[:1], _blocks(request, query, finder), *models[1:]]
|
|
138
|
+
# Every search the admin offers, including the ones of models that are
|
|
139
|
+
# not indexed with modelsearch (users, for one).
|
|
140
|
+
categories.append(
|
|
141
|
+
{
|
|
142
|
+
"label": _("Search in"),
|
|
143
|
+
"results": [
|
|
144
|
+
{
|
|
145
|
+
"title": area.label,
|
|
146
|
+
"url": f"{area.url}?{urlencode({'q': query})}",
|
|
147
|
+
"icon": area.icon_name or "search",
|
|
148
|
+
}
|
|
149
|
+
for area in admin_search_areas.search_items_for_request(request)
|
|
150
|
+
],
|
|
151
|
+
}
|
|
152
|
+
)
|
|
153
|
+
return [category for category in categories if category["results"]]
|
wagtail_command_palette-0.1.0/src/wagtail_command_palette/static/wagtail_command_palette/palette.css
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/* Colours are Wagtail's own custom properties, so both admin themes apply. */
|
|
2
|
+
.wcp {
|
|
3
|
+
--wcp-radius: 0.75rem;
|
|
4
|
+
--wcp-border: 1px solid var(--w-color-border-furniture);
|
|
5
|
+
|
|
6
|
+
width: min(42rem, calc(100vw - 2rem));
|
|
7
|
+
max-width: none;
|
|
8
|
+
margin-top: 12vh;
|
|
9
|
+
padding: 0;
|
|
10
|
+
overflow: hidden;
|
|
11
|
+
border: var(--wcp-border);
|
|
12
|
+
border-radius: var(--wcp-radius);
|
|
13
|
+
background: var(--w-color-surface-page);
|
|
14
|
+
color: var(--w-color-text-context);
|
|
15
|
+
box-shadow: 0 1.5rem 4rem -0.75rem rgb(0 0 0 / 45%);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.wcp--preview {
|
|
19
|
+
width: min(76rem, calc(100vw - 2rem));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.wcp::backdrop {
|
|
23
|
+
background: rgb(0 0 0 / 50%);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.wcp__search {
|
|
27
|
+
display: flex;
|
|
28
|
+
align-items: center;
|
|
29
|
+
gap: 0.75rem;
|
|
30
|
+
padding: 0 1rem;
|
|
31
|
+
border-bottom: var(--wcp-border);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* The focus indicator of the input, which keeps the focus while the palette is open. */
|
|
35
|
+
.wcp__search:focus-within {
|
|
36
|
+
box-shadow: inset 0 -2px 0 var(--w-color-focus);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.wcp__search .icon {
|
|
40
|
+
width: 1.125rem;
|
|
41
|
+
height: 1.125rem;
|
|
42
|
+
color: var(--w-color-text-meta);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* Wagtail forces its focus ring with !important; here it would be cut off by
|
|
46
|
+
the dialog's edges, so the bar under the search row replaces it. */
|
|
47
|
+
.wcp__input:focus-visible {
|
|
48
|
+
outline: 0 !important;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/* Doubled class: Wagtail styles every text input as a bordered field. */
|
|
52
|
+
.wcp__input.wcp__input {
|
|
53
|
+
flex: 1;
|
|
54
|
+
min-width: 0;
|
|
55
|
+
padding: 1rem 0;
|
|
56
|
+
border: 0;
|
|
57
|
+
border-radius: 0;
|
|
58
|
+
outline: 0;
|
|
59
|
+
background: transparent;
|
|
60
|
+
color: inherit;
|
|
61
|
+
font-size: 1.125rem;
|
|
62
|
+
box-shadow: none;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.wcp__key {
|
|
66
|
+
padding: 0.125rem 0.375rem;
|
|
67
|
+
border: var(--wcp-border);
|
|
68
|
+
border-radius: 0.25rem;
|
|
69
|
+
color: var(--w-color-text-meta);
|
|
70
|
+
font: inherit;
|
|
71
|
+
font-size: 0.75rem;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.wcp__body {
|
|
75
|
+
display: flex;
|
|
76
|
+
height: min(60vh, 30rem);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.wcp__results {
|
|
80
|
+
flex: 1 1 24rem;
|
|
81
|
+
min-width: 0;
|
|
82
|
+
padding: 0.5rem;
|
|
83
|
+
overflow-y: auto;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.wcp__heading {
|
|
87
|
+
padding: 0.75rem 0.75rem 0.25rem;
|
|
88
|
+
color: var(--w-color-text-meta);
|
|
89
|
+
font-size: 0.6875rem;
|
|
90
|
+
font-weight: 700;
|
|
91
|
+
letter-spacing: 0.08em;
|
|
92
|
+
text-transform: uppercase;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.wcp__option {
|
|
96
|
+
display: grid;
|
|
97
|
+
grid-template-columns: 1rem minmax(0, 1fr);
|
|
98
|
+
column-gap: 0.75rem;
|
|
99
|
+
align-items: center;
|
|
100
|
+
padding: 0.5rem 0.75rem;
|
|
101
|
+
border-radius: 0.375rem;
|
|
102
|
+
color: inherit;
|
|
103
|
+
text-decoration: none;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.wcp__option .icon {
|
|
107
|
+
width: 1rem;
|
|
108
|
+
height: 1rem;
|
|
109
|
+
color: var(--w-color-text-meta);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.wcp__title,
|
|
113
|
+
.wcp__meta {
|
|
114
|
+
grid-column: 2;
|
|
115
|
+
overflow: hidden;
|
|
116
|
+
text-overflow: ellipsis;
|
|
117
|
+
white-space: nowrap;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.wcp__meta {
|
|
121
|
+
color: var(--w-color-text-meta);
|
|
122
|
+
font-size: 0.8125rem;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.wcp__option[aria-selected="true"] {
|
|
126
|
+
background: var(--w-color-surface-button-default);
|
|
127
|
+
color: var(--w-color-text-button);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.wcp__option[aria-selected="true"] :is(.icon, .wcp__meta) {
|
|
131
|
+
color: inherit;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.wcp__empty {
|
|
135
|
+
margin: 0;
|
|
136
|
+
padding: 2rem 0.75rem;
|
|
137
|
+
color: var(--w-color-text-meta);
|
|
138
|
+
text-align: center;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
.wcp__preview {
|
|
142
|
+
flex: 1 1 60%;
|
|
143
|
+
min-width: 0;
|
|
144
|
+
border: 0;
|
|
145
|
+
border-left: var(--wcp-border);
|
|
146
|
+
background: #fff;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@media (max-width: 50rem) {
|
|
150
|
+
.wcp__preview {
|
|
151
|
+
display: none;
|
|
152
|
+
}
|
|
153
|
+
}
|
wagtail_command_palette-0.1.0/src/wagtail_command_palette/static/wagtail_command_palette/palette.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
const dialog = document.getElementById("wcp");
|
|
3
|
+
if (!dialog) return;
|
|
4
|
+
const input = dialog.querySelector(".wcp__input");
|
|
5
|
+
const results = dialog.querySelector(".wcp__results");
|
|
6
|
+
const preview = dialog.querySelector(".wcp__preview");
|
|
7
|
+
const DEBOUNCE_MS = 120;
|
|
8
|
+
let controller;
|
|
9
|
+
let timer;
|
|
10
|
+
let previewUrl;
|
|
11
|
+
|
|
12
|
+
const options = () => [...results.querySelectorAll("[role=option]")];
|
|
13
|
+
const selected = () => results.querySelector("[aria-selected=true]");
|
|
14
|
+
|
|
15
|
+
function select(option) {
|
|
16
|
+
for (const o of options()) o.setAttribute("aria-selected", o === option);
|
|
17
|
+
if (option) {
|
|
18
|
+
input.setAttribute("aria-activedescendant", option.id);
|
|
19
|
+
option.scrollIntoView({ block: "nearest" });
|
|
20
|
+
} else {
|
|
21
|
+
input.removeAttribute("aria-activedescendant");
|
|
22
|
+
}
|
|
23
|
+
const url = option?.dataset.preview;
|
|
24
|
+
preview.hidden = !url;
|
|
25
|
+
dialog.classList.toggle("wcp--preview", Boolean(url));
|
|
26
|
+
if (url && url !== previewUrl) {
|
|
27
|
+
previewUrl = url;
|
|
28
|
+
// replace(), not src: previews must not pile up in the back button.
|
|
29
|
+
preview.contentWindow.location.replace(url);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function load() {
|
|
34
|
+
controller?.abort();
|
|
35
|
+
controller = new AbortController();
|
|
36
|
+
const url = `${dialog.dataset.url}?${new URLSearchParams({ q: input.value })}`;
|
|
37
|
+
try {
|
|
38
|
+
const response = await fetch(url, { signal: controller.signal });
|
|
39
|
+
// A redirect is the login page: the session has ended.
|
|
40
|
+
if (!response.ok || response.redirected) return;
|
|
41
|
+
// HTML rendered and escaped by this package's own Django template.
|
|
42
|
+
results.innerHTML = await response.text();
|
|
43
|
+
select(options()[0]);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (error.name !== "AbortError") throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
document.addEventListener("keydown", (event) => {
|
|
50
|
+
const isShortcut =
|
|
51
|
+
event.key.toLowerCase() === "k" &&
|
|
52
|
+
(event.metaKey || event.ctrlKey) &&
|
|
53
|
+
!event.altKey &&
|
|
54
|
+
!event.shiftKey;
|
|
55
|
+
// In rich text, Cmd+K is Draftail's "add a link".
|
|
56
|
+
if (!isShortcut || event.target.closest?.(".Draftail-Editor")) return;
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
if (dialog.open) {
|
|
59
|
+
dialog.close();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
dialog.showModal();
|
|
63
|
+
input.select();
|
|
64
|
+
load();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
input.addEventListener("input", () => {
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
timer = setTimeout(load, DEBOUNCE_MS);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
input.addEventListener("keydown", (event) => {
|
|
73
|
+
const all = options();
|
|
74
|
+
const index = all.indexOf(selected());
|
|
75
|
+
if (event.key === "ArrowDown") select(all[(index + 1) % all.length]);
|
|
76
|
+
else if (event.key === "ArrowUp") select(all.at(index - 1));
|
|
77
|
+
else if (event.key === "Enter") selected()?.click();
|
|
78
|
+
else return;
|
|
79
|
+
event.preventDefault();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// mousemove, not mouseover: scrolling with the keyboard must not move the selection.
|
|
83
|
+
results.addEventListener("mousemove", (event) => {
|
|
84
|
+
const option = event.target.closest("[role=option]");
|
|
85
|
+
if (option && option !== selected()) select(option);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// A result on the page being edited only changes the hash: close by hand.
|
|
89
|
+
results.addEventListener("click", (event) => {
|
|
90
|
+
if (event.target.closest("[role=option]")) dialog.close();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// A click on the backdrop lands on the dialog itself.
|
|
94
|
+
dialog.addEventListener("click", (event) => {
|
|
95
|
+
if (event.target === dialog) dialog.close();
|
|
96
|
+
});
|
|
97
|
+
})();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{% load i18n static wagtailadmin_tags %}
|
|
2
|
+
<dialog class="wcp" id="wcp" aria-label="{% translate 'Command palette' %}" data-url="{% url 'wagtail_command_palette:results' %}">
|
|
3
|
+
<div class="wcp__search">
|
|
4
|
+
{% icon name="search" %}
|
|
5
|
+
<input class="wcp__input"
|
|
6
|
+
type="text"
|
|
7
|
+
role="combobox"
|
|
8
|
+
aria-expanded="true"
|
|
9
|
+
aria-controls="wcp-results"
|
|
10
|
+
aria-autocomplete="list"
|
|
11
|
+
aria-label="{% translate 'Search the admin' %}"
|
|
12
|
+
placeholder="{% translate 'Search pages, blocks, snippets, settings…' %}"
|
|
13
|
+
autocomplete="off"
|
|
14
|
+
spellcheck="false" />
|
|
15
|
+
<kbd class="wcp__key">esc</kbd>
|
|
16
|
+
</div>
|
|
17
|
+
<div class="wcp__body">
|
|
18
|
+
<div class="wcp__results" id="wcp-results" role="listbox" aria-label="{% translate 'Results' %}"></div>
|
|
19
|
+
{# No allow-scripts: a block may hold raw HTML written by another editor. #}
|
|
20
|
+
<iframe class="wcp__preview" title="{% translate 'Block preview' %}" sandbox="allow-same-origin" hidden></iframe>
|
|
21
|
+
</div>
|
|
22
|
+
</dialog>
|
|
23
|
+
<script defer src="{% static 'wagtail_command_palette/palette.js' %}"></script>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{% load i18n wagtailadmin_tags %}
|
|
2
|
+
{% for category in categories %}
|
|
3
|
+
<div class="wcp__group" role="group" aria-labelledby="wcp-group-{{ forloop.counter }}">
|
|
4
|
+
<div class="wcp__heading" id="wcp-group-{{ forloop.counter }}">{{ category.label }}</div>
|
|
5
|
+
{% for result in category.results %}
|
|
6
|
+
<a class="wcp__option"
|
|
7
|
+
role="option"
|
|
8
|
+
aria-selected="false"
|
|
9
|
+
id="wcp-option-{{ forloop.parentloop.counter }}-{{ forloop.counter }}"
|
|
10
|
+
href="{{ result.url }}"
|
|
11
|
+
{% if result.preview_url %}data-preview="{{ result.preview_url }}"{% endif %}>
|
|
12
|
+
{% icon name=result.icon %}
|
|
13
|
+
<span class="wcp__title">{{ result.title }}</span>
|
|
14
|
+
{% if result.meta %}<span class="wcp__meta">{{ result.meta }}</span>{% endif %}
|
|
15
|
+
</a>
|
|
16
|
+
{% endfor %}
|
|
17
|
+
</div>
|
|
18
|
+
{% empty %}
|
|
19
|
+
<p class="wcp__empty">{% blocktranslate %}No results for “{{ query }}”.{% endblocktranslate %}</p>
|
|
20
|
+
{% endfor %}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from django.http import Http404
|
|
2
|
+
from django.shortcuts import get_object_or_404, render
|
|
3
|
+
from django.utils.functional import cached_property
|
|
4
|
+
from wagtail.admin.admin_url_finder import AdminURLFinder
|
|
5
|
+
from wagtail.admin.views.generic.preview import StreamFieldBlockPreview
|
|
6
|
+
|
|
7
|
+
from wagtail_command_palette.blocks import find_block, latest
|
|
8
|
+
from wagtail_command_palette.models import BlockEntry
|
|
9
|
+
from wagtail_command_palette.search import search
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def results(request):
|
|
13
|
+
query = request.GET.get("q", "").strip()
|
|
14
|
+
return render(
|
|
15
|
+
request,
|
|
16
|
+
"wagtail_command_palette/results.html",
|
|
17
|
+
{"query": query, "categories": search(request, query)},
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class BlockPreview(StreamFieldBlockPreview):
|
|
22
|
+
"""Wagtail's block preview, with the content of an indexed block."""
|
|
23
|
+
|
|
24
|
+
@cached_property
|
|
25
|
+
def child(self):
|
|
26
|
+
entry = get_object_or_404(BlockEntry, pk=self.kwargs["pk"])
|
|
27
|
+
owner = entry.content_object
|
|
28
|
+
if not owner or not AdminURLFinder(self.request.user).get_edit_url(owner):
|
|
29
|
+
raise Http404
|
|
30
|
+
child = find_block(latest(owner), entry.block_id)
|
|
31
|
+
if not child:
|
|
32
|
+
raise Http404
|
|
33
|
+
return child
|
|
34
|
+
|
|
35
|
+
@cached_property
|
|
36
|
+
def block_def(self):
|
|
37
|
+
return self.child.block
|
|
38
|
+
|
|
39
|
+
@cached_property
|
|
40
|
+
def block_value(self):
|
|
41
|
+
return self.child.value
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from django.template.loader import render_to_string
|
|
2
|
+
from django.templatetags.static import static
|
|
3
|
+
from django.urls import include, path
|
|
4
|
+
from django.utils.html import format_html
|
|
5
|
+
from wagtail import hooks
|
|
6
|
+
|
|
7
|
+
from wagtail_command_palette import views
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@hooks.register("register_admin_urls")
|
|
11
|
+
def register_admin_urls():
|
|
12
|
+
urls = [
|
|
13
|
+
path("", views.results, name="results"),
|
|
14
|
+
path("block/<int:pk>/", views.BlockPreview.as_view(), name="block_preview"),
|
|
15
|
+
]
|
|
16
|
+
return [path("command-palette/", include((urls, "wagtail_command_palette")))]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@hooks.register("insert_global_admin_css")
|
|
20
|
+
def global_admin_css():
|
|
21
|
+
return format_html(
|
|
22
|
+
'<link rel="stylesheet" href="{}">',
|
|
23
|
+
static("wagtail_command_palette/palette.css"),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@hooks.register("insert_global_admin_js")
|
|
28
|
+
def global_admin_js():
|
|
29
|
+
return render_to_string("wagtail_command_palette/palette.html")
|