Guest User

Untitled

a guest
Mar 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. // Initialize the loop variables
  2. let currentLine = [] // ops making up the current line
  3. const content = [] // Components that we will ultimately render
  4.  
  5. // Map parchment attributes to React styling object
  6. const attrsToStyle = (attrs) => {
  7. console.warn("Don't forget to implement styling!");
  8. return {};
  9. };
  10.  
  11. // Given an array of ops that make up a single "line" of Parchment text,
  12. // instantiate a component and push that component onto the content array.
  13. function opsToComponent(line) {
  14. if(!line) return null; // Empty line = no component
  15.  
  16.  
  17. const lineAttributes = _.last(line)['attributes'] || {};
  18.  
  19. // Handle cases that use different components
  20. const headerLevel = lineAttributes['header'] || false;
  21. if(headerLevel) {
  22. delete lineAttributes['header'];
  23. const key = "quill-header-" + _.size(content);
  24. return <Header key={key} level={headerLevel}>{opsToComponent(line)}</Header>;
  25. }
  26.  
  27. // TODO Handle list (line attribute)
  28. // TODO Handle indent (line attribute)
  29.  
  30. // Handle text cases
  31. return _.map(line, ({insert,attributes}, idx) => {
  32. if(insert == "\n" && !attributes) return null;
  33. const key = "quill-text-" + _.size(content) + "-" + idx;
  34. // TODO Handle link
  35. return <Text key={key} style={attrsToStyle(attributes)}>{insert}</Text>;
  36. });
  37. };
  38.  
  39. const flushLine = () => {
  40. if(_.size(currentLine) > 0) {
  41. const component = opsToComponent(_.cloneDeep(currentLine));
  42. console.info("Pushing Quill component", component);
  43. content.push(component);
  44. }
  45. currentLine = [];
  46. };
  47.  
  48. // Loop over operations
  49. _.each(ops, (op) => {
  50. console.info("Processing parchment op", op);
  51. currentLine.push(op);
  52. const {insert} = op;
  53. const insertSize = _.size(insert);
  54. const crIdx = insert.lastIndexOf("\n");
  55. if(crIdx === insertSize-1)) { // Has a carriage return at the very end
  56. flushLine();
  57. }
  58. });
  59. flushLine();
  60.  
  61. return <View>{content}</View>;
Add Comment
Please, Sign In to add comment