Skip to content

Commit

Permalink
Add Ckeditor::Rails.assets_base_path
Browse files Browse the repository at this point in the history
  • Loading branch information
tsechingho committed Nov 16, 2021
1 parent f8c4bcf commit 4fa95ec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ ol,ul,dl {
}
```
### Configure plugins, languages, and skins of CKEditor assets
### Configure plugins, languages, skins and base_path of CKEditor assets
Add `ckeditor_rails.rb` to `config/initializers/`
Expand All @@ -112,6 +112,12 @@ Ckeditor::Rails.configure do |config|
# default is nil for all skins, or set as %w[moono-lisa]
config.assets_skins = nil
# default is nil and it will be "#{::Sprockets::Railtie.config.assets.prefix}/ckeditor",
# or set as String like '/assets/ckeditor',
# or set as Proc / Lambda
# no slash in the end
config.assets_base_path = nil
end
```
Expand Down
7 changes: 1 addition & 6 deletions lib/assets/javascripts/ckeditor/basepath.js.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
<%
base_path = File.join(Rails.application.config.assets.prefix, 'ckeditor/')
base_path = File.join(Rails.application.config.action_controller.relative_url_root, base_path) if Rails.application.config.action_controller.relative_url_root
%>

var CKEDITOR_BASEPATH = "<%= base_path %>";
var CKEDITOR_BASEPATH = "<%= "#{::Ckeditor::Rails.assets_base_path}/" %>";
24 changes: 24 additions & 0 deletions lib/ckeditor-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module Rails
mattr_accessor :assets_skins
@@assets_skins = nil

mattr_writer :assets_base_path
@@assets_base_path = nil

class << self
def configure
yield self
Expand Down Expand Up @@ -51,6 +54,27 @@ def default_plugins
def default_skins
%w[moono-lisa]
end

def default_base_path
"#{::Sprockets::Railtie.config.assets.prefix}/ckeditor"
end

def assets_base_path
return @@assets_base_path unless @@assets_base_path.nil?

if @@assets_base_path.respond_to? :call
self.assets_base_path = @@assets_base_path.call
elsif !@@assets_base_path.is_a? String
self.assets_base_path = default_base_path
end

relative_path = ::Rails.application.config.action_controller.relative_url_root
self.assets_base_path = File.join(relative_path, @@assets_base_path) if relative_path && @@assets_base_path

self.assets_base_path = @@assets_base_path.sub(/\/\z/, '') if @@assets_base_path.ends_with?('/')

@@assets_base_path
end
end

end
Expand Down
8 changes: 7 additions & 1 deletion lib/ckeditor-rails/asset_url_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ def self.stylesheet_files
@stylesheet_files ||= ::Ckeditor::Rails::Asset.new.stylesheet_files
end

def self.assets_base_path(context = nil)
return ::Ckeditor::Rails.assets_base_path unless context

"#{context.assets_prefix}/ckeditor"
end

def self.call(input)
return { data: input[:data] } unless stylesheet_files.include?(input[:filename])

context = input[:environment].context_class.new(input)
path_prefix = "#{context.assets_prefix}/ckeditor"
path_prefix = assets_base_path()
matched_folders = input[:filename].match(/\/ckeditor\/(plugins|skins)\/([\w-]+)\//)

data = input[:data].gsub(REGEX) { |_match|
Expand Down

0 comments on commit 4fa95ec

Please sign in to comment.