forked from tomkr/draft
-
Notifications
You must be signed in to change notification settings - Fork 1
Add ranges to draft #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| defmodule Draft.Ranges do | ||
| @moduledoc """ | ||
| Provides functions for adding inline style ranges and entity ranges | ||
| """ | ||
|
|
||
| def apply(text, inline_style_ranges, entity_ranges, entity_map) do | ||
| inline_style_ranges ++ entity_ranges | ||
| |> consolidate_ranges() | ||
| |> Enum.reduce(text, fn {start, finish}, acc -> | ||
| {style_opening_tag, style_closing_tag} = | ||
| case get_styles_for_range(start, finish, inline_style_ranges) do | ||
| "" -> {"", ""} | ||
| styles -> {"<span style=\"#{styles}\">", "</span>"} | ||
| end | ||
| entity_opening_tags = get_entity_opening_tags_for_start(start, entity_ranges, entity_map) | ||
| entity_closing_tags = get_entity_closing_tags_for_finish(finish, entity_ranges, entity_map) | ||
| opening_tags = "#{entity_opening_tags}#{style_opening_tag}" | ||
| closing_tags = "#{style_closing_tag}#{entity_closing_tags}" | ||
|
|
||
| adjusted_start = start + String.length(acc) - String.length(text) | ||
| adjusted_finish = finish + String.length(acc) - String.length(text) | ||
|
|
||
| acc | ||
| |> String.split_at(adjusted_finish) | ||
| |> Tuple.to_list | ||
| |> Enum.join(closing_tags) | ||
| |> String.split_at(adjusted_start) | ||
| |> Tuple.to_list | ||
| |> Enum.join(opening_tags) | ||
| end) | ||
| end | ||
|
|
||
| defp process_style("BOLD") do | ||
| "font-weight: bold;" | ||
| end | ||
|
|
||
| defp process_style("ITALIC") do | ||
| "font-style: italic;" | ||
| end | ||
|
|
||
| defp process_entity(%{"type"=>"LINK","mutability"=>"MUTABLE","data"=>%{"url"=>url}}) do | ||
| {"<a href=\"#{url}\">", "</a>"} | ||
| end | ||
|
|
||
| defp get_styles_for_range(start, finish, inline_style_ranges) do | ||
| inline_style_ranges | ||
| |> Enum.filter(fn range -> is_in_range(range, start, finish) end) | ||
| |> Enum.map(fn range -> process_style(range["style"]) end) | ||
| |> Enum.join(" ") | ||
| end | ||
|
|
||
| defp get_entity_opening_tags_for_start(start, entity_ranges, entity_map) do | ||
| entity_ranges | ||
| |> Enum.filter(fn range -> range["offset"] === start end) | ||
| |> Enum.map(fn range -> Map.get(entity_map, range["key"]) |> process_entity() |> elem(0) end) | ||
| end | ||
|
|
||
| defp get_entity_closing_tags_for_finish(finish, entity_ranges, entity_map) do | ||
| entity_ranges | ||
| |> Enum.filter(fn range -> range["offset"] + range["length"] === finish end) | ||
| |> Enum.map(fn range -> Map.get(entity_map, range["key"]) |> process_entity() |> elem(1) end) | ||
| |> Enum.reverse() | ||
| end | ||
|
|
||
| defp is_in_range(range, start, finish) do | ||
| range_start = range["offset"] | ||
| range_finish = range["offset"] + range["length"] | ||
|
|
||
| start >= range_start && finish <= range_finish | ||
| end | ||
|
|
||
| @doc """ | ||
| Takes multiple potentially overlapping ranges and breaks them into other mutually exclusive | ||
| ranges, so we can take each mini-range and add the specified, potentially multiple, styles | ||
| and entities to each mini-range | ||
|
|
||
| ## Examples | ||
| iex> ranges = [ | ||
| %{"offset" => 0, "length" => 4, "style" => "ITALIC"}, | ||
| %{"offset" => 4, "length" => 4, "style" => "BOLD"}, | ||
| %{"offset" => 2, "length" => 3, "key" => 0}] | ||
| iex> consolidate_ranges(ranges) | ||
| [{0, 2}, {2, 4}, {4, 5}, {5, 8}] | ||
| """ | ||
| defp consolidate_ranges(ranges) do | ||
| ranges | ||
| |> ranges_to_points() | ||
| |> points_to_ranges() | ||
| end | ||
|
|
||
| defp points_to_ranges(points) do | ||
| points | ||
| |> Enum.with_index | ||
| |> Enum.reduce([], fn {point, index}, acc -> | ||
| case Enum.at(points, index + 1) do | ||
| nil -> acc | ||
| next -> acc ++ [{point, next}] | ||
| end | ||
| end) | ||
| end | ||
|
|
||
| defp ranges_to_points(ranges) do | ||
| Enum.reduce(ranges, [], fn range, acc -> | ||
| acc ++ [range["offset"], range["offset"] + range["length"]] | ||
| end) | ||
| |> Enum.uniq | ||
| |> Enum.sort | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So my one overall note on this (aside from it being somewhat hard to follow). Why not us Elixir's actual ranges for this? (didn't really consider that in light of adjusting ranges as the text is modified but it would make finding overlaps and inclusions a little simpler if they could be used)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great minds. I started using ranges, which 1. were introduced in 1.8, so I wasn't pumped about that in a lib, but moreover 2. they are always inclusive, so it was cumbersome translating to/from the ranges that come from draftjs (which can be easily treated as non-inclusive "ranges," but really the offset+length thing made for some rough overhead)