features.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from django.forms import Media
  2. from wagtail.admin.staticfiles import versioned_static
  3. # Feature objects: these are mapped to feature identifiers within the rich text
  4. # feature registry (wagtail.rich_text.features). Each one implements
  5. # a `construct_options` method which modifies an options dict as appropriate to
  6. # enable that feature.
  7. # Additionally, a Feature object defines a media property
  8. # (https://docs.djangoproject.com/en/stable/topics/forms/media/) to specify css/js
  9. # files to import when the feature is active.
  10. class Feature:
  11. def __init__(self, js=None, css=None):
  12. self.js = js or []
  13. self.css = css or {}
  14. @property
  15. def media(self):
  16. js = [versioned_static(js_file) for js_file in self.js]
  17. css = {}
  18. for media_type, css_files in self.css.items():
  19. css[media_type] = [versioned_static(css_file) for css_file in css_files]
  20. return Media(js=js, css=css)
  21. class BooleanFeature(Feature):
  22. """
  23. A feature which is enabled by a boolean flag at the top level of
  24. the options dict
  25. """
  26. def __init__(self, option_name, **kwargs):
  27. super().__init__(**kwargs)
  28. self.option_name = option_name
  29. def construct_options(self, options):
  30. options[self.option_name] = True
  31. class ListFeature(Feature):
  32. """
  33. Abstract class for features that are defined in a list within the options dict.
  34. Subclasses must define option_name
  35. """
  36. def __init__(self, data, **kwargs):
  37. super().__init__(**kwargs)
  38. self.data = data
  39. def construct_options(self, options):
  40. if self.option_name not in options:
  41. options[self.option_name] = []
  42. options[self.option_name].append(self.data)
  43. class EntityFeature(ListFeature):
  44. """A feature which is listed in the entityTypes list of the options"""
  45. option_name = "entityTypes"
  46. class BlockFeature(ListFeature):
  47. """A feature which is listed in the blockTypes list of the options"""
  48. option_name = "blockTypes"
  49. class InlineStyleFeature(ListFeature):
  50. """A feature which is listed in the inlineStyles list of the options"""
  51. option_name = "inlineStyles"
  52. class DecoratorFeature(ListFeature):
  53. """A feature which is listed in the decorators list of the options"""
  54. option_name = "decorators"
  55. class ControlFeature(ListFeature):
  56. """A feature which is listed in the controls list of the options"""
  57. option_name = "controls"
  58. class PluginFeature(ListFeature):
  59. """A feature which is listed in the plugins list of the options"""
  60. option_name = "plugins"